peter (admin)   -  



 

Kubernetes Rollback

We will learn how to perform a rollback in Kubernetes so we can return to a previous working version of an application when an update causes a problem.

This helps quickly recover services and maintain application stability when something breaks during a deployment update.

Let's get started.

Check the Deployment Rollout History

For this, we are already logged into both the Master node and the Worker node.

Let's move on and first check the rollout history of the deployment by using:

kubectl rollout history deployment web-app

We use this command to understand which changes were applied and which revision we can return to if something goes wrong.

After pressing Enter, Kubernetes shows the deployment name web-app and lists its revision history.

For example:

deployment.apps/web-app

REVISION    CHANGE-CAUSE
1           <none>
2           kubectl set image deployment/web-app nginx=nginx:1.26

Revision 1 shows the change cause as <none>, which means that update was created without a recorded description.

Revision 2 shows the image update command that changed the deployment to NGINX version 1.26.

Simulate a Failed Deployment

Next, we intentionally update the deployment using a command that changes the container image of the web-app deployment to an image tag that does not exist.

kubectl set image deployment/web-app nginx=nginx:does-not-exist

The set image command modifies the container image used by the deployment. Here, we specify the container named nginx and assign it an invalid image version called does-not-exist.

Because this image does not exist, Kubernetes will not be able to find it in the container registry.

We intentionally use this incorrect image to simulate a failure scenario. This allows us to observe what happens when a deployment update fails and then learn how to recover the application using Kubernetes rollback functionality.

After pressing Enter, Kubernetes accepts the change and attempts to create new pods using the incorrect image.

You may also see a warning stating that the --record flag is deprecated if it was included in the command. This option is no longer required and can simply be removed.

The deployment update will still work normally without it.

Kubernetes should display a message similar to:

deployment.apps/web-app image updated

This means Kubernetes has accepted the change and will now attempt to create new pods using the incorrect image.

Check the Failed Pods

Now let's verify the result by retrieving the pods that belong to the web-app application.

kubectl get pods -l app=web-app

After pressing Enter, you may see that the existing pods are still running successfully, while a newly created pod has a status such as:

ImagePullBackOff

This happens because Kubernetes tried to pull the container image using the invalid does-not-exist tag, but the image could not be found in the container registry.

As a result, the new pod fails to start.

This confirms that the deployment update has introduced a problem and the new version of the application cannot run successfully.

Examine the Image Pull Failure

Let's retrieve more detailed information about the pods using the describe command.

For example:

kubectl describe pods -l app=web-app

We can also combine the command with sed to display only a specific portion of the output.

For example:

kubectl describe pods -l app=web-app | sed -n '1,120p'

After pressing Enter, you can see that the container image is set to:

nginx:does-not-exist

This is the incorrect image that we intentionally configured earlier.

Because this image tag does not exist in the container registry, Kubernetes cannot download the container image.

As a result, the container state shows Waiting with a reason such as:

ImagePullBackOff

The Events section at the bottom also shows that Kubernetes repeatedly attempted to pull the image but failed.

Roll Back the Deployment

Now let's perform a rollout undo, which returns the deployment to the previous working version before the problem occurred.

We do this because the last update used an incorrect container image that caused the new pods to fail.

Run:

kubectl rollout undo deployment web-app

After pressing Enter, you should see a message similar to:

deployment.apps/web-app rolled back

This means Kubernetes has restored the deployment to its previous configuration.

Check the Rollback Status

Next, we check the rollout progress using:

kubectl rollout status deployment web-app

This allows us to confirm that the recovery process completed successfully.

After pressing Enter, you should see:

deployment "web-app" successfully rolled out

This confirms that Kubernetes has completed the rollback and the deployment has returned to a working state.

Verify the Pods After the Rollback

To verify the recovery, retrieve the pod information again:

kubectl get pods -l app=web-app

The pods should now be in the Running state with their containers ready.

For example:

NAME                       READY   STATUS    RESTARTS   AGE
web-app-xxxxxxxxxx-xxxxx   1/1     Running   0          30s
web-app-xxxxxxxxxx-xxxxx   1/1     Running   0          30s

This confirms that the rollback successfully restored the previous working version of the deployment and the application is running normally again.

Verify the Application from the Worker Node

We can also verify the application from the Worker node.

Checking the application through the worker node's NodePort helps confirm that the Kubernetes Service is reachable and that the application is responding correctly.

Use curl with the worker node IP address and the NodePort number configured in the previous lectures:

curl -I http://<WORKER-NODE-IP>:<NODEPORT>

After pressing Enter, you should receive a response similar to:

HTTP/1.1 200 OK
Server: nginx/1.26.x

The HTTP/1.1 200 OK response confirms that the web application is accessible.

The server information showing NGINX version 1.26 confirms that Kubernetes successfully returned the deployment to the previous working container image.

How Kubernetes Rollback Works

A Kubernetes rollback allows us to recover from a failed deployment by returning the Deployment to a previous revision.

In this example, the process was:

Working Deployment
       ↓
nginx:1.26
       ↓
Update Deployment
       ↓
nginx:does-not-exist
       ↓
New Pod Fails
       ↓
ImagePullBackOff
       ↓
kubectl rollout undo
       ↓
Previous Configuration Restored
       ↓
nginx:1.26
       ↓
Pods Running
       ↓
Application Recovered

This provides a quick way to recover when a deployment introduces an incorrect image or other problematic configuration.

Instead of manually rebuilding the previous configuration, Kubernetes can use the Deployment's revision history to restore the previous working version.

 

 


Comments (0)

There are no comments yet. Be the first to comment.

Log in or register to leave a comment. all comments are subject to admin approval