How to Perform a Kubernetes Deployment Rolling Update and Rollback
The DevOpscube team is preparing a critical web application for production. The application is expected to receive frequent updates, so the team needs a Deployment strategy that allows application versions to be updated gradually while maintaining availability and providing the ability to quickly return to a previous version if an update causes problems.
Your task is to create a Deployment named devopscube-app with 3 replicas. The application Pods must use nginx:1.18.0 and expose container port 80.
Configure the Deployment to use a rolling update strategy with:
maxSurge: 1maxUnavailable: 2
After deploying the application, verify that all 3 Pods are running.
The development team has released a new application version. Update the Deployment to use nginx:1.19.10 and verify that the rollout completes successfully and the new Pods are running with the updated image.
Inspect the Deployment's rollout status and revision history.
If you need to revert the new version, roll the Deployment back to the previous revision. Verify that the application returns tonginx:1.18.0, and all 3 Pods are running.
Deployment, refer to the official Kubernetes documentation.Create the Deployment
Generate the Deployment manifest with 3 replicas using NGINX 1.18.0. The --dry-run=client option creates the YAML without creating the Deployment in the cluster.
kubectl create deployment devopscube-app \
--image=nginx:1.18.0 \
--replicas=3 \
--dry-run=client -o yaml > deploy.yamlConfigure the Rolling Update Strategy
Edit the generated manifest to configure the Deployment's rolling update behavior
Why RollingUpdate?
In simple terms, maxSurge controls how many extra Pods Kubernetes can create during an update, while maxUnavailable controls how many existing Pods can be unavailable at the same time.
With 3 replicas, maxSurge: 1 allows up to 4 Pods temporarily, and maxUnavailable: 2 allows up to 2 Pods to be unavailable during the rollout
Understanding maxSurge and maxUnavailable
In simple terms, maxSurge controls how many extra Pods Kubernetes can create during an update, while maxUnavailable controls how many existing Pods can be unavailable at the same time.
With 3 replicas, maxSurge: 1 allows up to 4 Pods temporarily, and maxUnavailable: 2 allows up to 2 Pods to be unavailable during the rollout.
Create the Deployment
Apply the manifest to create the Deployment:
$ kubectl apply -f deploy.yaml
deployment.apps/devopscube-app createdCheck the Deployment status:
$ kubectl get deployment devopscube-app
NAME READY UP-TO-DATE AVAILABLE AGE
devopscube-app 0/3 3 0 66sThen check the Pods:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
devopscube-app-684fbcdc8c-29gv7 1/1 Running 0 27s
devopscube-app-684fbcdc8c-9j66v 1/1 Running 0 27s
devopscube-app-684fbcdc8c-kwmrb 1/1 Running 0 27sYou should eventually see 3 Pods in Running state, matching the configured replica count.
Update the Application
The application now needs to be updated to a newer NGINX image.
There are multiple ways to update a Deployment. we using kubectl set image
For the CKA, kubectl set image is a quick way to update the Deployment without editing the manifest.
$ kubectl set image deployment/devopscube-app \
nginx=nginx:1.19.10
deployment.apps/devopscube-app image updatedThis changes the Deployment's Pod template, which triggers a rolling update.
Watch the Rolling Update
Check the Pods:
$ kubectl get pods -l app=devopscube-app -w
NAME READY STATUS RESTARTS AGE
devopscube-app-74cf869f9f-9ggl7 1/1 Running 0 3m54s
devopscube-app-74cf869f9f-rc2sn 1/1 Running 0 3m54s
devopscube-app-74cf869f9f-vm9kq 1/1 Running 0 3m54sYou will see old Pods being replaced by new Pods.
You can also check ReplicaSets:
$ kubectl get replicasets
NAME DESIRED CURRENT READY AGE
devopscube-app-5d79dcb878 0 0 0 20m
devopscube-app-684fbcdc8c 0 0 0 14m
devopscube-app-74cf869f9f 3 3 3 5mDuring the rollout, Kubernetes maintains the old and new ReplicaSets while replacing the Pods.
Verify the New Image
Check that the Deployment is using the new NGINX image:
$ kubectl get deployment devopscube-app \
-o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
nginx:1.19.10Check Rollout Status
Verify that the rolling update completed successfully:
$ kubectl rollout status deployment/devopscube-app
deployment "devopscube-app" successfully rolled outThis confirms that the rolling update completed successfully.
Roll Back the Deployment
If the new version causes a problem, roll back the Deployment to the previous revision:
$ kubectl rollout undo deployment/devopscube-app
deployment.apps/devopscube-app rolled backThis restores the Deployment to its previous version.
Verify the Rollback
Check the rollout:
$ kubectl rollout status deployment/devopscube-app
deployment "devopscube-app" successfully rolled outThen check the image:
$ kubectl get deployment devopscube-app \
-o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
nginx:1.18.0CKA Exam Tips
These tips will help you quickly identify key Kubernetes concepts and avoid common mistakes during the CKA exam Hands-on Practice.
1. kubectl set image Is Fast
Instead of using kubectl edit deployment devopscube-app, you can quickly update the container image with kubectl set image. This is often faster during the CKA.
2. Always Check Rollout Status
After changing a Deployment, use kubectl rollout status deployment/techiescamp-app to verify whether the rollout completed successfully.
3. Know the Three Essential Rollout Commands
Remember these three commands: rollout status checks the current rollout, rollout history shows previous revisions, and rollout undo rolls the Deployment back to a previous revision. These commands are especially useful for Deployment and rollback tasks in the CKA.