Kubernetes Deployments and rolling updates: updating your app without downtime

  1. Goal
  2. Introduction: why not create Pods directly
  3. Preparation
  4. Steps
  5. 1. Create the Deployment
  6. 2. Understand the two pace parameters
  7. 3. Update the version
  8. 4. View history and record the reason
  9. 5. Pause and restart
  10. Verification
  11. Common mistakes
  12. Summary
  13. References

Goal

After this article you can create a Deployment, change the image version, watch Kubernetes replace Pods in batches, and tune the pace of the update.

The article uses nginx as a sample app because it is easy to try.

Introduction: why not create Pods directly

A standalone Pod does not replace itself if its node fails, and it has no notion of an "update".

A Deployment solves both: you declare the desired state ("run 3 replicas of this image"), the Deployment controller creates a ReplicaSet that keeps exactly that many Pods, and it keeps comparing reality with your declaration.

When you change the version, the Deployment does not edit running Pods. It creates a new ReplicaSet for the new version, gradually increases the new ReplicaSet's Pod count and decreases the old one's.

This is called a rolling update, and it is the default strategy of a Deployment.

Preparation

Steps

Steps

1. Create the Deployment

Save a file named web.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: thu-nghiem
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: nginx:1.26
          ports:
            - containerPort: 80
          readinessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 3
            periodSeconds: 5

Apply it and wait for completion:

kubectl apply -f web.yaml
kubectl -n thu-nghiem rollout status deployment/web
kubectl -n thu-nghiem get pods -l app=web

You should see 3 Pods that are Running and READY 1/1.

2. Understand the two pace parameters

If you do not set them, both default to 25%. In the example above, maxSurge: 1 and maxUnavailable: 0 mean: there are always 3 ready Pods, and at most 1 extra new Pod is created at any moment.

This is the cautious choice; the price is a slower update, because each new Pod must pass its readiness probe before an old Pod is removed.

3. Update the version

There are two ways. The declarative way: edit image: nginx:1.27 in web.yaml and run kubectl apply -f web.yaml. The quick way with a command:

kubectl -n thu-nghiem set image deployment/web web=nginx:1.27
kubectl -n thu-nghiem rollout status deployment/web

In another terminal, run kubectl -n thu-nghiem get pods -l app=web -w to watch new Pods appear and old Pods disappear one after another.

Note: only changes in .spec.template (image, environment variables, Pod labels...) trigger a rollout. Changing replicas only scales; it does not create a new version.

4. View history and record the reason

kubectl -n thu-nghiem rollout history deployment/web
kubectl -n thu-nghiem annotate deployment/web \
  kubernetes.io/change-cause="upgrade nginx to 1.27" --overwrite

Every template change creates one more revision. The kubernetes.io/change-cause annotation makes the reason column in the history meaningful.

By default Kubernetes keeps at most 10 revisions (revisionHistoryLimit); older ReplicaSets are cleaned up.

5. Pause and restart

kubectl -n thu-nghiem rollout pause deployment/web
# change several things at once without triggering a rollout
kubectl -n thu-nghiem rollout resume deployment/web
kubectl -n thu-nghiem rollout restart deployment/web

pause lets you batch several changes into a single rollout. restart replaces all Pods using the same rolling mechanism without changing the configuration, which is useful when the application needs to re-read its configuration.

Verification

Common mistakes

To build small images for deployment, see also Multi-stage Dockerfiles: heavy builds, light runtime images.

Summary

A Deployment turns updating into a declarative action: change the template and Kubernetes replaces Pods in batches.

Tune maxSurge and maxUnavailable, and always add a readiness probe to balance speed with safety.

Clean up the experiment with kubectl delete namespace thu-nghiem (this deletes everything in that namespace, so make sure you are on the right cluster).

References

Kubernetes Deployments and rolling updates: updating your app without downtime