Tutorial: Learn How To Create A Deployment With Kubernetes Replicas Now! 🤔

The fifth question requires the use of Kubernetes (k8s) replicas in a deployment and is part of the series on mock CKAD Sample Questions.

Kubernetes replicas can help achieve high availability and scalability in our applications and may appear in the CKAD exam.

The following is taken from the Practice Exam for Certified Kubernetes Application Developer (CKAD) Certification on Matthew Palmer’s website and reads 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] “[c]onfigure 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.

Explore more k8s microservices tips and guides in the dedicated category page as well as the k8s practice questions category page for other articles that can be useful when learning about Kubernetes.

This article was last updated on June 17, 2025.

Want to restart a pod in k8s the right way?

There’s no built-in kubectl restart pod command, but there are alternative methods for restarting pods in this tutorial.

How to configure Kubernetes Deployment Replicas explained

In this section we’ll review the steps required to answer the practice CKAD question which is the focus of this article.

We’ll also review our work as well as take a look at a potential solution and explain why it is incorrect.

Article Prerequisites

Refer to the article prerequisites for details about technology and setup instructions required to answer this question.

Step One: Develop the Kubernetes deployment configuration with replicas

Our k8s complete deployment configuration example file can be seen here (see: question-five-deployment.yaml on Github).

We need top copy this YAML into a file on our local machine.

Once we’ve saved this configuration locally, we can then move onto the next step.

CTO Consulting Services

As a dedicated CTO Consultant, I provide strategies that will enhance your software development effort as well as help your software engineers deliver better products.
				
					apiVersion: apps/v1
kind: Deployment
metadata:
  name: question-5
  namespace: ggckad-s5
  labels:
    client: user
spec:
  replicas: 6
  strategy:
    type: Recreate
  selector:
    matchLabels: {
      app: revproxy
    }
  template:
    metadata: {
      labels: {
        app: revproxy
      }
    }
    spec:
      containers:
      - name: web
        #
        # We start with nginx:1.7.9 but will cause a rolling update when we change this
        # to latest.
        #
        image: nginx:1.7.9
        ports:
          - name: web
            containerPort: 80
            protocol: TCP
				
			

Let’s dissect the instructions and match them to each line in this configuration.

The first line of the instructions reads:

All operations in this question should be performed in the ggckad-s5 namespace.

— the namespace is declared on line #5.

Create a file called question-5.yaml that declares a deployment in the ggckad-s5 namespace…

The file is named question-5.yaml and the deployment is declared on line #2; the ggckad-s5 namespace is declared on line #5.

…with six replicas running the nginx:1.7.9 image.

The six replicas are declared on line #9 and the nginx:1.7.9 image is declared on line #29.

Each pod should have the label app=revproxy.

The revproxy metadata label is applied to each pod on line #19.

The deployment should have the label client=user.

The client is set to user on line #7.

Configure the deployment so that when the deployment is updated, the existing pods are killed off before new pods are created to replace them.

Finally, the recreate strategy on line #11 means that whenever an update to the Deployment is made, k8s will first terminate all existing pods and then create new ones with the updated configuration.

Whereas the RollingUpdate strategy updates pods incrementally the Recreate strategy stops all pods before starting new ones, potentially causing a brief downtime during the update.

Step Two: Create the ggckad-s5 namespace.

In this step we’ll create the ggckad-s5 namespace.

I’ve included the command to do this along with an example regarding what a successful execution of this command below.

				
					kubectl create namespace ggckad-s5
				
			
Command line output which shows the 'kubectl create namespace ggckad-s5' and the message indicating that the namespace was created successfully as follows: 'namespace/ggckad-s5 created'.
Step Two: Create the ggckad-s5 namespace using the 'kubectl create namespace ggckad-s5' command.

Step Three: Apply the configuration.

The command to use in order the apply the configuration file appears below along with a successful execution below.

				
					k apply -f ./question-5.yaml
				
			
Command line output after running 'kubectl apply -f ./question-5.yaml' to apply the deployment file with Kubernetes replicas -- we can see the output indicates success with 'deployment.apps/question-5 created'.
Apply the question-5.yaml deployment file which will create the Kubernetes replicas.

Finally, we need to verify our work.

Step Four (optional): Verify our solution by checking the rollout status.

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:

Terminal shell with question five yaml file being applied and then a list of Kubernetes deployments and Kubernetes pods in the ggckad-s5 namespace.
Terminal output after applying the question-5 configuration where we can see various labels in the ggckad-s5 Kubernetes namespace for both deployments and pods.

As per the instructions, 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 the old containers are terminated and then new ones are created — the following image demonstrates this behavior:

Example output for question five where the question-5.yaml is applied and we can see the pods are running in the ggckad-s5 namespace.
Terminal output after applying a deployment with a recreate strategy.

Review the rollout status

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
				
			

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

Example output for question five where a Kubernetes namespace is created and then the the question-5.yaml is applied and we can see that the deployment has been successfully rolled out.
Checking the rollout status of a deployment that has been updated.

Lastly we can reproduce the same behavior by executing the following command:

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

and 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 using the RollingUpdate deployment strategy

The same example but with a RollingUpdate deployment strategy would be incorrect.

We include the output of this example below for comparison purposes.

Terminal output for "k get pods -- show-labels --namespace ggckad-s5" executed several times and showing the pod status changing (Running, ContainerCreating, Pending, Terminating) as the pods are reconfigured. Eventually all pods are in the Running state.
Terminal output demonstrating various labels for pods running in the ggckad-s5 k8s namespace; the status is also changing as all pods are updated.

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
				
			

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

Shell output after applying the question-5.yaml and checking the rollout status, in which the "question-5" deployment has been successfully rolled out.
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!

Tutorial Conclusion

I hope this instructional has been helpful in understanding how to add k8s replicas to a deployment.

See Also

Other tutorials focusing on various aspects of k8s can be found below.

  1. Learn how to use kubectl to get the cluster name in this guide.
  2. Tutorial: Learn how to quickly install the Metrics Server in minikube in five easy steps!
  3. Learn how to mount a local directory in minikube by exploring three possible solutions.
  4. Example One: Learn how to create a kubernetes pod in a namespace.
  5. Example Two: Learn how to create a ConfigMap in Kubernetes with two environment variables.
  6. Example Three: Set user IDs and file system group IDs in containers using the Kubernetes SecurityContext.
  7. Example Four: Learn how to configure Kubernetes health check probes.
  8. This tutorial explains how to set up and run SQL Server for Mac, providing step-by-step guidance to get Microsoft SQL Server working on a MacOS environment using Docker.

Frequently Asked Questions (FAQ)

What are replicas in Kubernetes?

Kubernetes replicas refer to the multiple instances of a pod that are created in order to ensure high availability and reliability of an application.

Kubernetes replicas help to ensure that if one instance of a pod fails, others remain active to handle user requests thereby minimizing downtime.

K8s replicas are managed using a ReplicaSet or Deployment, which automatically adjusts the number of running pod instances to match the desired state.

This approach helps maintain consistency and stability in an application’s performance, even during unexpected failures or increased demand.

author avatar
ThosPFuller
I am a software engineer based in Northern Virginia (USA) and this website focuses on content engineering, web development, Technical SEO, Search Engine Optimization (SEO).

Leave a Reply