Kubernetes

A lot of the examples require jq to be installed

Kubernetes workloads

Keepalive but do nothing

command: ["/bin/bash", "-c", "--"]
args: ["while true; do sleep 30; done;"]

Managing Kubernetes with kubectl

Manually start a job from CronJob

kubectl create job --from=cronjob/<cronjob-name> <job-name>

Kill Terminating pods

kubectl get --all-namespaces pods |
awk '/Terminating/ { print $1 " " $2 }' |
xargs -I{} bash -c "kubectl delete pods -n {} --force --grace-period=0

Get all deployment with matching label

kubectl get deployment --all-namespaces -o json |
jq '.items[] | select (.metadata.labels["app.kubernetes.io/managed-by"] == "Helm") | { "name": .metadata.name, "namespace": .metadata.namespace }'

Get all pods with matching nodeSelector

kubectl get --all-namespaces pods -o json |
jq '.items[] | select(.spec.nodeSelector["cloud.google.com/gke-nodepool"] == "misc") | .metadata.name'

Get all pods with a label key matching .*app.*

kubectl get --all-namespaces pods -o json |
jq '.items[].metadata.labels | with_entries(select(.key | test(".*app.*")))'

Run a command on each pod

kubectl get pods -l colour=green -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' |
xargs -I{} kubectl -n somenamespace exec -it {} -c fpm date

Delete failed (eg. Evicted) pods

kubectl delete pods --field-selector status.phase==Failed

Get all pods on a node

kubectl get --all-namespaces pods -o json |
jq '.items[] | select(.spec.nodeName | contains("worker")) | .metadata.name'

Get all pods with IP

kubectl get --all-namespaces pods -o json | jq 'select(.items[].status.podIP == "10.10.10.10") | .items[].metadata.name'

Get all pods with host IP

kubectl get --all-namespaces pods -o json | jq 'select(.items[].status.hostIP == "10.10.10.10") | .items[].metadata.name'

Get non-ready pods

kubectl get pods pod-name -o json | jq '.status.containerStatuses[] | select(.ready == false)'

Get pods and the host they are scheduled on

kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName --all-namespaces

Get containers in a pod

kubectl get pods mypodname -o jsonpath={.spec.containers[*].name}

...or in multiple pods

kubectl pods -o jsonpath={.items[*].spec.containers[*].name}