Kubernetes Commands Cheat Sheet for Everyday Cluster Tasks
Essential kubectl commands for viewing, debugging, deploying, scaling, and managing your Kubernetes workloads
Website Visitors:Kubernetes CLI Reference
Core Resource Management
View Resources
-
kubectl get– List resources- Basic:
kubectl get pods -n default - Wide output:
kubectl get pods nginxpod -o wide(shows node placement, pod IP) - Label selector:
kubectl get pods -l app=k8s-web-app - Node details:
kubectl get pods -o widereveals the nodes where the pods are running from and ip details for the pods.kubectl describe pod pod-namereveals image used, node details, number of containers deployed, find why a pod launch failed (under events section) and other details
- Container status: Output like
1/1or1/2indicates running containers / total containers; values like0/1signal container failures
- Basic:
-
kubectl describe– Detailed resource inspectionkubectl describe pod my-pod(includes events section for failure diagnostics)- kubectl describe deployment
Create Resources
-
kubectl create– Imperative creation from file or stdin (fails if resource exists)kubectl create -f deployment.yaml- Imperative deployment:
kubectl create deployment nginx --image=nginx - Custom command:
kubectl create deployment hello-node --image=registry.k8s.io/e2e-test-images/agnhost:2.53 -- /agnhost netexec --http-port=8080
-
kubectl apply– Declarative configuration (creates or updates)kubectl apply -f service.yaml- Update image: Modify image name in YAML, then
kubectl apply -f pod.yaml
-
kubectl run– Direct pod creation (deprecated for deployments)kubectl run nginx --image=nginx(as of v1.18+, creates pod only; not a deployment)- Dry-run YAML generation:
kubectl run redis --image=redis --dry-run=client -o yaml
Modify Resources
kubectl edit– Live resource editing in default editorkubectl edit deployment my-deploy
Remove Resources
kubectl delete– Delete by name or filekubectl delete pod my-podkubectl delete deployment hello-nodekubectl delete service hello-node- File-based:
kubectl delete -f deployment.yaml
Debugging & Container Access
-
kubectl logs– Retrieve container logskubectl logs my-pod -c my-containerkubectl logs webpod
-
kubectl exec– Execute commands inside containerskubectl exec -it my-pod -- bash
-
kubectl port-forward– Forward local port to pod/servicekubectl port-forward svc/my-service 8080:80
-
kubectl top– Display resource utilization metricskubectl top nodes
-
kubectl cp– Copy files between local and containerskubectl cp ./local.txt my-pod:/remote.txt
Deployment & Rollout Management
-
kubectl rollout status– Monitor rollout progresskubectl rollout status deployment/my-deploy
-
kubectl rollout history– View revision historykubectl rollout history deployment/my-deploy
-
kubectl rollout undo– Rollback to previous revisionkubectl rollout undo deployment/my-deploy
Scaling & Service Exposure
-
kubectl scale– Manual replica scalingkubectl scale deployment my-deploy --replicas=3
-
kubectl autoscale– Configure horizontal pod autoscalingkubectl autoscale deployment my-deploy --min=2 --max=5 --cpu-percent=80
-
kubectl expose– Create service for pods or deployments- Expose deployment:
kubectl expose deployment my-deploy --port=80 --type=LoadBalancer - Expose pod:
kubectl expose pod webpod --type=NodePort --port=80 - NodePort example:
kubectl expose deployment hello-node --type=NodePort --port=8080 --target-port=8080
- Expose deployment:
-
‘kubectl get rc (replicacontroller) - lists all replica controllers created
Metadata & Object Updates
-
kubectl annotate– Add or update annotationskubectl annotate pod my-pod description="test pod"
-
kubectl label– Add or update labelskubectl label pod my-pod env=prod
-
kubectl patch– Partial object updateskubectl patch deployment my-deploy -p '{"spec":{"replicas":4}}'
-
kubectl set image– Update container imagekubectl set image deployment/my-deploy container-name=new-image:tag
-
kubectl set resources– Configure resource limits/requestskubectl set resources deployment/my-deploy -c=container-name --limits=cpu=200m,memory=512Mi
Configuration & Context Management
kubectl config view– Display kubeconfig file contentskubectl config get-contexts– List available contextskubectl config set-context– Modify context attributeskubectl config use-context– Switch active context
Cluster Security & Information
-
kubectl auth can-i– Verify RBAC permissionskubectl auth can-i create deployments --namespace=production
-
kubectl version– Display client and server version information -
kubectl cluster-info– Show cluster endpoint addresses
Node Operations
-
kubectl get nodes– List cluster nodeskubectl get nodes -o wide(includes OS image, kernel version, container runtime)
-
kubectl cordon– Mark node unschedulablekubectl cordon node-name
-
kubectl uncordon– Mark node schedulablekubectl uncordon node-name
-
kubectl drain– Evacuate node (reschedules pods, marks unschedulable)kubectl drain node-name --ignore-daemonsets
Advanced Utilities
kubectl get events– View recent cluster eventskubectl apply -k– Apply kustomization directorieskubectl explain– Interactive API resource documentationkubectl explain pod.spec.containers
kubectl proxy– Start API server proxykubectl wait– Block until resource meets conditionkubectl wait --for=condition=Ready pod/my-pod --timeout=60s
ReplicaController And ReplicaSet
kubectl create -f rs-deploy.yaml - creates the rs
kubectl get replicaset - list of rs created
kubectl delete replicaset myrsap - delete the rs. you can also specify multiple resources in the delete command.
kubectl replace -f rs-deploy.yaml - replace or update the rs
kubectl scale --replicas=4 -f rs-deploy.yaml update the replicaset count
kubectl get replicaset - list all replicasets
kubectl get replicationcontroller - lists all replicacontoller
kubectl describe replicaset rs-deploy.yaml - or kubectl describe replicaset myapp-rs - get more information about replicaset
kubectl describe pod podname - To get full details about pod. If it is crashed, it has the details why it crashed and image details.
kubectl explain resourcname - Explains the resource name given. Ex: kubectl explain rc explains about replicacontroller.
kubectl get all - lists all resources including pods, rs, rc and other resources created on that machine.
Deployments Rollback and update:
Create:
kubectl create -f deployment-definition.yml
Get:
kubectl get deployments
Update:
kubectl apply -f deployment-definition.yml
kubectl set image deployment/myapp-deployment nginx=nginx:1.9.1
Status:
kubectl rollout status deployment/myapp-deployment
kubectl rollout history deployment/myapp-deployment
Rollback:
kubectl rollout undo deployment/myapp-deployment
Resource Type Shortcuts
| Shortcut | Resource Type |
|---|---|
crd |
CustomResourceDefinition |
svc |
Service |
ingress |
Ingress |
pvc / pv |
PersistentVolumeClaim / PersistentVolume |
rs / ds |
ReplicaSet / DaemonSet |
statefulset |
StatefulSet |
jobs / cronjobs |
Job / CronJob |
hpa |
HorizontalPodAutoscaler |
configmap / secret |
ConfigMap / Secret |
role / clusterrole |
Role / ClusterRole |
rolebinding / clusterrolebinding |
RoleBinding / ClusterRoleBinding |
Imperative vs Declarative Command Behavior
kubectl create
- Creates resource only if it does not exist; fails on duplicate
- One-time imperative operation - because you tell Kubernetes “create this object now,” and it does exactly that once, without remembering that spec for future updates
- Does not track configuration changes for future updates
kubectl apply
- Creates resource if absent, or updates to match provided configuration
- Declarative workflow with last-applied-configuration tracking: it stores a “last-applied-configuration” annotation and uses that to calculate changes on subsequent
kubectl applyruns, making it idempotent and suitable for iterative updates from the same manifest file. - Idempotent; supports iterative updates
Pro Tips
- Use
kubectl explain <resource>.<field>for interactive API documentation - Inspect pod events via
kubectl describe pod <name>to diagnose startup failures - Generate YAML manifests with
--dry-run=client -o yamlbefore applying - Container status ratios in
kubectl get podsindicate deployment health
Your inbox needs more DevOps articles.
Subscribe to get our latest content by email.
