Below is a Kubernetes cheat sheet, which lists various useful commands that can be used with kubectl command-line interface to manage Kubernetes clusters. These commands cover a range of tasks, such as creating and managing deployments, pods, and services, querying resource usage, deleting resources, and more. Examples include running tests using temporary pods, checking node and pod resource usage, and deleting resources by labels. Additionally, the cheat sheet also provides tips on how to enable shell autocompletion for kubectl, and how to open a bash terminal in a pod.
kubectl run --rm mytest --image=yauritux/busybox-curl -itRun wget test temporarily
kubectl run --rm mytest --image=busybox -itRun nginx deployment with 2 replicas
kubectl run my-nginx --image=nginx --replicas=2 --port=80List everything
kubectl get all --all-namespacesList pods with nodes info
kubectl get pod -o wideShow nodes with labels
kubectl get nodes --show-labelsValidate yaml file with dry run
kubectl create --dry-run --validate -f pod-dummy.yamlStart a temporary pod for testing
kubectl run --rm -i -t --image=alpine test-$RANDOM -- shkubectl run shell command
kubectl exec -it mytest -- ls -l /etc/hostsGet system conf via configmap
kubectl -n kube-system get cm kubeadm-config -o yamlExplain resource kubectl explain pods
kubectl explain svcGet all services
kubectl get service --all-namespacesWatch pods
kubectl get pods -n wordpress --watchQuery healthcheck endpoint
curl -L http://127.0.0.1:10250/healthzOpen a bash terminal in a pod
kubectl exec -it storage shCheck pod environment variables
kubectl exec redis-master-ft9ex envEnable kubectl shell autocompletion
echo "source <(kubectl completion bash)" >>~/.bashrc, and reloadGet services sorted by name
kubectl get services –sort-by=.metadata.nameGet pods sorted by restart count
kubectl get pods –sort-by=’.status.containerStatuses[0].restartCount’Get node resource usage
kubectl top nodeGet pod resource usage
kubectl top podGet resource usage for a given pod
kubectl top <podname> --containersList resource utilization for all containers
kubectl top pod --all-namespaces --containers=trueDelete pod
kubectl delete pod/<pod-name> -n <my-namespace>Delete pod by force
kubectl delete pod/<pod-name> --grace-period=0 --forceDelete pods by labels
kubectl delete pod -l env=testDelete deployments by labels
kubectl delete deployment -l app=wordpressDelete all resources filtered by labels
kubectl delete pods,services -l name=myLabelDelete resources under a namespace
kubectl -n my-ns delete po,svc --allDelete persist volumes by labels
kubectl delete pvc -l app=wordpressDelete statefulset only (not pods)
kubectl delete sts/<stateful_set_name> --cascade=falseList all pods
kubectl get podsList pods for all namespace
kubectl get pods -all-namespacesList all critical pods
kubectl get -n kube-system pods -aList pods with more info
kubectl get pod -o wide, kubectl get pod/<pod-name> -o yamlGet pod info
kubectl describe pod/srv-mysql-serverList all pods with labels
kubectl get pods --show-labelsList running pods
kubectl get pods –field-selector=status.phase=RunningGet Pod initContainer status
kubectl get pod --template '{{.status.initContainerStatuses}}' <pod-name>kubectl run command
kubectl exec -it -n “$ns” “$podname” – sh -c “echo $msg >>/dev/err.log”