CodeBlanks Logo Icon CodeBlanks

kubectl get

Learn to list resources with kubectl get.

kubectl get

kubectl get lists objects as a table. The output format changes with -o:

kubectl get pods                    # table
kubectl get pods -o wide            # IP and node columns
kubectl get pod web-abc -o yaml     # full definition
kubectl get pods -o name            # in the form pod/web-abc
kubectl get deploy,svc              # several types

-o yaml also shows the fields Kubernetes added to the object (status, uid, timestamps).

JSONPath and custom columns

For scripts, a single field is taken with -o jsonpath, and readable tables with -o custom-columns:

$ kubectl get pods -o jsonpath='{.items[*].metadata.name}'
web-abc web-def
$ kubectl get pods -o custom-columns=AD:.metadata.name,NODE:.spec.nodeName
AD        NODE
web-abc   worker-1
web-def   worker-2

Practice Select the fields

Write two commands: list the deployments and services in one command; then print the names of all pods with jsonpath='{.items[*].metadata.name}'.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl get deploy,svc
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

Expected output

NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/web   2/2     2            2           1h
NAME                 TYPE        CLUSTER-IP     PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1      443/TCP   2d
web-7c5b9f6d8-abcde web-7c5b9f6d8-fghij

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which shows an object's full YAML definition in the cluster?

  • kubectl show TYPE NAME
  • kubectl get TYPE NAME -o yaml Correct answer
  • kubectl yaml TYPE NAME
  • kubectl get -yaml

Explanation: -o json gives the same information as JSON.

2 Get the pod's full definition as YAML.

Question
kubectl get pod web-abc -o ____
Answer
kubectl get pod web-abc -o yaml

Explanation: -o yaml gives the whole object.

3 Match the formats.

Term Meaning
-o wide Extra columns
-o yaml Full definition
-o name type/name list
-o jsonpath Single field selection

All lessons in Intermediate