Kubectl Cheatsheet
Configuration and Context Management
kubectl config current-context
kubectl config get-contexts
- Switch to a different context
kubectl config use-context <context-name>
- Set default namespace for current context
kubectl config set-context --current --namespace=<namespace>
kubectl config view
kubectl cluster-info
Pod Management
kubectl get pods
- List pods in specific namespace
kubectl get pods -n <namespace>
- List pods with more details
kubectl get pods -o wide
- Create a pod from YAML file
kubectl apply -f <pod.yaml>
kubectl run <pod-name> --image=<image-name>
kubectl delete pod <pod-name>
kubectl describe pod <pod-name>
kubectl logs <pod-name>
kubectl logs -f <pod-name>
- Execute commands in a pod
kubectl exec -it <pod-name> -- /bin/bash
Deployment Management
kubectl create deployment <deployment-name> --image=<image-name>
kubectl get deployments
kubectl describe deployment <deployment-name>
kubectl scale deployment <deployment-name> --replicas=<number>
kubectl set image deployment/<deployment-name> <container-name>=<new-image>
kubectl rollout undo deployment/<deployment-name>
kubectl rollout status deployment/<deployment-name>
kubectl delete deployment <deployment-name>
Service Management
kubectl expose deployment <deployment-name> --type=<service-type> --port=<port>
kubectl get services
kubectl describe service <service-name>
kubectl delete service <service-name>
- Port forward to a service
kubectl port-forward service/<service-name> <local-port>:<service-port>
Namespace Management
kubectl get namespaces
kubectl create namespace <namespace-name>
kubectl delete namespace <namespace-name>
- Get resources in all namespaces
kubectl get <resource-type> --all-namespaces
ConfigMaps and Secrets
- Create ConfigMap from literal values
kubectl create configmap <configmap-name> --from-literal=<key>=<value>
- Create ConfigMap from file
kubectl create configmap <configmap-name> --from-file=<file-path>
kubectl get configmaps
- Create Secret from literal values
kubectl create secret generic <secret-name> --from-literal=<key>=<value>
kubectl create secret generic <secret-name> --from-file=<file-path>
kubectl get secrets
kubectl get secret <secret-name> -o jsonpath="{.data.<key>}" | base64 --decode
Resource Inspection and Debugging
kubectl get all
- Get resources with labels
kubectl get <resource-type> -l <label-key>=<label-value>
kubectl get <resource-type> --watch
- Get resource in YAML format
kubectl get <resource-type> <resource-name> -o yaml
- Get resource in JSON format
kubectl get <resource-type> <resource-name> -o json
kubectl edit <resource-type> <resource-name>
kubectl get events
- Get events sorted by timestamp
kubectl get events --sort-by='.metadata.creationTimestamp'
Node Management
kubectl get nodes
kubectl describe node <node-name>
- Cordon a node (mark as unschedulable)
kubectl cordon <node-name>
kubectl uncordon <node-name>
kubectl drain <node-name> --ignore-daemonsets
Persistent Volumes and Claims
kubectl get pv
- List Persistent Volume Claims
kubectl get pvc
kubectl apply -f <pvc.yaml>
kubectl delete pvc <pvc-name>
Labels and Annotations
kubectl label <resource-type> <resource-name> <key>=<value>
- Remove label from resource
kubectl label <resource-type> <resource-name> <key>-
- Add annotation to resource
kubectl annotate <resource-type> <resource-name> <key>=<value>
- Remove annotation from resource
kubectl annotate <resource-type> <resource-name> <key>-
Resource Management with Files
- Apply configuration from file
kubectl apply -f <file.yaml>
- Apply configurations from directory
kubectl apply -f <directory>/
- Apply with recursive directory search
kubectl apply -R -f <directory>/
- Delete resources from file
kubectl delete -f <file.yaml>
kubectl apply -f <file.yaml> --dry-run=client
Troubleshooting and Debugging
- Check resource usage of nodes
kubectl top nodes
- Check resource usage of pods
kubectl top pods
- Get pod logs from previous container instance
kubectl logs <pod-name> --previous
- Get logs from specific container in pod
kubectl logs <pod-name> -c <container-name>
kubectl cp <pod-name>:<path> <local-path>
kubectl cp <local-path> <pod-name>:<path>
kubectl run debug-pod --rm -i --tty --image=busybox -- /bin/sh
Common Utilities
kubectl version
kubectl api-resources
kubectl api-versions
kubectl explain <resource-type>
kubectl explain <resource-type>.<field>
kubectl get <resource-type> -o wide
kubectl get <resource-type> -o yaml
kubectl get <resource-type> -o json
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
kubectl <command> --help