iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Cheatsheet

Kubernetes cheatsheet: kubectl essentials, manifests, troubleshooting, the one-page reference.

Kubernetes — cheatsheet

EXAMPLE
# ===== Contexts + namespaces =====
kubectl config get-contexts
kubectl config use-context prod
kubectl config set-context --current --namespace=apps

# ===== Get =====
kubectl get pods
kubectl get pods -A                  # all namespaces
kubectl get pods -l app=shop          # by label
kubectl get pods -o wide              # IPs + nodes
kubectl get pods -o yaml              # full YAML
kubectl get all                       # everything in current ns

# ===== Describe + events =====
kubectl describe pod my-pod
kubectl get events --sort-by=.metadata.creationTimestamp -A | tail -20

# ===== Logs =====
kubectl logs my-pod
kubectl logs my-pod -c api            # specific container
kubectl logs -f my-pod                # follow
kubectl logs --previous my-pod        # crashed previous container
kubectl logs -l app=shop --tail=100

# ===== Exec =====
kubectl exec -it my-pod -- sh
kubectl exec my-pod -- env

# ===== Apply / delete =====
kubectl apply -f deploy.yaml
kubectl apply -f ./k8s/                # folder
kubectl delete -f deploy.yaml
kubectl delete pod my-pod
kubectl diff -f deploy.yaml            # preview changes

# ===== Rollout =====
kubectl rollout status deploy/shop
kubectl rollout history deploy/shop
kubectl rollout undo deploy/shop
kubectl rollout restart deploy/shop

# ===== Scale =====
kubectl scale deploy/shop --replicas=5
kubectl autoscale deploy/shop --min=2 --max=10 --cpu-percent=75

# ===== Port forward =====
kubectl port-forward svc/shop 8080:80

# ===== Resource basics =====
# Deployment + Service + Ingress = HTTP app
# StatefulSet = ordered, persistent identity
# DaemonSet = one pod per node (loggers, agents)
# Job + CronJob = one-shot / scheduled
# ConfigMap = non-secret config
# Secret = sensitive config
# PersistentVolumeClaim = storage

# ===== Manifest skeleton =====
apiVersion: apps/v1
kind: Deployment
metadata: { name: app }
spec:
  replicas: 3
  selector: { matchLabels: { app: x } }
  template:
    metadata: { labels: { app: x } }
    spec:
      containers:
        - name: app
          image: ghcr.io/me/app:v1
          ports: [{ containerPort: 3000 }]
          resources:
            requests: { cpu: '100m', memory: '128Mi' }
            limits: { cpu: '500m', memory: '512Mi' }
          readinessProbe: { httpGet: { path: /healthz, port: 3000 } }
          livenessProbe: { httpGet: { path: /healthz, port: 3000 } }

# ===== Troubleshooting common errors =====
# ImagePullBackOff -> wrong image or registry auth
# CrashLoopBackOff -> app crashes; check logs --previous
# Pending -> insufficient resources / PVC unbound / taints
# 503 from Service -> selector mismatch or readiness probe failing
# OOMKilled -> memory limit too low

# ===== Aliases =====
alias k=kubectl
alias kgp='kubectl get pods'
alias kgs='kubectl get svc'
alias kd='kubectl describe'
alias kex='kubectl exec -it'

# ===== Tools =====
kubectx       fast context switching
kubens        fast namespace switching
stern         multi-pod log tailing
k9s           terminal UI dashboard
helm          package manager

Why it matters

Kubernetes cheatsheet: contexts + namespaces, get / describe / logs / exec, apply / diff / rollout / scale. Resource skeleton with probes + resources. Pin this during ops work and most daily tasks come together quickly.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
kubectl get pods
kubectl describe pod NAME
kubectl logs NAME
kubectl exec -it NAME -- sh
kubectl apply -f file.yaml
Try it Yourself »

Discussion

Loading…