Kubernetes Cheatsheet

kubectl commands, pods, services, deployments & config

DevOps
Contents
⚙️

kubectl Basics

kubectl version --short
kubectl cluster-info
kubectl get nodes
kubectl api-resources          # list all resource types

# Context & config
kubectl config get-contexts
kubectl config use-context my-cluster
kubectl config current-context

# Output formats
kubectl get pods -o wide       # more columns
kubectl get pods -o yaml       # YAML output
kubectl get pods -o json       # JSON output
kubectl get pods --sort-by=.metadata.name
📦

Pods

# List pods
kubectl get pods
kubectl get pods -A                     # all namespaces
kubectl get pods -l app=web             # by label

# Run a pod
kubectl run nginx --image=nginx
kubectl run -it busybox --image=busybox -- sh

# Pod lifecycle
kubectl describe pod <name>
kubectl logs <pod>                     # stdout logs
kubectl logs <pod> -c <container>      # specific container
kubectl logs -f <pod>                  # follow
kubectl exec -it <pod> -- /bin/bash    # shell into pod
kubectl port-forward <pod> 8080:80
kubectl delete pod <name>
kubectl cp <pod>:/path ./local-path
🚀

Deployments

# Create
kubectl create deployment web --image=nginx --replicas=3

# Manage
kubectl get deployments
kubectl describe deployment web
kubectl scale deployment web --replicas=5
kubectl set image deployment/web nginx=nginx:1.25

# Rollout
kubectl rollout status deployment/web
kubectl rollout history deployment/web
kubectl rollout undo deployment/web
kubectl rollout restart deployment/web

# Autoscale
kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=80
🌐

Services

# Expose deployment
kubectl expose deployment web --port=80 --type=ClusterIP
kubectl expose deployment web --port=80 --type=NodePort
kubectl expose deployment web --port=80 --type=LoadBalancer

kubectl get services
kubectl describe service web
kubectl delete service web

# Service types
# ClusterIP    — internal only (default)
# NodePort     — expose on node IP:port
# LoadBalancer — external load balancer
# ExternalName — CNAME alias
🔐

ConfigMaps & Secrets

# ConfigMap
kubectl create configmap myconfig --from-literal=KEY=value
kubectl create configmap myconfig --from-file=config.yaml
kubectl get configmaps
kubectl describe configmap myconfig

# Secrets
kubectl create secret generic mysecret --from-literal=pass=s3cret
kubectl get secrets
kubectl describe secret mysecret

# Use in pod
envFrom:
  - configMapRef:
      name: myconfig
  - secretRef:
      name: mysecret
📂

Namespaces

kubectl get namespaces
kubectl create namespace staging
kubectl delete namespace staging

# Work in a namespace
kubectl get pods -n staging
kubectl apply -f app.yaml -n staging

# Set default namespace
kubectl config set-context --current --namespace=staging
🔧

Debugging

kubectl get events --sort-by=.metadata.creationTimestamp
kubectl top nodes              # resource usage
kubectl top pods
kubectl describe pod <name>    # check Events section
kubectl logs <pod> --previous  # crashed container logs

# Debug container
kubectl debug -it <pod> --image=busybox

# Dry run
kubectl apply -f app.yaml --dry-run=client
kubectl diff -f app.yaml
📄

YAML Templates

# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:1.25
        ports:
        - containerPort: 80

# Apply / Delete
kubectl apply -f deployment.yaml
kubectl delete -f deployment.yaml