CKA · Question #21
Get list of all the pods showing name and namespace with a jsonpath expression.
This question tests your ability to use kubectl with a JSONPath output expression to extract specific fields (name and namespace) from all pod resources in a Kubernetes cluster. It is a core CKA skill for querying cluster state programmatically.
Question
Explanation
This question tests your ability to use kubectl with a JSONPath output expression to extract specific fields (name and namespace) from all pod resources in a Kubernetes cluster. It is a core CKA skill for querying cluster state programmatically.
Approach. The correct command is: kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.metadata.namespace}{"\n"}{end}'. The --all-namespaces (or -A) flag ensures pods from every namespace are included. The -o jsonpath flag enables JSONPath templating against the returned JSON object. .items[*] iterates over every pod in the list, and .metadata.name / .metadata.namespace extract the relevant fields from each pod's metadata block. The {"\t"} and {"\n"} literals add tab separation and newlines for readable output.
Concept tested. kubectl JSONPath output formatting (-o jsonpath) combined with multi-namespace pod listing (--all-namespaces). Tests knowledge of the Kubernetes API object structure (.items[*].metadata.name / .metadata.namespace) and JSONPath range iteration syntax.
Reference. https://kubernetes.io/docs/reference/kubectl/jsonpath/
Topics
Community Discussion
No community discussion yet for this question.