Browse topics

kubectl get and describe

Version
1.37
Review
Draft

Summary#

kubectl get lists objects and can print them as tables, YAML, JSON or selected fields. kubectl describe gives a readable report of an object, including related information such as events.

Syntax#

sh
kubectl get TYPE [NAME] -o wide
kubectl get TYPE NAME -o yaml
kubectl get TYPE -o jsonpath='{.items[*].metadata.name}'
kubectl get TYPE -o custom-columns='NAME:.metadata.name'
kubectl get pods --field-selector=status.phase=Running
kubectl describe TYPE NAME

Example#

sh
(
  set -eu
  ns="ref-get-$$"
  kubectl create namespace "$ns" >/dev/null
  trap 'kubectl delete namespace "$ns" >/dev/null' EXIT

  kubectl create deployment -n "$ns" web --image=registry.k8s.io/pause:3.10 --replicas=2 >/dev/null
  kubectl rollout status -n "$ns" deployment/web --timeout=120s >/dev/null

  kubectl get -n "$ns" deployment web -o name
  kubectl get -n "$ns" deployment web \
    -o custom-columns='NAME:.metadata.name,READY:.status.readyReplicas,IMAGE:.spec.template.spec.containers[0].image'
  kubectl get -n "$ns" pods -l app=web --field-selector=status.phase=Running \
    -o jsonpath='{range .items[*]}{.status.phase} {.spec.containers[0].name}{"\n"}{end}'
  kubectl describe -n "$ns" deployment web | grep -E '^(Name|Selector|Replicas|StrategyType):'
)

Expected output#

Text
deployment.apps/web
NAME   READY   IMAGE
web    2       registry.k8s.io/pause:3.10
Running pause
Running pause
Name:                   web
Selector:               app=web
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate

The last command keeps four stable lines; the full report also has timestamps and events that differ between runs.

Common mistakes#

Do not parse describe output or the default table in scripts. Both are meant for people. Select fields with -o jsonpath, -o json or -o custom-columns. kubectl's JSONPath does not support regular expressions.

Version applicability#

The commands target Kubernetes 1.37. kubectl is supported within one minor version, older or newer, of the API server.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0