k8s Cheatsheet
Force a Redeployment
If you want to recycle pods in a deployment
kubectl rollout restart deploy/<mydeployment>
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:
-
Adjust the host header
curl --header "Host: example.com" http://127.0.0.1/
-
Temporarily adjust curl’s name resolution (useful for setting teh SNI field correctly for SSL)
curl --resolve example.com:443:127.0.0.1 https://example.com/
-
Testing with a completely difference host/port mapping
curl --connect-to example.com:443:host-47.example.com:443 https://example.com/
Source: https://daniel.haxx.se/blog/2018/04/05/curl-another-host/
Network Troubleshooting
- Inject netshoot as a sidecar in a deployment (under
.spec.template.spec.containers
)
- name: netshoot
command:
- /bin/sh
- -c
- sleep 10000
image: nicolaka/netshoot
- Capturing traffic from sidecar
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
Debugging
Miscellaneous
General K8S/K8S Engineering Articles
I don’t necessarily endorse these views, but also agree that elegant engineering should be prized over complexity.