CKA · Question #28
Print pod name and start time to "/opt/pod-status" file
The task requires using kubectl with custom output columns to extract pod names and their start times, then redirecting this output to a specified file.
Question
Exhibits
Explanation
The task requires using kubectl with custom output columns to extract pod names and their start times, then redirecting this output to a specified file.
Approach. The correct interaction is to type a specific kubectl command into the terminal that retrieves the pod names and their exact start times, and then redirects this output to the /opt/pod-status file. The kubectl get pods command needs to be augmented with an output format that explicitly provides the name and startTime fields from the pod's metadata and status.
Here's the command to enter:
kubectl get pods -o custom-columns=NAME:.metadata.name,START_TIME:.status.startTime > /opt/pod-status
Explanation:
kubectl get pods: This is the standard command to list all pods in the current namespace.-o custom-columns=...: This flag allows specifying custom output columns. We define two columns:NAME:.metadata.name: This creates a column named 'NAME' and populates it with the pod's name, found at.metadata.namein the pod object structure.START_TIME:.status.startTime: This creates a column named 'START_TIME' and populates it with the pod's exact start timestamp, found at.status.startTimein the pod object structure. TheAGEcolumn shown in the default output is a relative duration, not the absolute start time.
> /opt/pod-status: This is a standard shell redirection operator that sends the entire output of thekubectlcommand to the specified file,/opt/pod-status.
Common mistakes.
- common_mistake. Several common mistakes could lead to an incorrect answer:
- Using default
kubectl get podsoutput: The default output only shows 'AGE' (e.g., '5h51m'), not the precise 'START_TIME' (e.g., '2023-10-27T10:00:00Z') which is required by the question. Simply runningkubectl get pods > /opt/pod-statuswould be incorrect. - Incorrect
jsonpathorcustom-columnssyntax: Errors in specifying the JSONPath (e.g.,NAME:.name,START_TIME:.startTimewithout the correct parent pathsmetadataandstatus) would result in missing or incorrect data. - Forgetting output redirection: If the command is run without
> /opt/pod-status, the output will just be printed to the terminal, failing to fulfill the requirement of writing to the file. - Attempting to parse
kubectl describe podoutput: Whilekubectl describe pod <pod-name>contains the start time, it also provides a lot of other information and would require complex text processing (e.g.,grep,awk) for each pod, which is inefficient and generally not the intended solution for such questions.
Concept tested. This question tests the candidate's knowledge of Kubernetes pod management, specifically using the kubectl command-line tool to retrieve detailed information about pods. Key concepts include:
kubectl getcommand: Basic usage for listing resources.- Output formatting: Using advanced output options like
custom-columns(orjsonpath/go-template) to extract specific fields from Kubernetes object manifests. - Pod object structure: Understanding where
metadata.nameandstatus.startTimeare located within a Pod's API object. - Shell redirection: Using
>to direct command output to a file.
Topics
Community Discussion
No community discussion yet for this question.

