Skip to content
Codeloom

Cheat Sheets

Kubernetes Cheat Sheet

Essential kubectl commands, pod specs, deployments, services, configmaps, secrets, and debugging patterns in one quick-reference page.

7 sections 22 snippets

Cluster Info

Cluster and context
kubectl cluster-info
kubectl config current-context
kubectl config get-contexts
kubectl config use-context my-cluster
Namespaces
kubectl get namespaces
kubectl create namespace staging
kubectl config set-context --current --namespace=staging

Pods

List and describe
kubectl get pods
kubectl get pods -o wide
kubectl get pods -A               # all namespaces
kubectl describe pod my-pod
Run a pod
kubectl run nginx --image=nginx:alpine
kubectl run debug --image=busybox -it --rm -- sh
Logs
kubectl logs my-pod
kubectl logs -f my-pod              # follow
kubectl logs my-pod -c sidecar      # specific container
kubectl logs --previous my-pod      # crashed container
Exec into pod
kubectl exec -it my-pod -- sh
kubectl exec my-pod -- cat /etc/config
Port forward
kubectl port-forward pod/my-pod 8080:80
kubectl port-forward svc/my-svc 3000:80

Deployments

Create and manage
kubectl create deployment web --image=nginx:alpine --replicas=3
kubectl get deployments
kubectl describe deployment web
Scale
kubectl scale deployment web --replicas=5
Update image
kubectl set image deployment/web nginx=nginx:1.25
kubectl rollout status deployment/web
Rollback
kubectl rollout undo deployment/web
kubectl rollout history deployment/web

Services

Expose a deployment
kubectl expose deployment web --port=80 --type=ClusterIP
kubectl expose deployment web --port=80 --type=LoadBalancer
List and describe
kubectl get svc
kubectl describe svc web
kubectl get endpoints web

ConfigMaps and Secrets

ConfigMap
kubectl create configmap app-config --from-literal=DB_HOST=db
kubectl create configmap app-config --from-file=config.yaml
kubectl get configmap app-config -o yaml
Secrets
kubectl create secret generic db-creds \
  --from-literal=username=admin \
  --from-literal=password=s3cret
kubectl get secret db-creds -o jsonpath='{.data.password}' | base64 -d

Apply and Delete

Apply manifests
kubectl apply -f deployment.yaml
kubectl apply -f k8s/              # entire directory
kubectl apply -f https://raw.githubusercontent.com/.../manifest.yaml
Delete resources
kubectl delete pod my-pod
kubectl delete -f deployment.yaml
kubectl delete deployment web
Dry run and diff
kubectl apply -f deploy.yaml --dry-run=client
kubectl diff -f deploy.yaml

Debugging

Get events
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl get events --field-selector reason=Failed
Resource usage
kubectl top nodes
kubectl top pods
kubectl top pods --sort-by=memory
Debug container
kubectl debug my-pod -it --image=busybox --target=app
Check DNS
kubectl run dns-test --image=busybox:1.36 -it --rm -- nslookup my-svc