Loading Posts...

Kubernetes Deployment Rolling Updates And Rolling Back

Previously, I published an article about Deploying a Kubernetes Cluster on Oracle Ravello Cloud Platform and I hope you already enjoyed it. In this post I thought to share how to create a deployment, rolling updates and rolling them back if you see any issues after the updates.

Creating A Kubernetes Deployment

In my lab I deployed a Nginx deployment with 3 replicas and below is the yaml that I used for the deployment. It’s simple deployment but still good enough to test my rolling updates.

apiVersion: apps/v1
kind: Deployment
metadata: 
  labels: 
    app: nginx
  name: nginx-deployment
spec: 
  replicas: 3
  selector: 
    matchLabels: 
      app: nginx
  template: 
    metadata: 
      labels: 
        app: nginx
    spec: 
      containers: 
        - 
          image: "nginx:1.7.9"
          name: nginx

Before you proceed with the deployment, make sure to validate it with a yaml validator. I use “YAML Lint” validator to validate my deployments. if it is a valid yaml it will tell you or you will redirect to the line you have errors with.

Kubernetes Deployment Rolling Updates : YAML Lint Validator

First of all I checked the cluster and node status with “kubectl” prior to the deployment

kubectl get nodes

Created my yaml file as “nginx1.7.9.yaml”, then created a deployment with “kubectl” and pointing the file with “-f” flag

kubectl create -f [yaml_file]

Checked the Deployment

Kubectl get deployments

Make sure all your replicas in ready state, if not just wait and execute the same command again and see the status of replicas. See below screen capture from my console

Kubernetes Deployment Rolling Updates : Create a deployment

Verified the deployment details with below command, you can see more details about the deployment such as created time stamp, labels, revision number, labels, selector, image version, events, etc. Here I have highlighted the information we need in later steps, but there are many details you can see about your deployment.

kubectl describe deployment [deployment name]

Kubernetes Deployment Rolling Updates : Describe deployment

Deployment Rolling Updates

I updated the running Nginx version to 1.13.10 from my previous version 1.7.9, I randomly selected it from the list of later nginx versions.

Executed below command to set the running image to 1.13.10

kubectl set image deployment/[deployment_name] nginx=nginx:[new_version]

To check the rollout status use below command

kubectl rollout status deployment/[deployment_name]

Check the status of the rollout in the console

Kubernetes Deployment Rolling Updates : rollout the update

Check the deployment status with “kubectl describe” command

Kubernetes Deployment Rolling Updates : Check the applied updates

Rolling Updates Using YAML File

There is another method to rollout the updates. We have deployed our deployment using yaml file, simply edit the version in the yaml file using a text editor and reapply would do the job for us.

Kubernetes Deployment Rolling Updates : Edit the deployment File

To reapply the changes use below command

kubectl apply -f [file]

Kubernetes Deployment Rolling Updates : Apply the file

You can check the rollout status from below piece of command

kubectl rollout status deployment/[deployment_name]

Kubernetes Deployment Rolling Updates : Check Rollout Status

Checked the nginx version

Kubernetes Deployment Rolling Updates : Check nginx version after the rollout

Rolling Back The Kubernetes Deployment

So, let’s see how we can rollback the deployment updates to a previous version. We can’t expect always to be successful after an update and there can be many reasons to roll back an update after applying to a deployment

Remember, I mentioned “revision number” in the kubectl describe command and this is the place where it comes in to play. You can check the versions and some other important template details in deployments with the below command

kubectl rollout history deployment/nginx-deployment --revision=[revision_number]

See below output which I ran to all of the revisions which I had in my deployment updates

Kubernetes Deployment Rolling Updates : Check templates with revision number

Roll back to the previous version with the revision number using the below command, here I posted the previous output to make it easy for you to read and understand

Kubernetes Deployment Rolling Updates : Rolling back

Check the deployment status again with the kubectl describe command. Please note that revision number will be updated and will not be the previous number. Just a note.

Kubernetes Deployment Rolling Updates : Check Roll back status

Just try it out if you need and you can take a copy of my Oracle Ravello Cloud blueprint to deploy a 4 node cluster easily from here. Your comments are always welcome, as always.

If You Found This Post As Useful Please Rate The Post And Share It!

Click to rate this post!
[Total: 11 Average: 5]

Aruna Lakmal

Associate Technical Specialist, Sri Lanka. Technology junky, enthusiast, a VMware vExpert and a blogger with more than 8 years of Experience in Virtualization and Cloud Native technologies.

Get Updates Directly To Your Inbox!

   

Leave a Comment

Loading Posts...