📋 Cheat Sheets

Kubernetes / kubectl Cheat Sheet — Commands You'll Use Daily


Click any command to expand the explanation and examples.

🔧 Context & Config

kubectl config — switch clusters config
# See current context
kubectl config current-context

List all contexts

kubectl config get-contexts

Switch context

kubectl config use-context my-cluster

Set default namespace

kubectl config set-context —current —namespace=my-app

View full config

kubectl config view

📋 Getting Info

kubectl get — list resources read
# Pods
kubectl get pods
kubectl get pods -o wide          # More columns (IP, node)
kubectl get pods -A               # All namespaces
kubectl get pods -l app=nginx     # Filter by label
kubectl get pods --field-selector status.phase=Running

Common resources

kubectl get deployments kubectl get services kubectl get nodes kubectl get namespaces kubectl get ingress kubectl get configmaps kubectl get secrets

All resources

kubectl get all

Output formats

kubectl get pods -o json kubectl get pods -o yaml kubectl get pods -o name # Just names

kubectl describe — detailed info read
# Describe a pod (events, conditions, containers)
kubectl describe pod my-pod

Describe a node

kubectl describe node my-node

Describe a service

kubectl describe svc my-service

This is your go-to for debugging. The Events section at the bottom shows what went wrong.

🚀 Creating & Updating

kubectl apply — create/update from YAML write
# Apply a manifest
kubectl apply -f deployment.yaml

Apply all YAML files in a directory

kubectl apply -f ./k8s/

Apply from URL

kubectl apply -f https://raw.githubusercontent.com/user/repo/main/deploy.yaml

Dry run (preview without applying)

kubectl apply -f deployment.yaml —dry-run=client

kubectl create — quick resource creation write
# Create a deployment
kubectl create deployment nginx --image=nginx

Create a service

kubectl expose deployment nginx —port=80 —type=LoadBalancer

Create a namespace

kubectl create namespace staging

Create a secret

kubectl create secret generic db-creds
—from-literal=username=admin
—from-literal=password=secret

Create a configmap

kubectl create configmap app-config
—from-file=config.yaml
—from-literal=LOG_LEVEL=info

kubectl scale & rollout write
# Scale replicas
kubectl scale deployment nginx --replicas=3

Autoscale

kubectl autoscale deployment nginx —min=2 —max=10 —cpu-percent=80

Update image (rolling update)

kubectl set image deployment/nginx nginx=nginx:1.25

Rollout status

kubectl rollout status deployment/nginx

Rollback

kubectl rollout undo deployment/nginx kubectl rollout undo deployment/nginx —to-revision=2

Rollout history

kubectl rollout history deployment/nginx

🔍 Debugging

kubectl logs — container logs debug
# Pod logs
kubectl logs my-pod

Follow (stream) logs

kubectl logs -f my-pod

Last 100 lines

kubectl logs —tail=100 my-pod

Logs from specific container (multi-container pod)

kubectl logs my-pod -c my-container

Logs from previous crashed container

kubectl logs my-pod —previous

Logs from all pods with a label

kubectl logs -l app=nginx —all-containers

kubectl exec — run commands in a pod debug
# Interactive shell
kubectl exec -it my-pod -- /bin/bash
kubectl exec -it my-pod -- /bin/sh    # If bash isn't available

Run a single command

kubectl exec my-pod — cat /etc/config/app.yaml kubectl exec my-pod — env kubectl exec my-pod — curl localhost:8080/health

Specific container

kubectl exec -it my-pod -c sidecar — /bin/sh

kubectl port-forward — access pods locally debug
# Forward pod port to localhost
kubectl port-forward my-pod 8080:80

Forward service port

kubectl port-forward svc/my-service 8080:80

Background it

kubectl port-forward my-pod 8080:80 &

kubectl top — resource usage debug
# Pod CPU/memory usage (requires metrics-server)
kubectl top pods
kubectl top pods --sort-by=memory

Node usage

kubectl top nodes

🗑️ Deleting

kubectl delete delete
# Delete by name
kubectl delete pod my-pod
kubectl delete deployment nginx
kubectl delete svc my-service

Delete from YAML

kubectl delete -f deployment.yaml

Delete all pods in namespace

kubectl delete pods —all

Force delete stuck pod

kubectl delete pod my-pod —grace-period=0 —force

Delete namespace (deletes everything in it!)

kubectl delete namespace staging

⚡ Shortcuts & Tips

Aliases and short names tips
# Short resource names
po    = pods
deploy = deployments
svc   = services
ns    = namespaces
no    = nodes
cm    = configmaps
ing   = ingress
pv    = persistentvolumes
pvc   = persistentvolumeclaims

Example

kubectl get po kubectl get deploy kubectl get svc

Recommended alias

alias k=kubectl

JSONPath queries tips
# Get pod IPs
kubectl get pods -o jsonpath='{.items[*].status.podIP}'

Get node names

kubectl get nodes -o jsonpath=‘{.items[*].metadata.name}‘

Get image for each pod

kubectl get pods -o jsonpath=‘{range .items[*]}{.metadata.name}{“\t”}{.spec.containers[0].image}{“\n”}{end}’