Contents

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 wide reveals the nodes where the pods are running from and ip details for the pods.
      • kubectl describe pod pod-name reveals 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/1 or 1/2 indicates running containers / total containers; values like 0/1 signal container failures
  • kubectl describe – Detailed resource inspection

    • kubectl 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 editor
    • kubectl edit deployment my-deploy

Remove Resources

  • kubectl delete – Delete by name or file
    • kubectl delete pod my-pod
    • kubectl delete deployment hello-node
    • kubectl delete service hello-node
    • File-based: kubectl delete -f deployment.yaml

Debugging & Container Access

  • kubectl logs – Retrieve container logs

    • kubectl logs my-pod -c my-container
    • kubectl logs webpod
  • kubectl exec – Execute commands inside containers

    • kubectl exec -it my-pod -- bash
  • kubectl port-forward – Forward local port to pod/service

    • kubectl port-forward svc/my-service 8080:80
  • kubectl top – Display resource utilization metrics

    • kubectl top nodes
  • kubectl cp – Copy files between local and containers

    • kubectl cp ./local.txt my-pod:/remote.txt

Deployment & Rollout Management

  • kubectl rollout status – Monitor rollout progress

    • kubectl rollout status deployment/my-deploy
  • kubectl rollout history – View revision history

    • kubectl rollout history deployment/my-deploy
  • kubectl rollout undo – Rollback to previous revision

    • kubectl rollout undo deployment/my-deploy

Scaling & Service Exposure

  • kubectl scale – Manual replica scaling

    • kubectl scale deployment my-deploy --replicas=3
  • kubectl autoscale – Configure horizontal pod autoscaling

    • kubectl 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
  • ‘kubectl get rc (replicacontroller) - lists all replica controllers created

Metadata & Object Updates

  • kubectl annotate – Add or update annotations

    • kubectl annotate pod my-pod description="test pod"
  • kubectl label – Add or update labels

    • kubectl label pod my-pod env=prod
  • kubectl patch – Partial object updates

    • kubectl patch deployment my-deploy -p '{"spec":{"replicas":4}}'
  • kubectl set image – Update container image

    • kubectl set image deployment/my-deploy container-name=new-image:tag
  • kubectl set resources – Configure resource limits/requests

    • kubectl set resources deployment/my-deploy -c=container-name --limits=cpu=200m,memory=512Mi

Configuration & Context Management

  • kubectl config view – Display kubeconfig file contents
  • kubectl config get-contexts – List available contexts
  • kubectl config set-context – Modify context attributes
  • kubectl config use-context – Switch active context

Cluster Security & Information

  • kubectl auth can-i – Verify RBAC permissions

    • kubectl 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 nodes

    • kubectl get nodes -o wide (includes OS image, kernel version, container runtime)
  • kubectl cordon – Mark node unschedulable

    • kubectl cordon node-name
  • kubectl uncordon – Mark node schedulable

    • kubectl 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 events
  • kubectl apply -k – Apply kustomization directories
  • kubectl explain – Interactive API resource documentation
    • kubectl explain pod.spec.containers
  • kubectl proxy – Start API server proxy
  • kubectl wait – Block until resource meets condition
    • kubectl 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 apply runs, 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 yaml before applying
  • Container status ratios in kubectl get pods indicate deployment health

Your inbox needs more DevOps articles.

Subscribe to get our latest content by email.