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β¦
ON THIS PAGE
π 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 node <node-name> | Detailed node information |
kubectl top nodes | Show node resource consumption (metrics server needed) |
π 2. Working With Namespaces
| Command | Use |
|---|---|
kubectl get namespaces | List namespaces |
kubectl create namespace dev | Create namespace |
kubectl delete namespace dev | Delete namespace |
kubectl config set-context --current --namespace=dev | Set default namespace |
π 3. Working With Pods
| Command | Use |
|---|---|
kubectl get pods | List pods |
kubectl get pods -o wide | List 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/bash | Shell into container |
kubectl port-forward <pod> 8080:80 | Forward port to local |
kubectl delete pod <pod> | Delete pod |
π 4. Deployments
| Command | Use |
|---|---|
kubectl get deployments | List deployments |
kubectl create deployment nginx --image=nginx | Create deployment |
kubectl describe deployment <name> | Deployment details |
kubectl scale deployment nginx --replicas=5 | Scale deployment |
kubectl set image deployment/nginx nginx=nginx:1.27 | Update image |
kubectl rollout status deployment/nginx | Check rollout |
kubectl rollout history deployment/nginx | View rollout history |
kubectl rollout undo deployment/nginx | Rollback deployment |
π 5. Services
| Command | Use |
|---|---|
kubectl get svc | List services |
kubectl expose deployment nginx --type=NodePort --port=80 | Create Service |
kubectl describe svc <name> | Service details |
kubectl delete svc <name> | Delete service |
π 6. ConfigMaps
| Command | Use |
|---|---|
kubectl create configmap mycfg --from-literal=ENV=prod | Create ConfigMap |
kubectl create configmap mycfg --from-file=app.conf | Create from file |
kubectl get configmaps | List |
kubectl describe configmap mycfg | Show details |
kubectl delete configmap mycfg | Remove |
π 7. Secrets
| Command | Use |
|---|---|
kubectl create secret generic mysecret --from-literal=password=secret123 | Create secret |
kubectl get secrets | List |
kubectl describe secret mysecret | Secret info |
kubectl delete secret mysecret | Remove |
kubectl get secret mysecret -o yaml | View encoded value |
| `echo ββ | base64 βdecode` |
π 8. Ingress
| Command | Use |
|---|---|
kubectl get ingress | List ingress |
kubectl describe ingress <name> | Details |
kubectl delete ingress <name> | Delete |
π 9. Jobs & CronJobs
| Command | Use |
|---|---|
kubectl get jobs | List jobs |
kubectl create job testjob --image=busybox -- echo "Hi" | Create job |
kubectl get cronjobs | List cronjobs |
kubectl delete job/cronjob <name> | Delete |
π 10. Storage
| Command | Use |
|---|---|
kubectl get pv | List persistent volumes |
kubectl get pvc | List persistent volume claims |
kubectl describe pv/pvc <name> | Details |
kubectl delete pvc <name> | Delete |
π 11. Editing Resources
| Command | Use |
|---|---|
kubectl edit deployment nginx | Edit resource in editor |
kubectl edit svc myservice | Edit service |
π 12. Apply & Manage YAML Manifests
| Command | Use |
|---|---|
kubectl apply -f app.yaml | Create/update resource |
kubectl delete -f app.yaml | Delete resource in file |
kubectl diff -f app.yaml | Show changes before apply |
π 13. Useful Output Formatting
| Command | Use |
|---|---|
kubectl get pods -o wide | More details |
kubectl get pods -o yaml | YAML output |
kubectl get pods -o json | JSON output |
kubectl get pods --sort-by=.metadata.name | Sort |
kubectl get pods -A | All namespaces |
π 14. Resource Debugging
| Command | Use |
|---|---|
kubectl describe <resource> | Deep debugging |
kubectl get events --sort-by=.metadata.creationTimestamp | Events timeline |
kubectl logs <pod> | Logs |
kubectl exec -it <pod> -- sh | Shell into pod |
kubectl debug node/<node> --image=busybox | Debug node (K8s 1.23+) |
π 15. Context & Configuration
| Command | Use |
|---|---|
kubectl config get-contexts | List contexts |
kubectl config use-context <context> | Switch cluster |
kubectl config current-context | Current context |
kubectl config view | View kubeconfig |
kubectl config set-context ... | Modify context |
π 16. Labels & Annotations
| Command | Use |
|---|---|
kubectl label pod <pod> env=prod | Add label |
kubectl label pod <pod> env- | Remove label |
kubectl annotate pod <pod> description="My pod" | Add annotation |
π 17. Deleting Resources
| Command | Use |
|---|---|
kubectl delete pod <pod> | Delete pod |
kubectl delete deployment <name> | Delete deployment |
kubectl delete namespace <name> | Delete namespace |
kubectl delete --all pods | Delete all pods |
π 18. Auto-Generate YAML (Very Useful!)
| Command | Use |
|---|---|
kubectl create deployment nginx --image=nginx -o yaml --dry-run=client | Generate YAML |
kubectl create configmap test --from-literal=a=1 -o yaml --dry-run=client | Generate YAML |
π 19. Shortcuts (Aliases)
| Shortcut | Original |
|---|---|
kubectl get pods β kubectl get po | po = pods |
kubectl get svc β kubectl get services | svc = service |
kubectl get deploy | deployments |
kubectl get cm | configmaps |
kubectl get ns | namespaces |
Keep reading
- 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β¦