This blog post will show you how to update a container image in a Kubernetes cluster using a rolling update strategy.
Rolling Update
The rolling update feature allows us to update a Kubernetes deployment without effecting the workloads and gradually replace pods with a new image.
Below, you can see the code block (strategy) that controls a rolling update. maxSurge control how many new pods with the new image will be added to the deployment while maxUnavailable control how many pods will be deleted.
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
Configuration
Below, you can see the deployment file with all the configuration blocks, including the strategy block that controls a rolling update.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx strategy: rollingUpdate: maxSurge: 1 maxUnavailable: 1 type: RollingUpdate template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.19.7 ports: - containerPort: 80
To deploy a new image, I will change the container image version and run the deployment using the following line.
kubectl apply -f main.yaml
To check the deployment status, I will run the rollout status with the deployment name, as shown below.
kubectl rollout status deployments nginx-deployment
The output below shows how a rolling update control the process of updating and image.
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination… Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination… Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination… Waiting for deployment "nginx-deployment" rollout to finish: 1 of 2 updated replicas are available… deployment "nginx-deployment" successfully rolled out