Skip to content
DBDeependra Bhatta~/notes

kubectl Cheat Sheet (With Clear Categories & Uses)

πŸ“Œ 1. Basic Cluster & Node Info Command Use kubectl version –short Check client & server versions kubectl cluster-info Display cluster master & services kubectl get nodes List nodes kubectl describe…

Β· updated Β· 6 min read
ON THIS PAGE

πŸ“Œ 1. Basic Cluster & Node Info

CommandUse
kubectl version --shortCheck client & server versions
kubectl cluster-infoDisplay cluster master & services
kubectl get nodesList nodes
kubectl describe node <node-name>Detailed node information
kubectl top nodesShow node resource consumption (metrics server needed)

πŸ“Œ 2. Working With Namespaces

CommandUse
kubectl get namespacesList namespaces
kubectl create namespace devCreate namespace
kubectl delete namespace devDelete namespace
kubectl config set-context --current --namespace=devSet default namespace

πŸ“Œ 3. Working With Pods

CommandUse
kubectl get podsList pods
kubectl get pods -o wideList pods with node & IP
kubectl describe pod <pod>Detailed pod info
kubectl logs <pod>Get logs
kubectl logs <pod> -c <container>Logs of specific container
kubectl exec -it <pod> -- /bin/bashShell into container
kubectl port-forward <pod> 8080:80Forward port to local
kubectl delete pod <pod>Delete pod

πŸ“Œ 4. Deployments

CommandUse
kubectl get deploymentsList deployments
kubectl create deployment nginx --image=nginxCreate deployment
kubectl describe deployment <name>Deployment details
kubectl scale deployment nginx --replicas=5Scale deployment
kubectl set image deployment/nginx nginx=nginx:1.27Update image
kubectl rollout status deployment/nginxCheck rollout
kubectl rollout history deployment/nginxView rollout history
kubectl rollout undo deployment/nginxRollback deployment

πŸ“Œ 5. Services

CommandUse
kubectl get svcList services
kubectl expose deployment nginx --type=NodePort --port=80Create Service
kubectl describe svc <name>Service details
kubectl delete svc <name>Delete service

πŸ“Œ 6. ConfigMaps

CommandUse
kubectl create configmap mycfg --from-literal=ENV=prodCreate ConfigMap
kubectl create configmap mycfg --from-file=app.confCreate from file
kubectl get configmapsList
kubectl describe configmap mycfgShow details
kubectl delete configmap mycfgRemove

πŸ“Œ 7. Secrets

CommandUse
kubectl create secret generic mysecret --from-literal=password=secret123Create secret
kubectl get secretsList
kubectl describe secret mysecretSecret info
kubectl delete secret mysecretRemove
kubectl get secret mysecret -o yamlView encoded value
`echo β€œβ€base64 –decode`

πŸ“Œ 8. Ingress

CommandUse
kubectl get ingressList ingress
kubectl describe ingress <name>Details
kubectl delete ingress <name>Delete

πŸ“Œ 9. Jobs & CronJobs

CommandUse
kubectl get jobsList jobs
kubectl create job testjob --image=busybox -- echo "Hi"Create job
kubectl get cronjobsList cronjobs
kubectl delete job/cronjob <name>Delete

πŸ“Œ 10. Storage

CommandUse
kubectl get pvList persistent volumes
kubectl get pvcList persistent volume claims
kubectl describe pv/pvc <name>Details
kubectl delete pvc <name>Delete

πŸ“Œ 11. Editing Resources

CommandUse
kubectl edit deployment nginxEdit resource in editor
kubectl edit svc myserviceEdit service

πŸ“Œ 12. Apply & Manage YAML Manifests

CommandUse
kubectl apply -f app.yamlCreate/update resource
kubectl delete -f app.yamlDelete resource in file
kubectl diff -f app.yamlShow changes before apply

πŸ“Œ 13. Useful Output Formatting

CommandUse
kubectl get pods -o wideMore details
kubectl get pods -o yamlYAML output
kubectl get pods -o jsonJSON output
kubectl get pods --sort-by=.metadata.nameSort
kubectl get pods -AAll namespaces

πŸ“Œ 14. Resource Debugging

CommandUse
kubectl describe <resource>Deep debugging
kubectl get events --sort-by=.metadata.creationTimestampEvents timeline
kubectl logs <pod>Logs
kubectl exec -it <pod> -- shShell into pod
kubectl debug node/<node> --image=busyboxDebug node (K8s 1.23+)

πŸ“Œ 15. Context & Configuration

CommandUse
kubectl config get-contextsList contexts
kubectl config use-context <context>Switch cluster
kubectl config current-contextCurrent context
kubectl config viewView kubeconfig
kubectl config set-context ...Modify context

πŸ“Œ 16. Labels & Annotations

CommandUse
kubectl label pod <pod> env=prodAdd label
kubectl label pod <pod> env-Remove label
kubectl annotate pod <pod> description="My pod"Add annotation

πŸ“Œ 17. Deleting Resources

CommandUse
kubectl delete pod <pod>Delete pod
kubectl delete deployment <name>Delete deployment
kubectl delete namespace <name>Delete namespace
kubectl delete --all podsDelete all pods

πŸ“Œ 18. Auto-Generate YAML (Very Useful!)

CommandUse
kubectl create deployment nginx --image=nginx -o yaml --dry-run=clientGenerate YAML
kubectl create configmap test --from-literal=a=1 -o yaml --dry-run=clientGenerate YAML

πŸ“Œ 19. Shortcuts (Aliases)

ShortcutOriginal
kubectl get pods β†’ kubectl get popo = pods
kubectl get svc β†’ kubectl get servicessvc = service
kubectl get deploydeployments
kubectl get cmconfigmaps
kubectl get nsnamespaces
  • Kubernetes in AWS

    In this topic we will learn how kubeadm is used to used to setup cluster. ECS is not recommended while setting up cluster. We are doing example of EKS in this blog. In complex scenarios, highly…

  • minikube

    How to install minikube in linux You can download the minikube from the above link for different OS. For now i will explain steps to setup in linux environment. Run this in your terminal and it will…

  • KUbernetes

    Introduction kubernetes is similar to docker swarm. Kubernetes is also a container orchestration tool. We can run kubernetes in every environment example laptop, dev, production, cloud. So if there…