Blog Post

Kubernetes Deployment Using Jenkins with Auto Rollback for Continuous Delivery

15Jun,16 Post Image

Containerization and OS-level virtualization has enabled unprecedented efficiencies in deploying applications faster and predictably while optimizing hardware resource utilization. With the old ways of deploying applications on the host, immutable VM images were heavy, less portable and provided little flexibility that’s demanded by DevOps-driven continuous delivery process requirements.

A new container-centric infrastructure approach toward application deployment is required to isolate filesystems, processes and resources from each other as well as the host. Kubernetes, the open-source platform automates containerized deployment of application builds, decoupled from the underlying infrastructure. The result is a streamlined approach toward continuous delivery with a portable, extensible and self-healing deployment process.

However, not every build is deployed successfully, and failed deployments can bottleneck or even halt the continuous delivery process.While Jenkins allows manual rollback for failed deployments, DevOps teams moving fast and failing often pursue automated solutions to replace mundane manual rollback processes. So we wrote a script for Kubernetes deployment using Jenkins that will enable you to automatically rollback to a previous known working state in case of failed deployment. Here is the gist link for the sript

Here’s the walk through:

1. Create a Jenkins a job and add Git configuration:

kub-image-1
2. Use the example below to check for the template deploy.tml.yaml in the micro-service:

Template :deploy.tmp.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: __NAME__
spec:
  replicas: __REPLICA__
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: __IMAGE__
        ports:
        - containerPort: 80

kub-image-2

3. Set up variables that will be used by the continuous integration Jenkins server. The required piece of information includes the names of the server, container and Docker image. Also specify the number of application versions or parts – known as PODS – on which the Kubernetes platform will run automated deployment operations.

NAME

PODS_COUNT

CONTAINER_NAME

DOCKER_IMAGE

4. Add execute shell as build script and paste the script from this link.

kub-image-3

5. Update the Jenkins variables using the existing template deploy.tmpl.yaml.Enter the location path of the file to start runningcommands against Kubernetes clusters from the appropriate location.

TEMPLATE=$(cat deploy.tmpl.yaml)
kubectl=/usr/local/bin/kubectl
kubectl="/usr/local/bin/kubectl --server=http://172.31.37.103:8080 --namespace=firefly-sidekiq"

Script automatically  rollback in event of unsuccessful deployment. The appropriate function is called to perform this operation for each build deployment. Yaml  template has  the variables. Utilize different flow patterns for existing and new deployments. Check for existing deployments, perform an update if available and notify the user accordingly. Poll for deployment status again to confirm the updated build has been deployed.

If the existing deployment has not been updated, mark the poll status function as not OK and initiate deployment rollback automatically. Notify the user accordingly. Furthermore, create a deployment first and recheck the poll status to confirm the deployment has indeed been created this time, and notify the user accordingly.

Need help? Contact nCloud if you’re facing issues using Kubernetes for automated deployment and rollback of your application builds within your DevOps environment.

  • Rafael Blázquez

    Thanks for the tips and the scripts!!

    I still have no clear where could be the best place to store the deploy.tmpl.yaml

    Can you provide some suggestions?

    • That Guy

      you can store where ever you have your kubectl configured.

    • jt

      @r@rafaelblzquez:disqus one other option. You can do something like this. So you don’t have to save the file ,

      echo $NGINX
      nginx:1.9.1
      cat << EOF | ./kubectl create -f –
      apiVersion: extensions/v1beta1
      kind: Deployment
      metadata:
      name: nginx-deployment
      spec:
      replicas: 3
      template:
      metadata:
      labels:
      app: nginx
      spec:
      containers:
      – name: nginx
      image: $NGINX
      ports:
      – containerPort: 80
      EOF

  • http://www.koltovich.com Slava Koltovich

    What in this case does “failed deployment” mean? I tried two cases: image is not available (ErrImagePull) and application crashes on start (CrashLoopBackOff). In both cases script says that deployment was successfully updated. Isn’t it considered a failed deployment?

Subscribe to Our Newsletter