Kubectl - my cheat sheet
Hello everyone, this is my first English article!
> Archived article from 2019, from the old nutellino.it: commands and versions may be out of date.
❤️ cli
Last updated 18/12/2019
Hello everyone, this is my first English article!
I will write down a list of my most-used kubectl commands as a reference to work with Kubernetes daily.
Nodes
Get kubernetes nodes
kubectl get nodes # simple
kubectl get nodes -o wide # more informationsDescribe nodes
kubectl describe node <nodename>Select nodes based on label
kubectl get nodes -l cluster=all # get all nodes with cluster=all label
kubectl get nodes --selector=cluster=all # same command as abovePods
Generate yaml for a single pod
kubectl run --generator=run-pod/v1 magicpod --image=nginx --requests=cpu=100m,memory=256Mi --port=80 --labels=ciccio=pasticcio --dry-run -o yamlRun a disposable interactive shell
kubectl run --generator=run-pod/v1 disposable --image=busybox:1.28 --rm -it -- /bin/shExpose pod
kubectl expose pod magicpod --name=magicpod-svc --port=80 --target-port=80Deployments
Edit deployment with vim and save change clause on rollout history
kubectl edit deployment my-deployment --recordGet deployment rollout history
kubectl rollout history deployment magicdeploymentCreate deployment with kubectl run (deprecated but useful)
kubectl run my-deployment --image=nginx:latest --labels="my=label" --limits="cpu=100m" --replicas=3Job
Create a job that runs sleep 10 (see that only changing --restart changes object created) (deprecated but useful)
kubectl run my-job --restart=OnFailure --image=busybox --command=true -- sleep 10CronJob
Create a cronjob that executes sleep 10 every minutes (deprecated but useful)
kubectl run my-scheduled-job --schedule="* * * * *" --restart=Never --image=busybox --command=true -- sleep 10Networking
Debug networking, ping a pod
kubectl run --generator=run-pod/v1 disposable --image=busybox:1.28 --rm -it -- ping 10-233-67-15.myepicnamespace.podDebug networking, resolve name
kubectl run --generator=run-pod/v1 disposable --image=busybox:1.28 -- sleep 3600 # creates a pod that runs for 1 hour
kubectl exec disposable -it -- nslookup 10-233-67-15.myepicnamespace.pod.cluster.localLogging
Get logs in follow mode from container inside pod starting from last 100 lines
kubectl logs -f wordpress-lemp-7fdcbf8bff-w5m7b --container nginx --tail=100Kubeadm
Get available tokens
kubeadm token listCreate new token
kubeadm token createRetrieve --discovery-token-ca-cert-hash
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
openssl dgst -sha256 -hex | sed 's/^.* //'to be continued! :)
$ cat ~/archivio/kubectl-my-cheat-sheet.md
WARNING: this file is from 2019.
I wrote it, but a me from a few years ago: it may no longer reflect what I think or what I can do.
Continue? [y/N]