| name | k8s-ops |
| description | Kubernetes cluster operations, deployments, and troubleshooting. Use when deploying manifests, checking rollout status, monitoring pods, debugging failures, viewing logs, managing namespaces, or any kubectl operations. Triggers on mentions of kubernetes, k8s, kubectl, pods, deployments, services, namespaces, or cluster management. |
Kubernetes Operations
Core Workflow
Deployment Lifecycle
kubectl apply --dry-run=server -f <manifest> -n <namespace>
kubectl apply -f <manifest> -n <namespace>
kubectl rollout status deployment/<name> -n <namespace> --timeout=300s
kubectl get pods -n <namespace> -l app=<label> -o wide
kubectl get events -n <namespace> --sort-by='.lastTimestamp' | tail -20
Quick Health Check
kubectl cluster-info
kubectl get nodes -o wide
kubectl top nodes
kubectl get all -n <namespace>
kubectl get pods -n <namespace> -o wide
kubectl top pods -n <namespace>
Troubleshooting Decision Tree
Pod Not Starting
- Check pod status:
kubectl get pods -n <ns> -o wide
- Describe for events:
kubectl describe pod <pod> -n <ns>
- Check logs:
kubectl logs <pod> -n <ns> --previous (if crashed)
Common causes:
ImagePullBackOff: Wrong image name/tag, missing imagePullSecrets
CrashLoopBackOff: App crash - check logs, health probes too aggressive
Pending: Insufficient resources, node selector/affinity issues
ContainerCreating: Volume mount issues, init container stuck
Pod Running But Not Receiving Traffic
- Check readiness:
kubectl get pods -n <ns> (READY column)
- Check endpoints:
kubectl get endpoints <service> -n <ns>
- Check service selector:
kubectl describe service <svc> -n <ns>
- Test connectivity:
kubectl run debug --rm -it --image=busybox -- wget -qO- <service>:<port>
High Restart Count
kubectl get pods -n <ns> -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.containerStatuses[0].restartCount}{"\n"}{end}'
kubectl get pod <pod> -n <ns> -o jsonpath='{.status.containerStatuses[0].lastState.terminated}'
kubectl get pod <pod> -n <ns> -o jsonpath='{.spec.containers[0].livenessProbe}'
Common Operations
Logs
kubectl logs <pod> -n <ns>
kubectl logs <pod> -n <ns> -c <container>
kubectl logs <pod> -n <ns> --previous
kubectl logs <pod> -n <ns> -f
kubectl logs -l app=<label> -n <ns> --all-containers
kubectl logs <pod> -n <ns> --since=1h
kubectl logs <pod> -n <ns> --since-time="2024-01-01T00:00:00Z"
Exec/Debug
kubectl exec -it <pod> -n <ns> -- /bin/sh
kubectl exec -it <pod> -n <ns> -c <container> -- /bin/bash
kubectl exec <pod> -n <ns> -- <command>
kubectl debug -it <pod> -n <ns> --image=busybox --target=<container>
Scaling
kubectl scale deployment/<name> -n <ns> --replicas=3
kubectl autoscale deployment/<name> -n <ns> --min=2 --max=10 --cpu-percent=80
kubectl get hpa -n <ns>
Rollback
kubectl rollout history deployment/<name> -n <ns>
kubectl rollout undo deployment/<name> -n <ns>
kubectl rollout undo deployment/<name> -n <ns> --to-revision=<N>
kubectl rollout pause deployment/<name> -n <ns>
kubectl rollout resume deployment/<name> -n <ns>
Resource Management
kubectl top pods -n <ns> --sort-by=memory
kubectl top pods -n <ns> --sort-by=cpu
kubectl describe limitrange -n <ns>
kubectl describe resourcequota -n <ns>
kubectl get pods -n <ns> -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].resources}{"\n"}{end}'
Context & Namespace Management
kubectl config get-contexts
kubectl config current-context
kubectl config use-context <context-name>
kubectl config set-context --current --namespace=<ns>
kubectl create namespace <name>
Output Formats
kubectl get pods -o wide
kubectl get deployment <name> -o yaml
kubectl get pod <name> -o json
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,IP:.status.podIP
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get secret <name> -o jsonpath='{.data.password}' | base64 -d
Port Forwarding
kubectl port-forward pod/<name> <local>:<remote> -n <ns>
kubectl port-forward svc/<name> <local>:<remote> -n <ns>
kubectl port-forward deployment/<name> <local>:<remote> -n <ns>
Labels & Selectors
kubectl label pods <pod> env=prod -n <ns>
kubectl label pods <pod> env- -n <ns>
kubectl get pods -l app=nginx,env=prod -n <ns>
kubectl get pods -l 'env in (prod,staging)' -n <ns>
kubectl delete pods -l app=test -n <ns>
Resource Cleanup
kubectl delete -f <manifest> -n <ns>
kubectl delete pods -l app=<label> -n <ns>
kubectl delete pod <pod> -n <ns> --grace-period=0 --force
kubectl delete pods -n <ns> --field-selector=status.phase=Succeeded
kubectl delete pods -n <ns> --field-selector=status.phase=Failed
Health Probes Reference
Probe Types
- Liveness: Is container alive? Failure → restart
- Readiness: Can container serve traffic? Failure → remove from endpoints
- Startup: Has app started? Blocks liveness/readiness until success
Debugging Probes
kubectl get pod <pod> -n <ns> -o yaml | grep -A10 livenessProbe
kubectl exec <pod> -n <ns> -- wget -qO- localhost:<port>/healthz
kubectl describe pod <pod> -n <ns> | grep -A5 "Liveness\|Readiness"
Tips
- Always use
-n <namespace> explicitly to avoid mistakes
- Use
--dry-run=client -o yaml to generate manifests
- Add
--watch to continuously monitor: kubectl get pods -w
- Use
kubectl explain <resource>.<field> to understand spec fields
- Annotate changes:
kubectl annotate deployment/<name> kubernetes.io/change-cause="<reason>"