5 Answers to Kubernetes CKAD Practice Questions
By: Thomas P. Fuller
Tags: DevOps, Kubernetes, Programming, K8s, Containers

Question 5.

The fifth question from Palmer’s practice exam [1] is as follows:

“All operations in this question should be performed in the ggckad-s5 namespace. Create a file called question-5.yaml that declares a deployment in the ggckad-s5 namespace, with six replicas running the nginx:1.7.9 image. Each pod should have the label app=revproxy . The deployment should have the label client=user . Configure the deployment so that when the deployment is updated, the existing pods are killed off before new pods are created to replace them.”

We need to check the labels for both the deployment and pods, and then we need to, as per [1] “configure the deployment so that when the deployment is updated, the existing pods are killed off before new pods are created to replace them.”

We have two deployment strategies available: RollingUpdate (the default), and Recreate [14]. Given the definitions for RollingUpdate and Recreate , in this case, we do not want to use the default and instead need to assign Recreate to the .spec.strategy.type .

Our deployment configuration can be seen here:

We can apply the file the same as we did in the previous question:

k apply -f ./question-5.yaml

Finally, we need to verify our work.

Verifying our solution

We need to check the labels for both the deployment and pods, and we also need to ensure that the requirement is met that when the deployment is updated, existing pods are killed off before new pods are created to replace them.

We can use the following command to view the deployment labels:

k get deployments --show-labels --namespace ggckad-s5

And we can use the following command to view the various pod labels:

k get pods --show-labels --namespace ggckad-s5

Below we can see the output of applying the question-5.yaml file and then executing these two commands. The yellow arrows show that the labels have been set as per the specification:

Note that our deployment relies on the nginx:1.7.9 image. This image is old, so if we update this to nginx:latest and apply the question-5.yaml file again, then we see that new containers are deployed while the old containers are gradually terminated. The following image demonstrates this behavior:

Applying a deployment with a recreate strategy

Next, we repeat the same exercise, only here we check the rollout status using the following command [11]:

kubectl rollout status deployment.v1.apps/question-5 --namespace ggckad-s5

We can see from a different angle that the old replicas are being terminated while the new ones are starting.

Last, we can reproduce the same behavior by executing the following command:

kubectl rollout restart deployment question-5 --namespace ggckad-s5

In this case, we don’t even need to update the nginx version in the question-5.yaml file .

Let’s take a look at an update when the deployment strategy type is set to RollingUpdate . This is the default, and it is also the incorrect choice given the specification.

An incorrect solution

The same example but with a RollingUpdate deployment strategy would be incorrect. We include the output of this example below for comparison purposes.

Next, we repeat the same exercise, only here we check the rollout status using the following command [11]:

kubectl rollout status deployment.v1.apps/question-5 --namespace ggckad-s5

We can see from a different angle that the old replicas are being terminated while the new ones are starting.

The rollout status when updating Nginx from 1.7.9 to latest

And that’s it for this question, and for this article for that matter — on to the conclusion!