Kubernetes basics cheatsheet.
Core concepts
- Pod: smallest unit; 1+ containers sharing network + storage.
- Deployment: declares desired replicas of pods.
- Service: stable network endpoint for pods.
- Namespace: virtual cluster, isolation.
- ConfigMap / Secret: configuration + secrets.
- Node: a VM/host that runs pods.
kubectl basics
kubectl version
kubectl cluster-info
kubectl get nodes
kubectl get pods
kubectl get pods -A # all namespaces
kubectl get all
kubectl get all -n myns
kubectl describe pod mypod
kubectl logs mypod
kubectl logs -f mypod
kubectl logs mypod -c container2 # specific container
kubectl logs --previous mypod # previous container instance
kubectl exec -it mypod -- bash
kubectl exec mypod -- ls /
Contexts
kubectl config get-contexts
kubectl config use-context production
kubectl config current-context
# Better: kubectx + kubens
brew install kubectx
kubectx prod # switch context
kubens myapp # switch namespace
Namespaces
kubectl get ns
kubectl create namespace myapp
kubectl delete namespace myapp
# Set default namespace
kubectl config set-context --current --namespace=myapp
kubectl get pods -n kube-system
Pods
kubectl run web --image=nginx
kubectl run web --image=nginx --port=80 --restart=Never
kubectl run debug --image=alpine --rm -it -- sh
kubectl get pods -o wide # more info
kubectl get pod web -o yaml
kubectl get pod web -o json | jq
kubectl delete pod web
kubectl delete pod web --grace-period=0 --force
Apply YAML
kubectl apply -f deployment.yaml
kubectl apply -f . # all yamls in dir
kubectl apply -k overlays/prod # kustomize
kubectl apply --dry-run=client -f x.yaml
kubectl diff -f x.yaml
kubectl delete -f deployment.yaml
Minimal Pod YAML
apiVersion: v1
kind: Pod
metadata:
name: web
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
Labels + selectors
kubectl get pods -l app=web
kubectl get pods -l 'env in (prod, staging)'
kubectl get pods -l 'app=web,env!=dev'
kubectl label pod web team=alpha
kubectl label pod web team- # remove label
Port-forward
kubectl port-forward pod/web 8080:80
kubectl port-forward service/web 8080:80
kubectl port-forward deployment/web 8080:80
Access pod from your machine.
Copy files
kubectl cp local.txt mypod:/tmp/
kubectl cp mypod:/tmp/log.txt local.log
Watch / wait
kubectl get pods -w # watch
kubectl wait --for=condition=ready pod/web --timeout=60s
kubectl rollout status deployment/web
Events
kubectl get events --sort-by=.lastTimestamp
kubectl get events --field-selector type=Warning
kubectl describe pod web # events at bottom
Resource short names
po = pods
svc = services
deploy = deployments
rs = replicasets
ns = namespaces
cm = configmaps
sec = secrets
ing = ingresses
pvc = persistentvolumeclaims
pv = persistentvolumes
sa = serviceaccounts
kubectl get po
kubectl get svc
Output formats
kubectl get pods -o yaml
kubectl get pods -o json
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
kubectl get pods -o wide
Aliases (recommended)
alias k=kubectl
alias kg='kubectl get'
alias kd='kubectl describe'
alias kgp='kubectl get pods'
alias kdp='kubectl describe pod'
alias kl='kubectl logs -f'
alias kx='kubectl exec -it'
Auto-completion
# bash
source <(kubectl completion bash)
# zsh
source <(kubectl completion zsh)
Common pod statuses
Pending: scheduled, not started (image pull, resource wait).Running: at least one container running.Succeeded: all containers exited 0 (usually Jobs).Failed: at least one container failed.Unknown: communication lost with node.CrashLoopBackOff: container keeps crashing.ImagePullBackOff: can’t pull image.
Common mistakes
- Wrong context → applying to wrong cluster.
- Default namespace confusion — set per-context.
kubectl deleteon wrong resource — no undo.- Deleting with
--force --grace-period=0instead of debugging. - Long log streams without
--tail.
Read this next
If you want my kubectl + kubectx setup, it’s at rajpoot.dev .
Building something AI-, backend-, or data-heavy and want a second pair of eyes? I do consulting and freelance work — see my projects and ways to reach me at rajpoot.dev .