Skip to main content
Reference

kubectl cheatsheet.

The kubectl commands you reach for most, grouped by what you’re trying to do. Click any command to copy it.

Cluster

  • kubectl cluster-info
    Show the master and DNS endpoint
  • kubectl version --client
    Print the kubectl client version
  • kubectl config get-contexts
    List all known kubeconfig contexts
  • kubectl config use-context <name>
    Switch the active context
  • kubectl config current-context
    Print the active context
  • kubectl api-resources
    List every resource type the cluster supports

Pods

  • kubectl get pods
    List Pods in the current namespace
  • kubectl get pods -A
    List Pods across every namespace
  • kubectl get pod <name> -o yaml
    Print the full Pod manifest
  • kubectl describe pod <name>
    Show events and detail for a Pod
  • kubectl delete pod <name>
    Delete a Pod (controller will recreate)
  • kubectl get pods --field-selector status.phase=Pending
    Show only Pending Pods

Deployments

  • kubectl get deploy
    List Deployments
  • kubectl create deploy nginx --image=nginx:1.27
    Create a Deployment from an image
  • kubectl rollout status deploy/<name>
    Watch a rollout to completion
  • kubectl rollout undo deploy/<name>
    Roll back to the previous revision
  • kubectl scale deploy/<name> --replicas=5
    Set the replica count
  • kubectl set image deploy/<name> <container>=<image>
    Update a container image

Services & networking

  • kubectl get svc
    List Services
  • kubectl expose deploy/<name> --port=80
    Create a ClusterIP Service for a Deployment
  • kubectl port-forward svc/<name> 8080:80
    Forward a local port to a Service
  • kubectl get ingress
    List Ingress resources
  • kubectl get endpoints
    Show which Pods back each Service

Config & secrets

  • kubectl get configmap
    List ConfigMaps
  • kubectl create configmap app --from-file=./config.yaml
    Create a ConfigMap from a file
  • kubectl get secret
    List Secrets
  • kubectl create secret generic db --from-literal=password=…
    Create a Secret from a literal
  • kubectl get secret db -o jsonpath='{.data.password}' | base64 -d
    Decode a Secret value

Logs & exec

  • kubectl logs <pod>
    Print logs for the first container in a Pod
  • kubectl logs <pod> -c <container>
    Logs for a specific container
  • kubectl logs -f <pod>
    Follow logs (tail -f)
  • kubectl logs --previous <pod>
    Logs from the previous container instance
  • kubectl exec -it <pod> -- sh
    Open a shell inside a Pod
  • kubectl cp <pod>:/path ./local
    Copy a file from a Pod

Debug & inspect

When something is wrong, these are the commands you reach for first.

  • kubectl get events --sort-by=.lastTimestamp
    Recent cluster events
  • kubectl top pod
    CPU/memory per Pod (requires metrics-server)
  • kubectl top node
    CPU/memory per Node
  • kubectl debug node/<node> -it --image=ubuntu
    Open a debug Pod on a Node
  • kubectl debug pod/<pod> -it --image=busybox --share-processes
    Attach an ephemeral container to a Pod
  • kubectl auth can-i create deploy
    Check whether the current user can perform an action

Apply, diff, kustomize

  • kubectl apply -f ./manifests/
    Apply every YAML in a directory
  • kubectl apply -k ./overlays/prod
    Apply a Kustomize overlay
  • kubectl diff -f deploy.yaml
    Show what would change before applying
  • kubectl delete -f ./manifests/
    Delete every resource defined in a directory
  • kubectl wait --for=condition=Ready pod/<name>
    Wait for a condition to be true