Rolling updates are a crucial feature of Kubernetes Deployments that allows you to update your application with zero downtime. This guide provides a detailed explanation of rolling updates, which is an important topic for the CKAD exam.
Rolling updates allow you to update your application declaratively. When you update the Pod template defined within the Deployment manifest, the Deployment controller replaces the old Pods with new Pods created using the updated template.
During a rolling update, the Deployment controller replaces Pods gradually, ensuring your application remains available throughout the update process.
Kubernetes Deployments support two update strategies:
The key difference is that RollingUpdate maintains availability during updates, while Recreate causes downtime but ensures no two versions run simultaneously.
You can configure the rolling update behavior using two parameters:
Specifies the maximum number of Pods that can be unavailable during the update process. Can be an absolute number or percentage of desired Pods.
Specifies the maximum number of Pods that can be created over the desired number of Pods. Can be an absolute number or percentage of desired Pods.
Example configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # Maximum one Pod over desired count
maxUnavailable: 1 # Maximum one Pod can be unavailable
template:
# Pod template definition
When you update a Deployment, the following process occurs:
You can control the update process with these commands:
kubectl rollout status deployment/my-app
kubectl rollout pause deployment/my-app
kubectl rollout resume deployment/my-app
If an update causes issues, you can roll back to a previous version:
kubectl rollout undo deployment/my-app
# First check available revisions
kubectl rollout history deployment/my-app
# Then roll back to a specific revision
kubectl rollout undo deployment/my-app --to-revision=2
Let’s walk through a complete example of updating a Deployment:
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: rolling-demo
spec:
replicas: 4
selector:
matchLabels:
app: nginx
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19.0
ports:
- containerPort: 80
EOF
kubectl get deployment rolling-demo
kubectl get pods -l app=nginx
kubectl set image deployment/rolling-demo nginx=nginx:1.20.0
kubectl rollout status deployment/rolling-demo
In another terminal, you can see the Pods being created and terminated:
kubectl get pods -l app=nginx -w
kubectl get rs
You’ll see both the old and new ReplicaSet, with the old one scaled down to 0.
kubectl rollout history deployment/rolling-demo
kubectl rollout undo deployment/rolling-demo
kubectl set image deployment/my-app my-container=my-image:v2 --record
If new Pods aren’t becoming ready, check:
kubectl describe pod <pod-name>kubectl logs <pod-name>If a rollout appears stuck, check:
kubectl describe deployment <n>kubectl get podsIf rollouts are taking too long:
kubectl set image deployment/name container=image:tagkubectl rollout status deployment/namekubectl rollout history deployment/namekubectl rollout undo deployment/namePractice monitoring rollouts with kubectl get pods -w
Mastering rolling updates is essential for modern application deployment and is a key topic on the CKAD exam. By understanding how to configure and manage rolling updates, you’ll be able to update applications with zero downtime and quickly roll back if issues occur.
Remember that rolling updates are one of the main advantages of using Deployments over directly managing ReplicaSets or Pods. This feature makes Deployments the preferred resource for deploying stateless applications in Kubernetes.