Kubernetes Day-to-Day Operational Tasks
1. Troubleshooting Pods
- Check Status:
kubectl get pods. Look for status other than Running (e.g., CrashLoopBackOff, Pending, ImagePullBackOff).
- Describe:
kubectl describe pod <pod_name>. Check the "Events" section at the bottom for errors like scheduling failures or probe failures.
- Logs:
kubectl logs <pod_name>. Add -p to see logs from the previous instance if the pod crashed.
2. Cluster Maintenance
- Node Status:
kubectl get nodes. Ensure all nodes are Ready.
- Top Nodes/Pods:
kubectl top nodes and kubectl top pods to see resource consumption (requires Metrics Server).
- Cordon/Drain: Before maintenance on a node (e.g., OS upgrade):
kubectl cordon <node_name>: Mark node as unschedulable.
kubectl drain <node_name> --ignore-daemonsets: Evict all pods to other nodes.
3. Deployment Management
- Scale: Manually scale a deployment.
kubectl scale deployment my-app --replicas=5
- Rollout Restart: Restart all pods in a deployment (useful to pick up config changes).
kubectl rollout restart deployment my-app
- Rollback: Undo a failed deployment.
kubectl rollout undo deployment my-app
4. Context Switching
- Switch Context:
kubectl config use-context <context_name> (or use tools like kubectx) to switch between dev, staging, and prod clusters.
- Switch Namespace:
kubectl config set-context --current --namespace=<namespace> (or use kubens).