kubernetes one-liner collection

Combining kubectl and scripting for a useful output.

List containers in pod

The shell variable ns needs to be set before the command is executed.
kubectl get pod -n $ns the-pod-name-656b97fd4d-tcbhx -o jsonpath='{.spec.containers[*].name}';echo ""

Same as above but different tools:
kubectl get pod -n $ns the-pod-name-656b97fd4d-tcbhx -o json | jq '.spec.containers[].name'

Show containers and their status details

The shell variable ns needs to be set before the command is executed.
kubectl describe pod -n $ns the-pod-name-656b97fd4d-tcbhx | awk '/Containers:/,/Volumes:/{if (/^[A-Z]|^ [A-Za-z]/){print};if(/State:/){p=1};if(p){print};if(/Restart Count:/){p=0}}' This produces something like this: Containers: logtransformer: State: Running Started: Thu, 11 Jul 2024 17:37:18 +0000 Last State: Terminated Reason: Error Exit Code: 9 Started: Wed, 10 Jul 2024 15:33:28 +0000 Finished: Thu, 11 Jul 2024 17:37:17 +0000 Ready: False Restart Count: 1 metrics: State: Running Started: Wed, 10 Jul 2024 15:33:30 +0000 Ready: True Restart Count: 0 tlsproxy: State: Running Started: Wed, 10 Jul 2024 15:33:31 +0000 Ready: True Restart Count: 0 Conditions: Type Status Initialized True Ready False ContainersReady False PodScheduled True Volumes:

Show pods in trouble

Filter by state:
kubectl get pod -o wide | grep -vE 'Running|STATUS|Completed'
Find pods with a missing container:
kubectl -n $ns get pod | grep -v Completed | awk -F"[ /]+" 'BEGIN{found=0} !/NAME/ {if ($2!=$3) { found=1; print $0}} END { if (!found) print "All containers are up"}'



© Guido Socher, Please enable Javascript to see email address
License: CC BY 4.0, http://creativecommons.org/licenses/by/4.0/