When to Use
- Cluster health verification
- Resource deployment and management
- Troubleshooting pod issues
- Viewing logs and events
Prerequisites
- kubectl installed and configured
- KUBECONFIG set to valid config
- kubelogin for Azure AD authentication
- Appropriate RBAC permissions
Commands
Cluster Health
# Cluster info
kubectl cluster-info
# Node status
kubectl get nodes -o wide
# System pods
kubectl get pods -n kube-system
# Unhealthy pods across all namespaces
kubectl get pods -A --field-selector=status.phase!=Running,status.phase!=Succeeded
Resource Management
# Dry-run before apply
kubectl apply -f manifest.yaml --dry-run=client -o yaml
# Diff changes
kubectl diff -f manifest.yaml
# Apply with recording
kubectl apply -f manifest.yaml --record
# Delete with grace
kubectl delete -f manifest.yaml --grace-period=30
Troubleshooting
# Describe pod
kubectl describe pod <pod-name> -n <namespace>
# Pod logs (current)
kubectl logs -f <pod-name> -n <namespace>
# Pod logs (previous crash)
kubectl logs <pod-name> -n <namespace> --previous
# Events by time
kubectl get events -n <namespace> --sort-by='.lastTimestamp'
# Exec into pod
kubectl exec -it <pod-name> -n <namespace> -- /bin/sh
Resource Queries
# Get all resources in namespace
kubectl get all -n <namespace> -o wide
# Get resource as YAML
kubectl get deployment <name> -n <namespace> -o yaml
# Resource usage
kubectl top pods -n <namespace>
kubectl top nodes
Best Practices
- ALWAYS use --dry-run=client before apply
- ALWAYS specify namespace with -n
- Use labels for selection: -l app=myapp
- Check events when pods fail
- Use kubectl diff for change preview
- NEVER delete without explicit namespace
Output Format
- Command executed
- Resource status summary
- Any warnings or errors
- Recommended actions
Integration with Agents
Used by: @platform, @devops, @sre, @test