Skip to content

Instantly share code, notes, and snippets.

@PabloHiro
Last active September 15, 2023 07:32
Show Gist options
  • Save PabloHiro/e2820730b9398081d9eb41f3430f3dd7 to your computer and use it in GitHub Desktop.
Save PabloHiro/e2820730b9398081d9eb41f3430f3dd7 to your computer and use it in GitHub Desktop.
Kubectl tips and tricks

Troubleshooting

kubectl run -it --rm --image=wbitt/network-multitool network-multitool -- sh

Top of pods in a node

kubectl get pods --all-namespaces -o wide | grep NODE_NAME | awk '{print $1" "$2}' | xargs -n2 kubectl top pods --no-headers --namespace | sort -t ' ' --key 2 --numeric --reverse

List pods resources requests

kubectl get pod -A -o custom-columns="Name:metadata.name,CPU-request:spec.containers[*].resources.requests.cpu,CPU-limit:spec.containers[*].resources.limits.cpu,Memory-request:spec.containers[*].resources.requests.memory,Memory-limits:spec.containers[*].resources.limits.memory"

List per node resources requests and limits

kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo {}; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- ; echo'

Get all images in a cluster

kubectl get pods --all-namespaces -o custom-columns='NAME:metadata.name,STATUS:status.phase,IMAGE:spec.containers[*].image'

Get relationships between pods and volumes

kubectl get pods --all-namespaces -o=json | jq -c '.items[] | {name: .metadata.name, namespace: .metadata.namespace, claimName: .spec | select( has ("volumes") ).volumes[] | select( has ("persistentVolumeClaim") ).persistentVolumeClaim.claimName }'

List secrets used as mounts

kubectl get pods --all-namespaces -o jsonpath='{.items[*].spec.volumes[*].secret.secretName}' | xargs -n1 | uniq

List secrets used as environment variables

kubectl get pods --all-namespaces -o jsonpath='{.items[*].spec.containers[*].env[*].valueFrom.secretKeyRef.name}' | xargs -n1 | uniq

Decode secret

kubectl get secret secretname -o go-template='{{range $k,$v := .data}}{{printf "%s: " $k}}{{if not $v}}{{$v}}{{else}}{{$v | base64decode}}{{end}}{{"\n"}}{{end}}'

Delete completed pods

kubectl delete pod --field-selector=status.phase==Succeeded -A

Delete failed pods

kubectl delete pod --field-selector=status.phase==Failed -A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment