Kubectl Command Cheat Sheet
Syntax
Basic syntax: kubectl [command] [TYPE] [NAME] [flags]
COMMAND | DESCRIPTION |
---|
kubectl --help | Display the command usage and list most common options |
kubectl <command> --help | Display the command usage and list all available options |
kubectl explain <resource> | Get documentation of resources |
Cluster & Context Management
COMMAND | DESCRIPTION |
---|
kubectl config view | Display merged kubeconfig settings |
kubectl config current-context | Display the current context |
kubectl config use-context <context> | Set the default context |
kubectl config set-context <context> | Set a context entry in kubeconfig |
kubectl config get-contexts | List all available contexts |
kubectl cluster-info | Display cluster information |
Resource Management
COMMAND | DESCRIPTION |
---|
kubectl get <resource> | List resources |
kubectl get <resource> -o wide | List resources with more details |
kubectl get <resource> -o yaml | List resources in YAML format |
kubectl get <resource> -o json | List resources in JSON format |
kubectl describe <resource> <name> | Display detailed information about a resource |
kubectl create -f <file.yaml> | Create a resource from a file |
kubectl apply -f <file.yaml> | Create or update a resource from a file |
kubectl delete <resource> <name> | Delete resources |
kubectl delete -f <file.yaml> | Delete resources using a file |
Workload Resources
COMMAND | DESCRIPTION |
---|
kubectl run <name> --image=<image> | Create and run a pod from an image |
kubectl create deployment <name> --image=<image> | Create a deployment |
kubectl scale deployment <name> --replicas=<n> | Scale a deployment |
kubectl set image deployment/<name> <container>=<image> | Update container image |
kubectl rollout status deployment/<name> | Check the status of a rollout |
kubectl rollout history deployment/<name> | View rollout history |
kubectl rollout undo deployment/<name> | Rollback to the previous version |
kubectl rollout restart deployment/<name> | Restart a deployment |
Pod Operations
COMMAND | DESCRIPTION |
---|
kubectl get pods | List all pods |
kubectl get pods -n <namespace> | List pods in a namespace |
kubectl get pods --show-labels | List pods with their labels |
kubectl get pods -l key=value | List pods with specific labels |
kubectl describe pod <pod-name> | Show detailed pod information |
kubectl logs <pod-name> | Print pod logs |
kubectl logs -f <pod-name> | Stream pod logs |
kubectl logs <pod-name> -c <container> | Print container logs from a pod |
kubectl exec -it <pod-name> -- <command> | Execute a command in a pod |
kubectl port-forward <pod-name> <local-port>:<pod-port> | Forward a port to a pod |
Service & Networking
COMMAND | DESCRIPTION |
---|
kubectl get services | List all services |
kubectl expose deployment <name> --port=<port> --target-port=<target-port> | Expose a deployment as a service |
kubectl get endpoints | List all endpoints |
kubectl get ingress | List all ingresses |
kubectl describe ingress <name> | Show detailed ingress information |
kubectl get networkpolicies | List all network policies |
Configuration & Storage
COMMAND | DESCRIPTION |
---|
kubectl get configmaps | List all configmaps |
kubectl create configmap <name> --from-file=<path> | Create configmap from a file |
kubectl create configmap <name> --from-literal=key=value | Create configmap from literal values |
kubectl get secrets | List all secrets |
kubectl create secret generic <name> --from-file=<path> | Create a secret from a file |
kubectl create secret generic <name> --from-literal=key=value | Create a secret from literal values |
kubectl get pv | List all persistent volumes |
kubectl get pvc | List all persistent volume claims |
kubectl get storageclasses | List all storage classes |
RBAC & Security
COMMAND | DESCRIPTION |
---|
kubectl get serviceaccounts | List all service accounts |
kubectl create serviceaccount <name> | Create a service account |
kubectl get roles | List all roles |
kubectl get rolebindings | List all role bindings |
kubectl get clusterroles | List all cluster roles |
kubectl get clusterrolebindings | List all cluster role bindings |
kubectl auth can-i <verb> <resource> | Check if you can perform an action |
Namespaces & Resource Quotas
COMMAND | DESCRIPTION |
---|
kubectl get namespaces | List all namespaces |
kubectl create namespace <name> | Create a namespace |
kubectl get resourcequotas | List all resource quotas |
kubectl get limitranges | List all limit ranges |
kubectl -n <namespace> get <resource> | Get resources in specific namespace |
Debugging & Troubleshooting
COMMAND | DESCRIPTION |
---|
kubectl get events | Show events |
kubectl get events --sort-by=.metadata.creationTimestamp | Show events sorted by timestamp |
kubectl describe node <node-name> | Show detailed node information |
kubectl top node | Show node resource usage |
kubectl top pod | Show pod resource usage |
kubectl get componentstatuses | Check control plane components status |
kubectl get podsecuritypolicies | List pod security policies |
kubectl debug <pod-name> -it --image=<debugger-image> | Debug a pod with a debug container |
Advanced Operations
COMMAND | DESCRIPTION |
---|
kubectl diff -f <file.yaml> | Preview apply changes |
kubectl patch <resource> <name> -p '{}' | Update field(s) of a resource |
kubectl cp <pod-name>:/path /local/path | Copy files from pod to local |
kubectl cp /local/path <pod-name>:/path | Copy files from local to pod |
kubectl taint nodes <node-name> key=value:effect | Add a taint to a node |
kubectl cordon <node-name> | Mark node as unschedulable |
kubectl drain <node-name> | Drain node in preparation for maintenance |
kubectl uncordon <node-name> | Mark node as schedulable |
kubectl api-resources | List all supported resources and their API groups |
kubectl api-versions | List all supported API versions |
Common Options
OPTION | DESCRIPTION |
---|
-n, --namespace <namespace> | Specify namespace |
-A, --all-namespaces | List resources across all namespaces |
-l, --selector <key=value> | Filter resources by label |
-o, --output <format> | Output format (json, yaml, wide, etc.) |
--dry-run=client | Simulate an operation without making actual changes |
--field-selector <field=value> | Filter resources by fields |
--force | Force immediate removal of resources |
--grace-period=<seconds> | Period of time to wait before force termination |
--wait | Wait for completion before returning |
--timeout=<duration> | Set timeout for operation |
Working with Manifests
COMMAND | DESCRIPTION |
---|
kubectl kustomize <dir> | Build a kustomization target |
kubectl apply -k <dir> | Apply a kustomization directory |
kubectl delete -k <dir> | Delete resources using a kustomization directory |
kubectl create -f <url> | Create resources from a URL |
kubectl replace -f <file> | Replace a resource from a file |
Comments