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.
Question
Exhibits
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:
- Identify the service's pod selector: Pods are linked to services via label selectors. The first step is to query the
bazservice in thedevelopmentnamespace to find itsspec.selector. This is done using:kubectl get service baz -n development -o jsonpath='{.spec.selector}'. For example, this might output{"app":"baz"}. - List pods based on the selector: Once the selector (e.g.,
app=baz) is identified, use it to filter pods. The commandkubectl get pods -n development -l app=bazwill list all pods matching the selector in the specified namespace. - 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 jsonpathoutput option is used with a Go template. The template'{range .items[*]}{.metadata.name}{"\n"}{end}'iterates through each pod item (.items[*]) and prints itsmetadata.namefollowed by a newline character ({"\n"}). - Redirect output to the specified file: Finally, the output of the formatted
kubectlcommand is redirected to/opt/KUCC00302/kucc00302.txtusing 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 developmentor 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=bazwithout 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 jsonpathor-o custom-columnscorrectly, 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:bazinstead ofapp=bazwith the-lflag. - Incorrect file path or name: Any typo in
/opt/KUCC00302/kucc00302.txtwill 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
Community Discussion
No community discussion yet for this question.

