Skip to content

Instantly share code, notes, and snippets.

@pshanoop
Last active July 18, 2023 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pshanoop/6efce63e688593a848c905b62fadd07f to your computer and use it in GitHub Desktop.
Save pshanoop/6efce63e688593a848c905b62fadd07f to your computer and use it in GitHub Desktop.
kubectl-cheatsheet.md

DNS performance test

https://github.com/guessi/kubernetes-dnsperf/tree/master

Get all pods with ContainerId

 kubectl  get pods -o custom-columns=Name:metadata.name,DockerID:.status.containerStatuses[*].containerID

Get pod by ContainerID

kubectl get pod -o jsonpath='{range .items[?(@.status.containerStatuses[].containerID=="containerd://8c17358eeadf13eb419a919d445f18b5a5060c23535b6aeab77d694422c2728f")]}{.metadata.name}, {.metadata.namespace}{end}' -A

List pods Sorted by Restart Count

kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

Get all worker nodes (use a selector to exclude

results that have a label
# named 'node-role.kubernetes.io/control-plane')
kubectl get node --selector='!node-role.kubernetes.io/control-plane'

Get all running pods in the namespace

kubectl get pods --field-selector=status.phase=Running

Check which nodes are ready

JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
 && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"

Output decoded secrets without external tools

kubectl get secret my-secret -o go-template='{{range $k,$v := .data}}{{"### "}}{{$k}}{{"\n"}}{{$v|base64decode}}{{"\n\n"}}{{end}}'

List all Secrets currently in use by a pod

kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq

List all warning events

kubectl events --types=Warning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment