Kubernetes Cheatsheet

This cheatsheet provides a comprehensive and practical reference for common Kubernetes (kubectl) commands. It covers cluster info, pods, deployments, services, configs, logs, command combos, and more. Use it to boost your productivity in Kubernetes and DevOps workflows.

Cluster Info

kubectl version
Show kubectl version info
kubectl cluster-info
Show cluster info
kubectl config view
Show kubeconfig settings
kubectl config get-contexts
List all contexts
kubectl config use-context [name]
Switch context
kubectl get nodes
List all nodes
kubectl get nodes -o wide
List all nodes with more info
kubectl describe node [name]
Show node details
kubectl get node --selector=[label]
List nodes by label selector
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
Show external IPs of all nodes
kubectl top node [name]
Show node resource usage (metrics-server required)

Namespaces

kubectl get ns
List all namespaces
kubectl get ns -o yaml
List namespaces in YAML format
kubectl describe ns
Show namespace details
kubectl delete ns [name]
Delete a namespace
kubectl edit ns [name]
Edit a namespace

Pods

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 node info
kubectl get pods --show-labels
Show pod labels
kubectl get pods -l app=[label]
List pods by label
kubectl get pods --field-selector status.phase=Running
List running pods only
kubectl describe pod [name]
Show pod details
kubectl exec -it [pod] -- bash
Run bash in a pod
kubectl logs [pod]
Show pod logs
kubectl logs --since=1h [pod]
Show pod logs from the last hour
kubectl logs --tail=20 [pod]
Show last 20 lines of pod logs
kubectl logs -f -c [container] [pod]
Follow logs for a specific container in a pod
kubectl delete pod [name]
Delete a pod
kubectl port-forward [pod] 8080:80
Forward port 8080 to pod's 80
kubectl cp [pod]:/src /dest
Copy files from pod to local
kubectl run [pod] --image=nginx --restart=Never
Run a single pod with nginx image
kubectl get pod [name] -o yaml --export > nameoffile.yaml
Export pod YAML to file
kubectl get pods -o json | jq .items[].metadata.name
List pod names using jq

Deployments

kubectl get deployments
List all deployments
kubectl describe deployment [name]
Show deployment details
kubectl get deploy -o wide
List deployments with more info
kubectl get deploy -o yaml
List deployments in YAML format
kubectl create deployment [name] --image=[img]
Create a deployment
kubectl scale deployment [name] --replicas=3
Scale deployment to 3 pods
kubectl set image deployment/[name] [container]=[img]
Update deployment image
kubectl rollout status deployment/[name]
Show rollout status
kubectl rollout undo deployment/[name]
Rollback deployment
kubectl delete deployment [name]
Delete a deployment
kubectl edit deploy [name]
Edit a deployment
kubectl expose deploy [name] --port=80 --type=NodePort
Expose deployment as a NodePort service

ReplicaSets

kubectl get rs
List all ReplicaSets
kubectl describe rs
Show ReplicaSet details
kubectl get rs -o wide
List ReplicaSets with more info
kubectl get rs -o yaml
List ReplicaSets in YAML format

DaemonSets

kubectl get ds
List all DaemonSets
kubectl describe ds --all-namespaces
Show DaemonSets in all namespaces
kubectl describe ds [name] -n [namespace]
Show DaemonSet details in a namespace
kubectl get ds [name] -n [namespace] -o yaml
Get DaemonSet YAML in a namespace
kubectl edit ds [name] -n kube-system
Edit a DaemonSet in kube-system namespace
kubectl delete ds [name]
Delete a DaemonSet

Services

kubectl get svc
List all services
kubectl describe svc [name]
Show service details
kubectl get svc -o wide
List services with more info
kubectl get svc -o yaml
List services in YAML format
kubectl get svc --show-labels
Show service labels
kubectl expose deployment [name] --port=80 --type=NodePort
Expose deployment as a service
kubectl port-forward svc/[name] 8080:80
Forward port 8080 to service's 80
kubectl delete svc [name]
Delete a service
kubectl edit svc [name]
Edit a service

Config & Secrets

kubectl get configmap
List all configmaps
kubectl get configmap --all-namespaces
List configmaps in all namespaces
kubectl get configmap --all-namespaces -o yaml
List configmaps in YAML format
kubectl describe configmap [name]
Show configmap details
kubectl create configmap [name] --from-literal=key=val
Create configmap from literal
kubectl get secret
List all secrets
kubectl get secret --all-namespaces
List secrets in all namespaces
kubectl get secret -o yaml
List secrets in YAML format
kubectl describe secret [name]
Show secret details
kubectl create secret generic [name] --from-literal=key=val
Create secret from literal
kubectl get serviceaccounts
List all service accounts
kubectl get serviceaccounts default -o yaml > ./sa.yaml
Export default service account to YAML
kubectl replace serviceaccount default -f ./sa.yaml
Replace default service account from YAML
kubectl edit sa [name]
Edit a service account
kubectl delete sa [name]
Delete a service account

Events & Logs

kubectl get events
List cluster events
kubectl get events -n kube-system
List events in kube-system namespace
kubectl get events -w
Watch events in real time
kubectl describe pod [name]
Show pod events and details
kubectl logs [pod]
Show logs for a pod
kubectl logs -f [pod]
Follow logs for a pod
kubectl logs [pod] -c [container]
Show logs for a specific container
kubectl logs --since=1h [pod]
Show pod logs from the last hour
kubectl logs --tail=20 [pod]
Show last 20 lines of pod logs
kubectl logs [pod] > pod.log
Save pod logs to a file

Resource Management

kubectl get all
List all resources in current namespace
kubectl get all --all-namespaces
List all resources in all namespaces
kubectl get svc,po
List services and pods
kubectl get deploy,no
List deployments and nodes
kubectl delete node [name]
Delete a node
kubectl delete pod [name]
Delete a pod
kubectl edit node [name]
Edit a node
kubectl edit pod [name]
Edit a pod
kubectl edit deploy [name]
Edit a deployment
kubectl delete deploy [name]
Delete a deployment
kubectl scale deploy [name] --replicas=5
Scale deployment to 5 pods
kubectl expose deploy [name] --port=80 --type=NodePort
Expose deployment as NodePort service

Labels, Taints & Annotations

kubectl label nodes <node-name> <label-key>=<label-value>
Add a label to a node
kubectl label nodes <node-name> <label-key>-
Remove a label from a node
kubectl label nodes <node-name> <label-key>=<label-value> --overwrite
Overwrite a label on a node
kubectl taint [node] [taint]
Add a taint to a node
kubectl cordon [node]
Mark node as unschedulable (cordon)
kubectl uncordon [node]
Mark node as schedulable (uncordon)
kubectl drain [node]
Drain a node (evict all pods)
kubectl annotate po [pod] [annotation]
Add annotation to a pod
kubectl annotate no [node] [annotation]
Add annotation to a node

Ingress & Storage

kubectl get ing
List all ingresses
kubectl get ing --all-namespaces
List ingresses in all namespaces
kubectl get pv
List all persistent volumes
kubectl describe pv
Show persistent volume details
kubectl get pvc
List all persistent volume claims
kubectl describe pvc
Show persistent volume claim details
kubectl get sc
List all storage classes
kubectl get sc -o yaml
List storage classes in YAML format

Resource Creation & Export

kubectl create -f [file]
Create resource from file
kubectl apply -f [file]
Apply resource from file
kubectl run [pod] --image=nginx --restart=Never
Run a pod with nginx image
kubectl create svc nodeport [name] --tcp=8080:80
Create a NodePort service
kubectl create deploy [name] --image=nginx
Create a deployment with nginx image
kubectl create deploy [name] --image=nginx --dry-run -o yaml > deploy.yaml
Generate deployment YAML without creating it
kubectl get po [name] -o yaml --export > pod.yaml
Export pod YAML to file
kubectl run nginx --image=nginx:alpine --dry-run -o yaml > deploy.yaml
Generate deployment YAML with alpine image

Interactive & Help

kubectl run [pod] --image=busybox --rm -it --restart=Never -- sh
Run an interactive shell in a pod
kubectl -h
Show kubectl help
kubectl create -h
Show help for create command
kubectl run -h
Show help for run command
kubectl explain deploy.spec
Explain deployment spec fields

API & Cluster Status

kubectl get --raw /apis/metrics.k8s.io/
Raw API call to metrics server
kubectl config
Show kubeconfig help
kubectl cluster-info
Show cluster info
kubectl get componentstatus
Show cluster component status

Command Combos

kubectl get pods -o wide
List pods with node info
kubectl get pods --sort-by=.metadata.creationTimestamp
List pods sorted by creation time
kubectl get pods -l app=[label]
List pods by label
kubectl delete pod $(kubectl get pods -o name | grep 'test')
Delete all pods with 'test' in name
kubectl get all --all-namespaces
List all resources in all namespaces
kubectl top pod
Show pod resource usage (metrics-server required)
kubectl exec -it [pod] -- env
Show environment variables in a pod
kubectl get pods -o json | jq .items[].metadata.name
List pod names using jq

Categories

  • Cluster Info

    Commands for viewing cluster information, nodes, and contexts.

  • Pods

    Commands for listing, describing, executing, and managing pods.

  • Deployments

    Commands for creating, scaling, updating, and rolling back deployments.

  • Services

    Commands for exposing, forwarding, and managing services.

  • Config & Secrets

    Commands for managing configmaps and secrets.

  • Logs & Events

    Commands for viewing logs and cluster events.

  • Command Combos

    Powerful multi-step workflows and advanced usage patterns for real-world scenarios.

Features

  • Quick search functionality
  • Organized by categories
  • Clear command descriptions
  • Common and advanced use cases covered
  • Easy to copy commands
  • Responsive design
  • Perfect for quick reference