Kubectl - my cheat sheet

December 11, 2019 0 Comments Kubernetes, kubectl, cheatsheet

❤️ 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 informations  

Describe 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 above  

Pods

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 yaml  

Run a disposable interactive shell

kubectl run --generator=run-pod/v1 disposable --image=busybox:1.28 --rm -it -- /bin/sh  

Expose pod

kubectl expose pod magicpod --name=magicpod-svc --port=80 --target-port=80  

Deployments

Edit deployment with vim and save change clause on rollout history

kubectl edit deployment my-deployment --record  

Get deployment rollout history

kubectl rollout history deployment magicdeployment  

Create deployment with kubectl run (deprecated but useful)

kubectl run my-deployment --image=nginx:latest --labels="my=label" --limits="cpu=100m" --replicas=3  

Job

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 10  

CronJob

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 10  

Networking

Debug networking, ping a pod

kubectl run --generator=run-pod/v1 disposable --image=busybox:1.28 --rm -it -- ping 10-233-67-15.myepicnamespace.pod  

Debug 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.local  

Logging

Get logs in follow mode from container inside pod starting from last 100 lines

kubectl logs -f wordpress-lemp-7fdcbf8bff-w5m7b --container nginx --tail=100  

Kubeadm

Get available tokens

kubeadm token list  

Create new token

kubeadm token create  

Retrieve --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! :)

Samuele Chiocca
Padova, italy Website