Kubernetes Commands Cheat Sheet

Complete kubectl reference for managing Kubernetes clusters, pods, deployments, services, and more. Essential commands for DevOps engineers and developers working with K8s.

Cluster Info & Context

Command Description
kubectl cluster-info Display cluster endpoint information
kubectl version Show client and server Kubernetes version
kubectl config view Display merged kubeconfig settings
kubectl config get-contexts List all available contexts
kubectl config current-context Display the current context
kubectl config use-context CONTEXT_NAME Switch to a different context
kubectl config set-context --current --namespace=NAMESPACE Set default namespace for current context
kubectl api-resources List all available resource types
kubectl api-versions List all supported API versions

Pod Operations

Command Description
kubectl get pods List all pods in current namespace
kubectl get pods -A List all pods in all namespaces
kubectl get pods -o wide List pods with additional details (node, IP)
kubectl get pods --watch Watch pod status changes in real-time
kubectl describe pod POD_NAME Show detailed information about a pod
kubectl logs POD_NAME Print pod logs
kubectl logs POD_NAME -c CONTAINER_NAME Print logs from specific container in pod
kubectl logs -f POD_NAME Stream pod logs (follow)
kubectl logs --previous POD_NAME Show logs from previous crashed container
kubectl exec -it POD_NAME -- /bin/bash Execute interactive shell in pod
kubectl exec POD_NAME -- COMMAND Execute command in pod
kubectl port-forward POD_NAME 8080:80 Forward local port to pod port
kubectl cp POD_NAME:/path/to/file ./local-file Copy files from pod to local
kubectl cp ./local-file POD_NAME:/path/to/file Copy files from local to pod
kubectl delete pod POD_NAME Delete a pod
kubectl delete pod POD_NAME --grace-period=0 --force Force delete a pod immediately
kubectl run nginx --image=nginx --restart=Never Create a single pod (not managed by deployment)

Deployments

Command Description
kubectl get deployments List all deployments
kubectl describe deployment DEPLOYMENT_NAME Show deployment details
kubectl create deployment NAME --image=IMAGE Create a deployment from image
kubectl scale deployment DEPLOYMENT_NAME --replicas=5 Scale deployment to 5 replicas
kubectl autoscale deployment DEPLOYMENT_NAME --min=2 --max=10 Enable horizontal pod autoscaling
kubectl set image deployment/DEPLOYMENT_NAME CONTAINER=IMAGE:TAG Update container image
kubectl rollout status deployment/DEPLOYMENT_NAME Check rollout status
kubectl rollout history deployment/DEPLOYMENT_NAME View rollout history
kubectl rollout undo deployment/DEPLOYMENT_NAME Rollback to previous revision
kubectl rollout undo deployment/DEPLOYMENT_NAME --to-revision=2 Rollback to specific revision
kubectl rollout restart deployment/DEPLOYMENT_NAME Restart all pods in deployment
kubectl rollout pause deployment/DEPLOYMENT_NAME Pause a rollout
kubectl rollout resume deployment/DEPLOYMENT_NAME Resume a paused rollout
kubectl delete deployment DEPLOYMENT_NAME Delete a deployment

Services & Networking

Command Description
kubectl get services List all services
kubectl get svc List all services (short form)
kubectl describe service SERVICE_NAME Show service details
kubectl expose deployment NAME --port=80 --type=ClusterIP Expose deployment as ClusterIP service
kubectl expose deployment NAME --port=80 --type=NodePort Expose deployment as NodePort service
kubectl expose deployment NAME --port=80 --type=LoadBalancer Expose deployment as LoadBalancer service
kubectl get endpoints List service endpoints
kubectl get ingress List all ingress resources
kubectl describe ingress INGRESS_NAME Show ingress details
kubectl get networkpolicies List network policies
kubectl port-forward service/SERVICE_NAME 8080:80 Forward local port to service
kubectl delete service SERVICE_NAME Delete a service

ConfigMaps & Secrets

Command Description
kubectl get configmaps List all configmaps
kubectl describe configmap CONFIGMAP_NAME Show configmap details
kubectl create configmap NAME --from-literal=KEY=VALUE Create configmap from literal values
kubectl create configmap NAME --from-file=PATH Create configmap from file
kubectl create configmap NAME --from-env-file=PATH Create configmap from env file
kubectl get secrets List all secrets
kubectl describe secret SECRET_NAME Show secret details (values hidden)
kubectl create secret generic NAME --from-literal=KEY=VALUE Create generic secret from literal
kubectl create secret generic NAME --from-file=PATH Create secret from file
kubectl create secret docker-registry NAME --docker-server=SERVER --docker-username=USER --docker-password=PASS Create Docker registry secret
kubectl create secret tls NAME --cert=PATH --key=PATH Create TLS secret from cert and key
kubectl get secret SECRET_NAME -o jsonpath='{.data.KEY}' | base64 -d Decode and view secret value

Namespaces

Command Description
kubectl get namespaces List all namespaces
kubectl get ns List all namespaces (short form)
kubectl describe namespace NAMESPACE_NAME Show namespace details
kubectl create namespace NAMESPACE_NAME Create a new namespace
kubectl get pods -n NAMESPACE_NAME List pods in specific namespace
kubectl get all -n NAMESPACE_NAME List all resources in namespace
kubectl delete namespace NAMESPACE_NAME Delete a namespace (deletes all resources)
kubectl config set-context --current --namespace=NAMESPACE_NAME Set default namespace for kubectl

Persistent Volumes

Command Description
kubectl get pv List all persistent volumes
kubectl get persistentvolumes List all persistent volumes (long form)
kubectl describe pv PV_NAME Show persistent volume details
kubectl get pvc List all persistent volume claims
kubectl get persistentvolumeclaims List all persistent volume claims (long form)
kubectl describe pvc PVC_NAME Show persistent volume claim details
kubectl get storageclass List all storage classes
kubectl get sc List all storage classes (short form)
kubectl describe storageclass STORAGECLASS_NAME Show storage class details
kubectl delete pvc PVC_NAME Delete a persistent volume claim

RBAC (Role-Based Access Control)

Command Description
kubectl get roles List roles in current namespace
kubectl get roles -A List roles in all namespaces
kubectl describe role ROLE_NAME Show role details
kubectl get rolebindings List role bindings
kubectl describe rolebinding ROLEBINDING_NAME Show role binding details
kubectl get clusterroles List cluster roles
kubectl describe clusterrole CLUSTERROLE_NAME Show cluster role details
kubectl get clusterrolebindings List cluster role bindings
kubectl get serviceaccounts List service accounts
kubectl get sa List service accounts (short form)
kubectl create serviceaccount SA_NAME Create a service account
kubectl auth can-i VERB RESOURCE Check if you can perform action (e.g., create pods)
kubectl auth can-i VERB RESOURCE --as USER Check permissions for specific user

Debugging & Troubleshooting

Command Description
kubectl get events List all events in current namespace
kubectl get events --sort-by=.metadata.creationTimestamp List events sorted by time
kubectl get events -A List events in all namespaces
kubectl describe pod POD_NAME Show pod details including recent events
kubectl logs POD_NAME --all-containers=true Show logs from all containers in pod
kubectl logs -l app=myapp Show logs from pods matching label
kubectl top nodes Show node resource usage (CPU, memory)
kubectl top pods Show pod resource usage
kubectl top pods -A Show pod resource usage in all namespaces
kubectl get pods --field-selector=status.phase=Failed List only failed pods
kubectl get pods --field-selector=status.phase=Pending List only pending pods
kubectl debug POD_NAME -it --image=busybox Create debug container in pod
kubectl run debug --rm -it --image=busybox -- sh Create temporary debug pod
kubectl attach POD_NAME -it Attach to running container

Useful kubectl Flags & Tips

Command Description
kubectl get pods -o yaml Output in YAML format
kubectl get pods -o json Output in JSON format
kubectl get pods -o wide Output with additional columns
kubectl get pods -o name Output only resource names
kubectl get pods -o jsonpath='{.items[*].metadata.name}' Extract specific fields with JSONPath
kubectl get pods -l app=myapp Filter by label selector
kubectl get pods -l 'env in (prod,staging)' Filter by multiple label values
kubectl get pods --show-labels Show all pod labels
kubectl label pod POD_NAME env=prod Add label to pod
kubectl label pod POD_NAME env- Remove label from pod
kubectl annotate pod POD_NAME description='My pod' Add annotation to pod
kubectl apply -f file.yaml Create/update resources from file
kubectl apply -f directory/ Apply all YAML files in directory
kubectl delete -f file.yaml Delete resources defined in file
kubectl diff -f file.yaml Show diff between current and desired state
kubectl create deployment NAME --image=IMAGE --dry-run=client -o yaml Generate YAML without creating resource
kubectl get all List common resources (pods, services, deployments)
kubectl get all -A List common resources in all namespaces
kubectl explain pod Show documentation for pod resource
kubectl explain pod.spec.containers Show documentation for nested field
kubectl proxy Start proxy to Kubernetes API server
kubectl wait --for=condition=ready pod -l app=myapp Wait for pods to be ready
kubectl replace --force -f file.yaml Force replace resource (delete and recreate)
kubectl patch deployment NAME -p '{"spec":{"replicas":3}}' Patch resource with JSON
kubectl cordon NODE_NAME Mark node as unschedulable
kubectl uncordon NODE_NAME Mark node as schedulable
kubectl drain NODE_NAME --ignore-daemonsets Drain node for maintenance
kubectl taint nodes NODE_NAME key=value:NoSchedule Add taint to node
kubectl completion bash Generate bash completion script

Pro Tips