Kubectl Command Cheatsheet
This cheatsheet covers the most commonly used kubectl commands for daily Kubernetes cluster management, including Pod operations, workload management, networking, debugging, and cluster maintenance.
Pod Management
Create, view, and manage Pods.
``bash
# List all Pods
kubectl get pods
# List Pods in all namespaces kubectl get pods --all-namespaces
# Show more info (node, IP, etc.) kubectl get pods -o wide
# Watch Pod status changes kubectl get pods -w
# Show Pod details kubectl describe pod my-pod
# Get Pod YAML definition kubectl get pod my-pod -o yaml
# Show Pod labels kubectl get pods --show-labels
# Filter by labels kubectl get pods -l app=nginx,env=prod
# Delete a Pod kubectl delete pod my-pod
# Force delete a Pod kubectl delete pod my-pod --force --grace-period=0
# Run a Pod (one-shot) kubectl run nginx --image=nginx --restart=Never
# Run a Pod and expose a port kubectl run web --image=nginx --port=80
# Export Pod definition to file
kubectl get pod my-pod -o yaml --export > pod.yaml
`
Workload Management
Manage Deployments, StatefulSets, DaemonSets, etc.
`bash
# Create a Deployment
kubectl create deployment myapp --image=nginx
# List Deployments kubectl get deployments
# Show Deployment details kubectl describe deployment myapp
# Scale a Deployment kubectl scale deployment myapp --replicas=5
# Auto-scale a Deployment kubectl autoscale deployment myapp --min=2 --max=10 --cpu-percent=80
# Rolling update image kubectl set image deployment/myapp myapp=myapp:v2
# Check rollout status kubectl rollout status deployment/myapp
# View rollout history kubectl rollout history deployment/myapp
# Rollback to previous version kubectl rollout undo deployment/myapp
# Rollback to specific revision kubectl rollout undo deployment/myapp --to-revision=2
# Pause/resume rollout kubectl rollout pause deployment/myapp kubectl rollout resume deployment/myapp
# List DaemonSets kubectl get daemonsets --all-namespaces
# List StatefulSets kubectl get statefulsets
# Manage CronJobs
kubectl get cronjobs
kubectl create cronjob backup --image=busybox --schedule="0 0 * * *" -- /bin/sh -c "date"
`
Networking
Manage Services, Ingress, and network policies.
`bash
# List all Services
kubectl get services
# List Services in all namespaces kubectl get svc --all-namespaces
# Expose a Deployment as a Service kubectl expose deployment myapp --port=80 --target-port=8080 --type=ClusterIP
# Create NodePort Service kubectl expose deployment myapp --port=80 --type=NodePort
# Create LoadBalancer Service kubectl expose deployment myapp --port=80 --type=LoadBalancer
# Port forward to a Pod kubectl port-forward pod/my-pod 8080:80
# Port forward to a Service kubectl port-forward svc/my-service 8080:80
# View Endpoints kubectl get endpoints
# View Ingress kubectl get ingress
# View Network Policies kubectl get networkpolicies
# Create a Service (using YAML shorthand)
kubectl create service clusterip my-svc --tcp=80:8080
`
Configuration
Manage ConfigMaps, Secrets, quotas, and resource configs.
`bash
# Create ConfigMap from literals
kubectl create configmap app-config --from-literal=key1=val1 --from-literal=key2=val2
# Create ConfigMap from file kubectl create configmap app-config --from-file=config.properties
# View ConfigMaps kubectl get configmaps kubectl describe configmap app-config
# Create Secret from literals kubectl create secret generic db-secret --from-literal=password=mysecret
# Create Secret from files kubectl create secret generic tls-secret --from-file=tls.crt --from-file=tls.key
# View Secrets kubectl get secrets kubectl get secret db-secret -o yaml
# Encode/decode Secret values echo -n "mysecret" | base64 echo "bXlzZWNyZXQ=" | base64 -d
# View ResourceQuotas kubectl get resourcequotas
# View LimitRanges kubectl get limitranges
# View ServiceAccounts kubectl get serviceaccounts
# Edit a resource (default editor) kubectl edit deployment myapp
# Apply YAML configuration kubectl apply -f deployment.yaml
# Delete resources from config
kubectl delete -f deployment.yaml
`
Debugging
Logs, exec, events, and troubleshooting.
`bash
# View Pod logs
kubectl logs my-pod
# Follow logs kubectl logs -f my-pod
# View last N log lines kubectl logs --tail=100 my-pod
# View logs since a duration kubectl logs --since=1h my-pod
# View logs for a specific container in multi-container Pod kubectl logs my-pod -c sidecar-container
# Exec into a container shell kubectl exec -it my-pod -- /bin/sh
# Run a single command in a container kubectl exec my-pod -- env kubectl exec my-pod -- ls -la /app
# Copy files to a Pod kubectl cp /local/file.txt my-pod:/remote/path/
# Copy files from a Pod kubectl cp my-pod:/remote/path/file.txt /local/file.txt
# View cluster events kubectl get events kubectl get events --sort-by='.lastTimestamp' kubectl get events -w
# Filter events kubectl get events --field-selector type=Warning
# List API resources kubectl api-resources
# Explain resource fields kubectl explain pod kubectl explain pod.spec.containers
# Test API access permissions
kubectl auth can-i create deployments
kubectl auth can-i delete pods --as=system:serviceaccount:default:my-sa
`
Cluster Management
Node management, context switching, cluster info.
`bash
# List nodes
kubectl get nodes
# Show node details kubectl describe node worker-1
# Show node resource usage kubectl top node kubectl top pod
# Mark node as unschedulable kubectl cordon worker-1
# Drain pods from a node kubectl drain worker-1 --ignore-daemonsets --delete-emptydir-data
# Restore node scheduling kubectl uncordon worker-1
# Add/remove taints kubectl taint nodes worker-1 key=value:NoSchedule kubectl taint nodes worker-1 key=value:NoSchedule-
# Add/remove labels kubectl label node worker-1 disktype=ssd kubectl label node worker-1 disktype-
# Add/remove annotations kubectl annotate pod my-pod description="my annotation" kubectl annotate pod my-pod description-
# View cluster info kubectl cluster-info
# View current context kubectl config current-context
# List all contexts kubectl config get-contexts
# Switch context kubectl config use-context production
# View kubeconfig kubectl config view
# Set cluster context kubectl config set-context my-context --namespace=prod --cluster=my-cluster --user=my-user
# Merge kubeconfig files export KUBECONFIG=file1:file2 kubectl config view --flatten > merged-config
# View API versions kubectl api-versions
# View all resources in cluster kubectl get all --all-namespaces ``
Pod(7)
| Command | Level | ||
|---|---|---|---|
kubectl get podsList all Pods | Basic | kubectl get pods | |
kubectl get pods --all-namespacesList Pods in all namespaces | Basic | kubectl get pods --all-namespaces | |
kubectl describe pod my-podShow detailed Pod information | Basic | kubectl describe pod my-pod | |
kubectl get pod my-pod -o yamlExport Pod YAML definition | Intermediate | kubectl get pod my-pod -o yaml | |
kubectl get pods -l app=nginx,env=prodFilter Pods by labels | Intermediate | kubectl get pods -l app=nginx,env=prod | |
kubectl delete pod my-pod --force --grace-period=0Force delete a Pod | Intermediate | kubectl delete pod my-pod --force --grace-period=0 | |
kubectl run nginx --image=nginx --restart=NeverCreate a one-shot Pod | Basic | kubectl run nginx --image=nginx --restart=Never |
Workloads(6)
| Command | Level | ||
|---|---|---|---|
kubectl create deployment myapp --image=nginxCreate a Deployment | Basic | kubectl create deployment myapp --image=nginx | |
kubectl scale deployment myapp --replicas=5Scale a Deployment | Basic | kubectl scale deployment myapp --replicas=5 | |
kubectl set image deployment/myapp myapp=myapp:v2Rolling update Deployment image | Intermediate | kubectl set image deployment/myapp myapp=myapp:v2 | |
kubectl rollout status deployment/myappCheck Deployment rollout status | Intermediate | kubectl rollout status deployment/myapp | |
kubectl rollout undo deployment/myappRollback Deployment to previous version | Intermediate | kubectl rollout undo deployment/myapp | |
kubectl rollout history deployment/myappView Deployment rollout history | Intermediate | kubectl rollout history deployment/myapp |
Networking(4)
| Command | Level | ||
|---|---|---|---|
kubectl expose deployment myapp --port=80 --target-port=8080 --type=ClusterIPExpose Deployment as a ClusterIP Service | Intermediate | kubectl expose deployment myapp --port=80 --target-port=8080 --type=ClusterIP | |
kubectl expose deployment myapp --port=80 --type=NodePortCreate a NodePort Service | Intermediate | kubectl expose deployment myapp --port=80 --type=NodePort | |
kubectl port-forward pod/my-pod 8080:80Port forward to a Pod (local debugging) | Intermediate | kubectl port-forward pod/my-pod 8080:80 | |
kubectl get ingressView Ingress resources | Intermediate | kubectl get ingress |
Config(6)
| Command | Level | ||
|---|---|---|---|
kubectl create configmap app-config --from-literal=key1=val1Create ConfigMap from literal values | Basic | kubectl create configmap app-config --from-literal=key1=val1 | |
kubectl create configmap app-config --from-file=config.propertiesCreate ConfigMap from file | Basic | kubectl create configmap app-config --from-file=config.properties | |
kubectl create secret generic db-secret --from-literal=password=mysecretCreate Secret from literal values | Intermediate | kubectl create secret generic db-secret --from-literal=password=mysecret | |
kubectl edit deployment myappEdit a Deployment resource | Intermediate | kubectl edit deployment myapp | |
kubectl apply -f deployment.yamlApply YAML configuration file | Basic | kubectl apply -f deployment.yaml | |
kubectl delete -f deployment.yamlDelete resources defined in YAML config | Basic | kubectl delete -f deployment.yaml |
Debug(6)
| Command | Level | ||
|---|---|---|---|
kubectl logs -f my-podFollow Pod logs | Basic | kubectl logs -f my-pod | |
kubectl logs --tail=100 my-podView last N lines of Pod logs | Basic | kubectl logs --tail=100 my-pod | |
kubectl exec -it my-pod -- /bin/shEnter container interactive shell | Basic | kubectl exec -it my-pod -- /bin/sh | |
kubectl cp /local/file.txt my-pod:/remote/path/Copy files to a Pod | Intermediate | kubectl cp /local/file.txt my-pod:/remote/path/ | |
kubectl get events --sort-by='.lastTimestamp'View cluster events sorted by timestamp | Intermediate | kubectl get events --sort-by='.lastTimestamp' | |
kubectl explain pod.spec.containersView resource field documentation | Basic | kubectl explain pod.spec.containers |
Cluster(12)
| Command | Level | ||
|---|---|---|---|
kubectl auth can-i create deploymentsCheck if current user can create Deployments | Expert | kubectl auth can-i create deployments | |
kubectl get nodesList cluster nodes | Basic | kubectl get nodes | |
kubectl describe node worker-1Show node details | Intermediate | kubectl describe node worker-1 | |
kubectl top nodeShow node resource usage | Intermediate | kubectl top node | |
kubectl top podShow Pod resource usage | Intermediate | kubectl top pod | |
kubectl cordon worker-1Mark node as unschedulable | Expert | kubectl cordon worker-1 | |
kubectl drain worker-1 --ignore-daemonsets --delete-emptydir-dataDrain Pods from a node for maintenance | Expert | kubectl drain worker-1 --ignore-daemonsets --delete-emptydir-data | |
kubectl taint nodes worker-1 key=value:NoScheduleAdd taint to node to prevent scheduling | Expert | kubectl taint nodes worker-1 key=value:NoSchedule | |
kubectl cluster-infoView cluster information | Basic | kubectl cluster-info | |
kubectl config current-contextView current context | Basic | kubectl config current-context | |
kubectl config use-context productionSwitch to a different context | Basic | kubectl config use-context production | |
kubectl get all --all-namespacesView all resources in the cluster | Intermediate | kubectl get all --all-namespaces |