k8s Cheatsheet

Force a Redeployment

If you want to recycle pods in a deployment without deleting the pods (Source)

kubectl patch deployment your_deployment \
-p "{\"spec\": {\"template\": {\"metadata\": { \"labels\": {  \"redeploy\": \"$(date +%s)\"}}}}}"

Get ALL resources

Only gets pods/deployments/services

kubectl get all <-n namespace>

Great hint from this kubectl issue on leveraging kubectl api-resources to get all resources within a namespace

kubectl api-resources --verbs=list --namespaced -o name \
  | xargs -n 1 kubectl get --show-kind --ignore-not-found -l <label>=<value> -n <namespace>

Job from Cronjob

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

Custom columns

Super useful for a LOT of quick information. For example, to display pods and their current nodes:

kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName

(PS, the above info is also provided in kubectl get pods -o wide, but above still useful if you are ONLY looking for nodenames)

And to display ingresses with hostname and path

kubectl get ingress -o custom-columns=NAME:.metadata.name,HOST:.spec.rules[0].host,PATH:.spec.rules[0].http.paths[0].path

Testing Ingress

Sometimes it’s necessary to test an Ingress leveraging routing rules based on hostname. Several methods to test this exist, depending on the specific situation:

Source: https://daniel.haxx.se/blog/2018/04/05/curl-another-host/

Network Troubleshooting

     -  name: netshoot
        - /bin/sh
        - -c
        - sleep 10000
        image: nicolaka/netshoot
kubectl exec -it -c netshoot <podname>
# Capture traffic until user hits Ctrl+C
tcpdump -w nodeport.pcap port 80
# Dump out enough info to read HTTP headers
tcpdump -vvvs 1024 -r nodeport.pcap



General K8S/K8S Engineering Articles

I don’t necessarily endorse these views, but also agree that elegant engineering should be prized over complexity.


Steve Miller BY-NC 4.0 | Rendered by Hugo | Subscribe