Kubernetes Cheatsheet

A comprehensive quick reference for Kubernetes commands, configuration, and troubleshooting. Keep this cheatsheet handy for daily container orchestration operations.

kubectl Configuration Troubleshooting Advanced
Basics
Cluster Information
kubectl cluster-info
Display cluster information
kubectl cluster-info
kubectl version
Display client and server versions
kubectl version --short
kubectl config view
View current kubeconfig
kubectl config view --minify
kubectl get nodes
List all nodes in the cluster
kubectl get nodes -o wide
kubectl get namespaces
List all namespaces
kubectl get ns
Pods
Listing and Inspecting Pods
kubectl get pods
List all pods in current namespace
kubectl get pods
kubectl get pods --all-namespaces
List pods in all namespaces
kubectl get pods --all-namespaces
kubectl get pods -o wide
List pods with detailed info (IP, node, etc.)
kubectl get pods -o wide
kubectl describe pod <pod>
Show detailed pod information
kubectl describe pod nginx-pod
kubectl get pod <pod> -o yaml
Get pod YAML definition
kubectl get pod nginx-pod -o yaml
Creating and Running Pods
kubectl run <name> --image=<image>
Create and run a pod
kubectl run nginx --image=nginx
kubectl run --restart=Never <name> --image=<image>
Create a standalone pod
kubectl run nginx --image=nginx --restart=Never
kubectl create -f <file>
Create resources from file
kubectl create -f pod.yaml
kubectl apply -f <file>
Apply resources from file (create/update)
kubectl apply -f pod.yaml
Managing Pods
kubectl delete pod <pod>
Delete a pod
kubectl delete pod nginx-pod
kubectl delete -f <file>
Delete resources from file
kubectl delete -f pod.yaml
kubectl logs <pod>
View pod logs
kubectl logs nginx-pod
kubectl logs -f <pod>
Follow pod logs
kubectl logs -f nginx-pod
kubectl exec -it <pod> -- /bin/sh
Execute a command in a pod
kubectl exec -it nginx-pod -- /bin/sh
kubectl port-forward <pod> <local:remote>
Forward local port to pod
kubectl port-forward nginx-pod 8080:80
Deployments
kubectl get deployments
List all deployments
kubectl get deployments
kubectl create deployment <name> --image=<image>
Create a deployment
kubectl create deployment nginx --image=nginx
kubectl scale deployment <name> --replicas=<count>
Scale a deployment
kubectl scale deployment nginx --replicas=3
kubectl set image deployment <name> <container>=<image>
Update image of a deployment
kubectl set image deployment/nginx nginx=nginx:1.25
kubectl rollout status deployment <name>
Check rollout status
kubectl rollout status deployment nginx
kubectl rollout undo deployment <name>
Rollback deployment
kubectl rollout undo deployment nginx
kubectl rollout history deployment <name>
View rollout history
kubectl rollout history deployment nginx
kubectl delete deployment <name>
Delete a deployment
kubectl delete deployment nginx
Services
kubectl get services
List all services
kubectl get svc
kubectl expose deployment <name> --type=<type> --port=<port>
Expose a deployment as a service
kubectl expose deployment nginx --type=ClusterIP --port=80
kubectl describe service <name>
Show detailed service information
kubectl describe svc nginx
kubectl get endpoints
List service endpoints
kubectl get endpoints
kubectl delete service <name>
Delete a service
kubectl delete svc nginx
kubectl port-forward service/<name> <local:remote>
Forward local port to a service
kubectl port-forward svc/nginx 8080:80
Configuration
kubectl get configmaps
List ConfigMaps
kubectl get configmaps
kubectl create configmap <name> --from-literal=<key>=<value>
Create a ConfigMap
kubectl create configmap app-config --from-literal=env=prod
kubectl get secrets
List secrets
kubectl get secrets
kubectl create secret generic <name> --from-literal=<key>=<value>
Create a secret
kubectl create secret generic db-creds --from-literal=password=secret
kubectl get serviceaccounts
List service accounts
kubectl get serviceaccounts
kubectl create serviceaccount <name>
Create a service account
kubectl create serviceaccount app-sa
kubectl label <resource> <name> <key>=<value>
Add a label to a resource
kubectl label pod nginx app=web
kubectl annotate <resource> <name> <key>=<value>
Add an annotation to a resource
kubectl annotate pod nginx description="web server"
Storage
kubectl get persistentvolumes
List PersistentVolumes
kubectl get pv
kubectl get persistentvolumeclaims
List PersistentVolumeClaims
kubectl get pvc
kubectl get storageclasses
List StorageClasses
kubectl get sc
kubectl create -f <storage-file>
Create storage resources
kubectl create -f pvc.yaml
Networking
kubectl get ingress
List Ingress resources
kubectl get ingress
kubectl get networkpolicies
List NetworkPolicies
kubectl get networkpolicies
kubectl describe ingress <name>
Describe an Ingress
kubectl describe ingress my-ingress
kubectl get endpoints
List service endpoints
kubectl get endpoints
kubectl get svc --all-namespaces
List services in all namespaces
kubectl get svc -A
Troubleshooting
kubectl describe pod <pod>
Detailed pod information
kubectl describe pod nginx-pod
kubectl logs <pod>
View pod logs
kubectl logs nginx-pod
kubectl logs --previous <pod>
View previous container logs
kubectl logs --previous nginx-pod
kubectl exec -it <pod> -- /bin/sh
Execute command in pod
kubectl exec -it nginx-pod -- /bin/sh
kubectl top nodes
Show node resource usage
kubectl top nodes
kubectl top pods
Show pod resource usage
kubectl top pods
kubectl get events
List cluster events
kubectl get events --sort-by='.lastTimestamp'
kubectl get events --field-selector involvedObject.name=<pod>
List events for a specific pod
kubectl get events --field-selector involvedObject.name=nginx-pod
kubectl describe node <node>
Describe a node
kubectl describe node node-1
Advanced
kubectl get crds
List CustomResourceDefinitions
kubectl get crds
kubectl get componentstatuses
Check component health
kubectl get cs
kubectl get roles,clusterroles,rolebindings,clusterrolebindings
List RBAC resources
kubectl get roles --all-namespaces
kubectl auth can-i <verb> <resource>
Check if you can perform an action
kubectl auth can-i get pods
kubectl cordon <node>
Mark node as unschedulable
kubectl cordon node-1
kubectl drain <node> --ignore-daemonsets
Drain a node for maintenance
kubectl drain node-1 --ignore-daemonsets
kubectl uncordon <node>
Mark node as schedulable
kubectl uncordon node-1
kubectl taint nodes <node> <key>=<value>:<effect>
Add a taint to a node
kubectl taint nodes node-1 dedicated=db:NoSchedule
kubectl taint nodes <node> <key>-
Remove a taint
kubectl taint nodes node-1 dedicated-
Frequently Asked Questions
How do I switch between Kubernetes contexts?
Use kubectl config use-context <context-name>. List available contexts with kubectl config get-contexts.
What is the difference between apply and create?
kubectl create creates a new resource and fails if it exists. kubectl apply creates or updates a resource (declarative management). Use apply for GitOps and declarative workflows.
How do I view logs for a specific container in a multi-container pod?
Use kubectl logs <pod> -c <container-name> to view logs for a specific container.
How do I get the IP address of a pod?
Use kubectl get pods -o wide to see pod IPs, or kubectl get pod <pod> -o jsonpath='{.status.podIP}'.
What is the default namespace?
The default namespace is default. Use -n <namespace> to target a different namespace.
How do I delete all resources in a namespace?
Use kubectl delete all --all -n <namespace>. Be careful—this deletes most resources but not all (e.g., PVCs).
How do I edit a running resource?
Use kubectl edit <resource> <name> to edit a resource in your default editor.
How do I get help for a kubectl command?
Use kubectl <command> --help or kubectl help for general help.
Previous: Interview Questions

Keep this cheatsheet handy for quick reference during daily Kubernetes operations. Bookmark it for easy access.