nerdexam
Linux_Foundation

CKA · Question #6

Create a file: /opt/KUCC00302/kucc00302.txt that lists all pods that implement service baz in namespace development. The format of the file should be one pod name per line.

The task requires using kubectl to identify pods associated with a specific Kubernetes service by its selector, extract their names, and write them one per line to a designated file.

Submitted by obi.ng· May 4, 2026Services and Networking

Question

Create a file: /opt/KUCC00302/kucc00302.txt that lists all pods that implement service baz in namespace development. The format of the file should be one pod name per line.

Exhibits

CKA question #6 exhibit 1
CKA question #6 exhibit 2

Explanation

The task requires using kubectl to identify pods associated with a specific Kubernetes service by its selector, extract their names, and write them one per line to a designated file.

Approach. The correct approach involves a sequence of kubectl commands to achieve the desired outcome:

  1. Identify the service's pod selector: Pods are linked to services via label selectors. The first step is to query the baz service in the development namespace to find its spec.selector. This is done using: kubectl get service baz -n development -o jsonpath='{.spec.selector}'. For example, this might output {"app":"baz"}.
  2. List pods based on the selector: Once the selector (e.g., app=baz) is identified, use it to filter pods. The command kubectl get pods -n development -l app=baz will list all pods matching the selector in the specified namespace.
  3. Format output to pod names only, one per line: To extract just the pod names and ensure each is on a new line, the -o jsonpath output option is used with a Go template. The template '{range .items[*]}{.metadata.name}{"\n"}{end}' iterates through each pod item (.items[*]) and prints its metadata.name followed by a newline character ({"\n"}).
  4. Redirect output to the specified file: Finally, the output of the formatted kubectl command is redirected to /opt/KUCC00302/kucc00302.txt using the shell redirection operator >.

The complete set of commands (assuming the service selector is app: baz):

kubectl get service baz -n development -o jsonpath='{.spec.selector}'
# (Examine output, e.g., {"app":"baz"})
kubectl get pods -n development -l app=baz -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' > /opt/KUCC00302/kucc00302.txt

Common mistakes.

  • common_mistake. Common mistakes include:
  • Omitting or incorrect namespace: Failing to include -n development or specifying the wrong namespace will result in looking for pods in the default namespace or an incorrect one.
  • Incorrectly identifying the service's selector: Guessing the pod label selector (e.g., assuming app=baz without verifying) instead of dynamically querying the service definition. This can lead to filtering the wrong pods or no pods if the actual selector is different (e.g., component=baz, run=baz).
  • Incorrect output formatting: Not using -o jsonpath or -o custom-columns correctly, which would produce full YAML/JSON output instead of just pod names, or failing to ensure each name is on a separate line (e.g., forgetting {"\n"} in the jsonpath template).
  • Incorrect label syntax: Using app:baz instead of app=baz with the -l flag.
  • Incorrect file path or name: Any typo in /opt/KUCC00302/kucc00302.txt will prevent the file from being created or being found in the correct location for validation.

Concept tested. The core concepts tested are Kubernetes resource querying (kubectl get), filtering resources by namespace (-n), filtering resources by labels (-l), understanding service selectors and how they relate to pods, formatting kubectl command output (-o jsonpath or -o custom-columns) to extract specific data, and basic Linux shell file redirection (>).

Topics

#Kubernetes Services#Pod Selection#kubectl Output Filtering#Namespaces

Community Discussion

No community discussion yet for this question.

Full CKA Practice