nerdexam
Linux_Foundation

CKA · Question #26

Create 2 nginx image pods in which one of them is labelled with env=prod and another one labelled with env=dev and verify the same.

The test-taker must create two distinct Nginx Kubernetes Pods, one labeled 'env=prod' and the other 'env=dev', by modifying a generated YAML file twice and then applying it, finally verifying their creation and labels.

Submitted by anna_se· May 4, 2026Workloads and Scheduling

Question

Create 2 nginx image pods in which one of them is labelled with env=prod and another one labelled with env=dev and verify the same.

Exhibit

CKA question #26 exhibit

Explanation

The test-taker must create two distinct Nginx Kubernetes Pods, one labeled 'env=prod' and the other 'env=dev', by modifying a generated YAML file twice and then applying it, finally verifying their creation and labels.

Approach. The correct interaction involves the following steps:

  1. Modify YAML for the 'prod' pod: The kubectl run command has already created a basic nginx.yaml file. The test-taker must first open this file using vim nginx.yaml.
    • Inside vim, change the pod's name from nginx to a unique name, e.g., nginx-prod (under metadata.name).
    • Add the env: prod label under the metadata.labels section. The labels section should typically look like:
      metadata:
        labels:
          run: nginx
          env: prod
        name: nginx-prod
      
    • Save the changes and exit vim using :wq.
  2. Create the 'prod' pod: Execute kubectl create -f nginx.yaml to deploy the Nginx pod with the 'env=prod' label.
  3. Modify YAML for the 'dev' pod: Re-open nginx.yaml using vim nginx.yaml.
    • Inside vim, change the pod's name again to another unique name, e.g., nginx-dev.
    • Change the env label from prod to dev.
      metadata:
        labels:
          run: nginx
          env: dev
        name: nginx-dev
      
    • Save the changes and exit vim using :wq.
  4. Create the 'dev' pod: Execute kubectl create -f nginx.yaml to deploy the Nginx pod with the 'env=dev' label.
  5. Verify: Finally, execute kubectl get pods --show-labels to list all running pods along with their labels. This command will confirm that both nginx-prod (with env=prod) and nginx-dev (with env=dev) pods are created and running as specified.

Common mistakes.

  • common_mistake. 1. Not changing the pod name: A common mistake is not changing the metadata.name field when creating the second pod. Kubernetes Pod names must be unique within a namespace. If the same nginx name is used for both pods, the second kubectl create -f nginx.yaml command will fail with an 'AlreadyExists' error.
  1. Incorrect YAML syntax or indentation: Misplacing the labels section or using incorrect YAML indentation can lead to syntax errors when kubectl create attempts to parse the manifest, preventing pod creation.
  2. Attempting to define two pods in a single YAML file without proper structure: A single Pod manifest defines one Pod. While a list of objects can be defined in YAML, for distinct pods with different labels, modifying and applying the YAML twice or using two separate files is the most straightforward approach. Incorrectly adding multiple pod definitions in a single file or using a Deployment (which typically creates replicas with identical labels) would not directly meet the requirement of two distinct pods with different env labels without further complexity.
  3. Forgetting to verify: The question explicitly asks to 'verify the same'. Failing to run kubectl get pods --show-labels or a similar verification command (e.g., kubectl get po -l env=prod and kubectl get po -l env=dev) would result in an incomplete solution.

Concept tested. This question tests the candidate's ability to create and manage Kubernetes Pods using YAML manifests, specifically focusing on:

  • Understanding and applying labels to Kubernetes resources (metadata.labels).
  • Using kubectl run --dry-run to generate base YAML manifests.
  • Basic text editing skills with vim to modify YAML files.
  • Using kubectl create -f to deploy resources from YAML.
  • Understanding the requirement for unique resource names within a namespace.
  • Verifying resource creation and properties using kubectl get with options like --show-labels.

Topics

#Pod Creation#Kubernetes Labels#Workloads#kubectl

Community Discussion

No community discussion yet for this question.

Full CKA Practice