In this lab we will see the Recreate and RollingUpdate strategies in action.

Recreate strategy

We will use the YAML below where we explicitly set the deployment strategy to Recreate.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
  labels:
    app.kubernetes.io/name: hello
spec:
  replicas: 5
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app.kubernetes.io/name: hello
  template:
    metadata:
      labels:
        app.kubernetes.io/name: hello
    spec:
      containers:
        - name: hello-container
          image: busybox
          command: ["sh", "-c", "echo Hello from my container! && sleep 3600"]

Save the above YAML to deployment-recreate.yaml file and create the deployment:

kubectl apply -f deployment-recreate.yaml

To see the recreate strategy in action, we will need a way to watch the changes that are happening to the Pods as we update the image version, for example.

You can open a second terminal window and use the -watch flag when listing all Pods - the -watch flag will keep the command running, and any changes to the Pods are written to the screen.

kubectl get pods --watch

From the first terminal window, let's update the Docker image from busybox to busybox:1.31.1 by running:

kubectl set image deployment hello hello-container=busybox:1.31.1

The second terminal window's output where we are watching the Pods should look like the one below. Note that I have added the line breaks between the groups.

NAME                     READY   STATUS    RESTARTS   AGE
hello-6fcbc8bc84-jpm64   1/1     Running   0          54m
hello-6fcbc8bc84-wsw6s   1/1     Running   0          54m
hello-6fcbc8bc84-wwpk2   1/1     Running   0          54m
hello-6fcbc8bc84-z2dqv   1/1     Running   0          54m
hello-6fcbc8bc84-zpc5q   1/1     Running   0          54m

hello-6fcbc8bc84-z2dqv   1/1     Terminating   0          56m
hello-6fcbc8bc84-wwpk2   1/1     Terminating   0          56m
hello-6fcbc8bc84-wsw6s   1/1     Terminating   0          56m
hello-6fcbc8bc84-jpm64   1/1     Terminating   0          56m
hello-6fcbc8bc84-zpc5q   1/1     Terminating   0          56m
hello-6fcbc8bc84-wsw6s   0/1     Terminating   0          56m
hello-6fcbc8bc84-z2dqv   0/1     Terminating   0          56m
hello-6fcbc8bc84-zpc5q   0/1     Terminating   0          56m
hello-6fcbc8bc84-jpm64   0/1     Terminating   0          56m
hello-6fcbc8bc84-wwpk2   0/1     Terminating   0          56m
hello-6fcbc8bc84-z2dqv   0/1     Terminating   0          56m

hello-84f59c54cd-77hpt   0/1     Pending       0          0s
hello-84f59c54cd-77hpt   0/1     Pending       0          0s
hello-84f59c54cd-9st7n   0/1     Pending       0          0s
hello-84f59c54cd-lxqrn   0/1     Pending       0          0s
hello-84f59c54cd-9st7n   0/1     Pending       0          0s
hello-84f59c54cd-lxqrn   0/1     Pending       0          0s
hello-84f59c54cd-z9s5s   0/1     Pending       0          0s
hello-84f59c54cd-8f2pt   0/1     Pending       0          0s
hello-84f59c54cd-77hpt   0/1     ContainerCreating   0          0s
hello-84f59c54cd-z9s5s   0/1     Pending             0          0s
hello-84f59c54cd-8f2pt   0/1     Pending             0          0s
hello-84f59c54cd-9st7n   0/1     ContainerCreating   0          1s
hello-84f59c54cd-lxqrn   0/1     ContainerCreating   0          1s
hello-84f59c54cd-z9s5s   0/1     ContainerCreating   0          1s
hello-84f59c54cd-8f2pt   0/1     ContainerCreating   0          1s
hello-84f59c54cd-77hpt   1/1     Running             0          3s
hello-84f59c54cd-lxqrn   1/1     Running             0          4s
hello-84f59c54cd-9st7n   1/1     Running             0          5s
hello-84f59c54cd-8f2pt   1/1     Running             0          6s
hello-84f59c54cd-z9s5s   1/1     Running             0          7s

The first couple of lines show all five Pods running. Right at the first empty line above (I added that for clarity), we ran the set image command. The controller terminated all Pods first. Once they were terminated (second empty line in the output above), the controller created the new Pods.

The apparent downside of this strategy is that once controller terminates old Pods and starts up the new ones, there are no running Pods to handle any traffic, which means that there will be downtime. Make sure you delete the deployment kubectl delete deploy hello before continuing. You can also press CTRL+C to stop running the -watch command from the second terminal window (keep the window open as we will use it again shortly).

Delete the deployment using kubectl delete deploy hello.