Skip to main content

Workloads & Scheduling

How to Update a Kubernetes Deployment Using the Recreate Strategy

The DevOpsCube team is preparing a major application update that requires the existing application Pods to be replaced completely during deployment. To avoid running different application versions simultaneously, the team has decided to use the Recreate deployment strategy.

Your task is to create a Deployment named devopscube-web with 3 replicas, using the nginx:1.18.0 image.

Verify that all 3 Pods are running successfully.

The development team then provides a new application version, nginx:1.19.9. Update the existing Deployment using the appropriate image update mechanism rather than directly editing the Deployment configuration.

Record the reason for this change and save the recorded change cause to cause.txt.

Verify that the rollout completes successfully and that the new Pods are running with the updated image. Check the Deployment's rollout status and revision history.

The team then decides that future updates to this application must use the Recreate strategy. Change the Deployment strategy accordingly.

Deploy another application version using nginx:1.24.0, record the reason for the change, and verify that the new Pods are running with the updated image.

Finally, roll the Deployment back to the previous revision and verify that:

  • The rollback completes successfully.
  • The Deployment returns to the expected previous image version.
  • All 3 Pods are running.
  • The Pods are using the expected image after the rollback.
  • The rollout history reflects the changes and rollback.
📚
For more details about Deployment, refer to the official Kubernetes documentation.

Create the Deployment

Create the Deployment with 3 replicas using NGINX 1.18.0:

$ kubectl create deployment devopscube-web \
  --image=nginx:1.18.0 \
  --replicas=3
  
deployment.apps/devopscube-web created

Check the Pods:

$ kubectl get pods

NAME                             READY   STATUS    RESTARTS   AGE
devopscube-web-7c45bf48f-2hs9n   1/1     Running   0          32s
devopscube-web-7c45bf48f-jd4x6   1/1     Running   0          32s
devopscube-web-7c45bf48f-skk9s   1/1     Running   0          32s

You should see 3 Pods in Running state.

Update the Application

The first update changes the NGINX image from 1.18.0 to 1.19.9. Since the task says not to edit the Deployment directly, use kubectl set image:

$ kubectl set image deployment/devopscube-web \
  nginx=nginx:1.19.9

Important CKA Note About --record

Older Kubernetes tutorials may use --record, but this option is deprecated/removed in modern Kubernetes versions. For the current CKA, don't rely on it to populate CHANGE-CAUSE.

If you need to record an explicit change cause, use a Deployment annotation:

kubectl annotate deployment devopscube-web \
  kubernetes.io/change-cause="Updated nginx image to 1.19.9" \
  --overwrite

This records the reason in the Deployment's revision history.

Verify the New Image

Check the Deployment to confirm that the image was updated successfully:

$ kubectl get deployment devopscube-web   -o jsonpath='{.spec.template.sp
ec.containers[0].image}{"\n"}'

nginx:1.19.9

Check Rollout History

Run the following command to verify the Deployment revisions and change cause:

$ kubectl rollout history deployment/devopscube-web

deployment.apps/devopscube-web 
REVISION  CHANGE-CAUSE
1         <none>
2         Updated nginx image to 1.19.9

You should see multiple revisions in the rollout history.

Save the Rollout Information

Save the rollout status and history to cause.txt:

$ kubectl rollout status deployment/techiescamp-web > cause.txt

$ kubectl rollout history deployment/techiescamp-web >> cause.txt

Verify:

cat cause.txt

REVISION  CHANGE-CAUSE
1         <none>
2         Updated nginx image to 1.19.9

You should see the rollout status followed by the rollout history.

Important: cause.txt contains the command output. The actual change cause is stored as a Deployment annotation and displayed by rollout history when available.

Change the Strategy to Recreate

The Recreate strategy stops all existing Pods before creating new ones.

Unlike RollingUpdate, the old and new Pods do not run at the same time, so there is a short period when no Pods are running.

You can edit the Deployment:

kubectl edit deployment devopscube-web
deployment.yaml before
strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate
deployment.yaml after
strategy: type: Recreate

Find the existing RollingUpdate strategy in the Deployment and replace it with Recreate.

Remove the rollingUpdate settings and keep only type: Recreate. The final configuration should have 3 replicas using the Recreate strategy.

Why remove rollingUpdate?

maxSurge and maxUnavailable apply only to the RollingUpdate strategy. When using Recreate, simply set the strategy type:

strategy:
  type: Recreate

Verify the Strategy

Run the following command to confirm that the Deployment uses the Recreate strategy:

$ kubectl get deployment devopscube-web \
  -o jsonpath='{.spec.strategy.type}{"\n"}'
  
Recreate

Update the Application to nginx:1.24.0

Update the Deployment to use the new NGINX image:

$ kubectl set image deployment/devopscube-web \
  nginx=nginx:1.24.0

Then record the change cause:

$ kubectl annotate deployment devopscube-web \
  kubernetes.io/change-cause="Updated nginx image to 1.24.0" \
  --overwrite

Verify the New Pods

Check:

$ kubectl get pods -l app=devopscube-web

NAME                              READY   STATUS    RESTARTS   AGE
devopscube-web-7bb6fc8969-klj5z   1/1     Running   0          5m
devopscube-web-7bb6fc8969-q8xvd   1/1     Running   0          5m
devopscube-web-7bb6fc8969-vvxhb   1/1     Running   0          5m

Check the images:

$ kubectl get deployment devopscube-web   -o jsonpath='{.spec.template.sp
ec.containers[0].image}{"\n"}'

nginx:1.24.0

Roll Back the Deployment

Undo the most recent Deployment revision to restore the previous version:

$ kubectl rollout undo deployment/devopscube-web

deployment.apps/devopscube-web rolled back

The Deployment will return to the previous revision, which uses:nginx:1.19.9

Check Rollout History Again

After the rollback, run kubectl rollout history again to verify the Deployment's revision history.

A rollback creates a new revision rather than deleting the previous one. The exact revision numbers may vary depending on the Deployment's history.

RollingUpdate vs Recreate

This is an important CKA comparison.

Feature RollingUpdate Recreate
Old Pods Gradually replaced Removed first
New Pods Created during rollout Created after old Pods are removed
Downtime Usually minimized Possible/expected
maxSurge Supported Not used
maxUnavailable Supported Not used
Typical use Frequent application updates Updates requiring clean replacement

CKA Exam Tips

These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.

1. Remember the Recreate Syntax

Use the following configuration for a Recreate strategy:

strategy:
  type: Recreate

Do not keep rollingUpdate settings such as maxSurge or maxUnavailable under a Recreate strategy.

2. Recreate Can Cause Downtime

The key difference is that with Recreate, Kubernetes removes the existing Pods before creating the new ones. This means the application can experience downtime during the update.

3. Don't Depend on --record in a Modern CKA Environment

Older CKA material often uses the --record option with commands such as kubectl set image. For current Kubernetes versions, --record is not a reliable approach, so use Deployment annotations when you need to record an explicit change cause.