Using commands like oc get pods
or kubectl get pods
returns a list of pods with lots of extra information, including pods that have completed or in an error state, plus other information. For example:
# Get a list of all running pods in the current namespace (Kubernetes)
kubectl get pods
# Get a list of all running pods in the current namespace (OpenShift)
oc get pods
NAME READY STATUS RESTARTS AGE
apply-route-restriction-28067995-h4dxn 0/3 Completed 0 6m21s
apply-route-restriction-28067995-jdmwf 0/3 Completed 0 11m
apply-route-restriction-28068000-ktpg2 1/3 ErrImagePull 0 81s
apply-route-restriction-28068000-q7vmp 0/3 Completed 0 6m22s
apply-route-restriction-28068005-tr69x 0/3 Completed 0 82s
ste4site-db-ha-5gvs-0 4/4 Running 1 (10d ago) 25d
ste4site-db-ha-wfsc-0 4/4 Running 0 25d
ste4site-repo1-incr-28067280-26fdv 0/1 Completed 0 12h
ste4stie-repo1-incr-28067760-bdcqm 0/1 Completed 0 4h6m
ste4site-repo1-incr-28068000-82kw6 0/1 Completed 0 6m22s
ste4site-web-app-dev-25-7jwbw 1/1 Running 0 29d
ste4site-web-svc-dev-40-trd9h 1/1 Running 0 5d17h
ste4site-web-svc-dev-41-deploy 0/1 Error 0 5d18h
This is great if you want an overview of the namespace state, but if you’re automating a task or prefer a cleaner look, you can reduce the amount of information in the pod list to something more readable and easier to use in a script.
Rather than filtering the output with grep
, sed
, or awk
, there are command-line options in oc
and kubectl
to change the default output.
To see only running pods, use the option --field-selector=status.phase==Running
. For example:
oc get pods --field-selector=status.phase==Running
To get a list of pods without the column headers, use --no-headers
. or -o name
. For example:
oc get pods --no-headers
To get a list of pods with only the pod names, use -o custom-columns=":metadata.name"
. For example:
oc get pods -o custom-columns=":metadata.name"
Combine them into one command like this:
oc get pods --no-headers -o custom-columns=":metadata.name" --field-selector=status.phase==Running