Skip to main content
In this lesson, you’ll learn how to update and roll back deployments in Kubernetes. We’ll start with a deployment definition file located in the “deployment” directory. The deployment, named “myapp-deployment”, runs six replicas using the NGINX image. Below is the deployment YAML definition:
Before creating the deployment, verify that no similar objects exist in your cluster.

Creating the Deployment

First, check that there are no existing objects:
Once confirmed, create the deployment using:
After creating the deployment, check its rollout status:
You should see an output like:
Keep in mind that if you check the rollout status immediately after creation, you might witness an intermediate state as Kubernetes updates each pod sequentially.

Demonstrating Rollout Status with Immediate Deletion

To better illustrate how the rollout process works, follow these steps:
  1. Delete the current deployment:
  2. Re-create the deployment:
  3. Immediately check the rollout status:
You may encounter messages such as “0 of 6 updated replicas”, “1 of 6 updated replicas”, etc. Kubernetes waits until all six pods are running before marking the rollout as successful.

Checking Rollout History

Once the deployment is live, view its revision history with:
Example output:
Since the change cause was not recorded initially, the change cause column is empty.

Recording the Change Cause

To capture the reason behind changes, delete the existing deployment and re-create it using the --record option:
  1. Delete the deployment and wait until all pods terminate:
  2. Confirm deletion by checking the pods:
  3. Re-create the deployment with the record flag:
  4. Monitor the rollout status:
After completion, verify the revision history:
The output should now show an entry with a recorded change cause:

Updating the Deployment with kubectl edit

To update the deployment, start by viewing its details:
Notice in the annotations section that the create command is recorded and the deployment is running six replicas of the NGINX image. Now, update the container image interactively and record the change:
In the editor, change the container’s image from nginx to nginx:1.18 (a lower version). An example edit might look like this:
Save and exit the editor. Kubernetes will begin rolling out the update by gradually replacing the old pods with new ones that have the updated image. Monitor the progress with:
Finally, check the rollout history to confirm the change:
You should now see a new entry corresponding to the kubectl edit action.
The image shows a terminal displaying Kubernetes deployment details, including namespace, replicas, strategy type, and container image version (nginx:1.18).

Using kubectl set image for a Different Update Method

Another approach to update the container image is by using the kubectl set image command. For instance, to update the container image to nginx:1.18-perl, execute:
Then, verify the rollout status:
The status messages will indicate that the old replicas for version 1.18 are being replaced by new replicas running version 1.18-perl. Confirm the updated revision history with:
Expected revision history:

Rolling Back to a Previous Revision

If the new image (version 1.18-perl) causes issues, you can easily roll back to a previous version. To revert from revision 3 to revision 2 (running NGINX 1.18), execute:
Monitor the rollback process:
Once complete, confirm the current deployment configuration:
Note that while the rollout history might show an updated revision number, the state will match the previous, stable revision.

Simulating a Failed Rollout

To demonstrate how Kubernetes handles a failed rollout, modify the deployment to use a non-existent image. Start by editing the deployment:
Change the container image to an invalid name, such as nginx:1.18-does-n, as shown below:
Save and exit the editor. At this point, check the rollout status:
In a separate terminal, you can inspect the deployment and pod statuses:
You will observe that while five pods continue running with the previous configuration, the new pods trying to run the invalid image show an “ErrImagePull” error. Even with some pods failing, the application remains accessible via the running pods.

Rolling Back the Failed Deployment

The rollout history now includes a new revision (for example, revision 5) that reflects the failed update. To restore stability (rolling back to revision 4), run:
Then, monitor the rollback progress:
Finally, verify that all pods are running the correct image version (NGINX 1.18) by checking the pods:
The pod status should look similar to:
Review the complete rollout history to verify all revisions and their change causes:

Conclusion

In this lesson, you have learned to:
  • Create a Kubernetes deployment using a YAML file.
  • Monitor rollout progress using kubectl rollout status.
  • Record change causes with the --record flag.
  • Update the deployment interactively using kubectl edit and via the kubectl set image command.
  • Simulate a failed rollout scenario with an invalid image.
  • Roll back to a previous revision using kubectl rollout undo.
This approach ensures that your updates are applied smoothly and, if issues arise, can be quickly reverted to maintain application availability. For further reading, consider exploring the Kubernetes Documentation and additional Kubernetes Basics.

Watch Video