0
0
Kubernetesdevops~5 mins

Rolling update strategy in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you update an application running in Kubernetes, you want to avoid downtime. The rolling update strategy updates your app gradually, replacing old versions with new ones without stopping the whole app at once.
When you want to update your app without making it unavailable to users.
When you need to fix bugs or add features to your app while it is running.
When you want to test new versions of your app safely by updating pods one by one.
When you want to keep your app stable and avoid sudden crashes during updates.
When you want Kubernetes to manage the update process automatically.
Config File - deployment.yaml
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: example-app
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-container
        image: nginx:1.21
        ports:
        - containerPort: 80

This file defines a Deployment named example-deployment that runs 3 copies (replicas) of an Nginx container.

The strategy section sets the update method to RollingUpdate. It controls how many pods can be unavailable (maxUnavailable: 1) and how many extra pods can be created during the update (maxSurge: 1).

This means Kubernetes will update pods one by one, keeping the app available.

Commands
This command creates or updates the Deployment using the configuration file. It starts running 3 pods with the specified Nginx image.
Terminal
kubectl apply -f deployment.yaml
Expected OutputExpected
deployment.apps/example-deployment created
This command lists all pods with the label app=example-app to check that the pods are running.
Terminal
kubectl get pods -l app=example-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-deployment-5d8f9f7d7f-abc12 1/1 Running 0 10s example-deployment-5d8f9f7d7f-def34 1/1 Running 0 10s example-deployment-5d8f9f7d7f-ghi56 1/1 Running 0 10s
-l - Filter pods by label
This command updates the Deployment to use a new image version nginx:1.22, triggering a rolling update.
Terminal
kubectl set image deployment/example-deployment example-container=nginx:1.22
Expected OutputExpected
deployment.apps/example-deployment image updated
This command shows the progress of the rolling update to confirm it finishes successfully.
Terminal
kubectl rollout status deployment/example-deployment
Expected OutputExpected
deployment "example-deployment" successfully rolled out
This command lists the pods again to verify that all pods are running the new image version.
Terminal
kubectl get pods -l app=example-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-deployment-6a7b8c9d0e-xyz12 1/1 Running 0 30s example-deployment-6a7b8c9d0e-uvw34 1/1 Running 0 30s example-deployment-6a7b8c9d0e-rst56 1/1 Running 0 30s
-l - Filter pods by label
Key Concept

If you remember nothing else from this pattern, remember: rolling updates replace pods gradually to keep your app running without downtime.

Common Mistakes
Not specifying the rolling update strategy in the Deployment, causing Kubernetes to use the default or recreate all pods at once.
This can cause downtime because all pods might stop before new ones start.
Always set the strategy type to RollingUpdate with maxUnavailable and maxSurge values to control the update flow.
Setting maxUnavailable or maxSurge to 0 or too high values without understanding the impact.
Setting maxUnavailable to 0 means no pods can be down, which might block updates; setting maxSurge too high can overload resources.
Use small values like 1 for maxUnavailable and maxSurge to balance availability and update speed.
Not checking rollout status after updating the image.
You might miss errors during the update and end up with a broken app.
Always run 'kubectl rollout status' to confirm the update completed successfully.
Summary
Create a Deployment with a rolling update strategy to update pods gradually.
Use 'kubectl set image' to change the container image and trigger the rolling update.
Check the update progress with 'kubectl rollout status' to ensure no downtime.