Skip to main content
In this guide, we demonstrate how to update and rollback Kubernetes deployments through practical examples. You will learn how to create a deployment, verify rollout progress, update container images, and safely rollback to previous revisions when necessary. Previously, a deployment named “myapp-deployment” was defined in a YAML file located in the project’s deployment directory. Initially, this deployment ran three replicas of an NGINX container. In our updated configuration, the deployment now uses six replicas.
Ensure that your Kubernetes cluster is running and you have the necessary permissions to create, update, and delete deployments.

Creating the Deployment

Start by creating the updated deployment with the following YAML definition:
Before you create the deployment, confirm that there are no existing objects in your cluster:
Create the deployment using the -f option:
Monitor the rollout progress with:

Verifying Rollout Progress with Replicas

Immediately after creation, you can check the rollout status. As the six replicas are deployed sequentially, the output may display incremental progress:
Each message indicates the number of updated replicas that have become available. When all six pods are running, the deployment is considered successful.

Viewing Rollout History

After the deployment, you can review the history of your deployment revisions:
Initially, the output might look like this:
To track changes, delete the deployment, wait for the pods to terminate, and then recreate it with the --record flag:
Running the history command again will now display a recorded change cause.

Updating the Deployment Image

Step 1: Inspect the Deployment

First, view the details of the existing deployment:
You will see that the deployment runs six replicas using the “nginx” image.

Step 2: Update the Image

To update the image with a specific version, edit the deployment using:
Within the editor, change the container image from:
to
The complete snippet after the update should resemble:
Save and exit using :wq. Kubernetes will gradually roll out this update. You can track the update progress with:
After the new pods are created and the old ones terminated, verify the update by describing the deployment again:
The image should now appear as “nginx:1.18”.

Updating the Image with kubectl set image

To switch to a different image variant, you can use the kubectl set image command. For example, to update to “nginx:1.18-perl”, execute:
Monitor the rollout status with:
Example output:
Review the revision history to confirm the update:
An example output might be:

Rolling Back a Deployment

In the event that the new image (“nginx:1.18-perl”) introduces issues, you can rollback to a previous revision. For example, to rollback to revision 2:
After rolling back, inspect the deployment details:
You should confirm that the image has reverted to “nginx:1.18”. A new revision reflecting the rollback will appear in the history.

Simulating a Failed Update

To simulate a failed rollout, update the deployment with a non-existent image. Modify the YAML as shown below:
After saving your changes, check the rollout status:
You might see messages like:
At this point, Kubernetes will fail to pull the invalid image, and new pods will show an error state (e.g., ErrImagePull) while the previously running pods continue to serve traffic. Inspect the deployment and pod statuses using:
Example output:
And list the pods with:
Example output:
The pods experiencing ErrImagePull indicate that the non-existent image is causing the failed update, while the old pods continue to handle traffic. Review the updated revision history with:
A sample output could be:

Undoing the Faulty Update

To recover from the failed update, rollback to the previous stable revision (e.g., from revision 5 to revision 4) by running:
Monitor the rollout status after the rollback:
Finally, verify that the deployment now uses the correct image (“nginx:1.18”):
And check the deployment details:
All six pods should be running the stable image with the faulty pods replaced.

Final Notes

Kubernetes deployments employ a rolling update strategy designed to ensure continuous application availability during updates. New replicas are only promoted when they are confirmed healthy, reducing downtime and mitigating risks even when faulty updates occur.
The image lists supported tags and Dockerfile links for NGINX Docker images, maintained by the NGINX Docker Maintainers, with help resources provided.
This concludes our demonstration on handling updates and rollbacks in Kubernetes deployments. In upcoming segments, we will explore advanced deployment patterns and troubleshooting techniques for more complex scenarios.

Watch Video

Practice Lab