Skip to content

Kubernetes and kubectl cheat sheet

The kubectl commands and YAML for pods, deployments, services, scaling, rollouts, rollbacks, ConfigMaps and Secrets from the Kubernetes course.

Looking around

CommandWhat it does
kubectl get pods -o widepods, with node and IP
kubectl get deploy,svc,cm,secretseveral kinds at once
kubectl describe pod NAMEdetails and Events (start here when a pod fails)
kubectl logs NAME [-c container] [-f] [--previous]output, or the crashed run's
kubectl exec -it NAME -- sha shell in a pod
kubectl get events --sort-by=.lastTimestampwhat happened, in order
kubectl explain deployment.spec.strategythe docs for a field

Add -n namespace or -A (all namespaces).

Creating and changing

CommandWhat it does
kubectl apply -f app.yamlcreate or update from a file
kubectl create deployment web --image=nginx:1.27 --replicas=3quick deployment
kubectl expose deployment web --port=80 --type=NodePorta Service for it
kubectl scale deployment web --replicas=5change the count
kubectl delete -f app.yamlremove what the file made
kubectl port-forward svc/web 8080:80reach it from your machine

Rollouts

CommandWhat it does
kubectl set image deployment/web web=nginx:1.28new version
kubectl rollout status deployment/webwait for it
kubectl rollout history deployment/webrevisions
kubectl rollout undo deployment/web [--to-revision=N]go back
kubectl rollout restart deployment/webrestart every pod

A deployment and its service

apiVersion: apps/v1
kind: Deployment
metadata: { name: web }
spec:
  replicas: 3
  selector: { matchLabels: { app: web } }
  template:
    metadata: { labels: { app: web } }      # must match the selector
    spec:
      containers:
        - name: web
          image: nginx:1.27
          ports: [{ containerPort: 80 }]
          resources:
            requests: { cpu: 100m, memory: 64Mi }
            limits: { memory: 128Mi }
          readinessProbe: { httpGet: { path: /, port: 80 } }
          envFrom: [{ configMapRef: { name: web-config } }]
---
apiVersion: v1
kind: Service
metadata: { name: web }
spec:
  selector: { app: web }
  ports: [{ port: 80, targetPort: 80 }]

Paste a manifest into the Kubernetes YAML explainer to check it.

Configuration

kubectl create configmap web-config --from-literal=MODE=prod --from-file=app.conf
kubectl create secret generic db --from-literal=password='s3cret'
kubectl get secret db -o jsonpath='{.data.password}' | base64 -d

A Secret is base64, not encryption. A changed ConfigMap reaches env vars only after the pods restart.

Common pod states

Pending (no node fits: requests, taints) · ImagePullBackOff (image name or registry) · CrashLoopBackOff (the app exits: read logs --previous) · Running but not READY (readiness probe fails).

Practise it on a real machineKubernetes Foundations course
Open the lab