| name | kubectl-cli |
| description | Use kubectl to manage Kubernetes clusters, deploy applications, inspect pods, view logs, and manage resources. |
kubectl (Kubernetes CLI)
Official CLI for Kubernetes. Manage clusters, deployments, pods, services, and configurations.
Configuration
kubectl config current-context
kubectl config get-contexts
kubectl config use-context my-cluster
kubectl cluster-info
Common Commands
Viewing Resources
kubectl get pods
kubectl get pods -A
kubectl get pods -n my-namespace
kubectl get deployments
kubectl get services
kubectl get nodes
kubectl get all
kubectl describe pod my-pod
kubectl top pods
Deploying
kubectl apply -f manifest.yaml
kubectl apply -f ./k8s/
kubectl create deployment my-app --image=my-app:latest
kubectl set image deployment/my-app my-app=my-app:v2
kubectl rollout status deployment/my-app
kubectl rollout undo deployment/my-app
kubectl scale deployment/my-app --replicas=3
Debugging
kubectl logs my-pod
kubectl logs my-pod -c my-container
kubectl logs -f my-pod --tail=100
kubectl exec -it my-pod -- bash
kubectl port-forward my-pod 8080:80
kubectl get events --sort-by='.lastTimestamp'
Managing Resources
kubectl delete pod my-pod
kubectl delete -f manifest.yaml
kubectl edit deployment my-app
kubectl patch deployment my-app -p '{"spec":{"replicas":2}}'
Agent Best Practices
- Use
-o json or -o jsonpath for structured output
- Always specify namespace with
-n to avoid mistakes
- Use
kubectl diff -f manifest.yaml before apply to preview changes
- Use
--dry-run=client -o yaml to generate manifests without applying
- Use
kubectl get pods -o wide for node placement info
- Watch resources with
kubectl get pods -w for real-time updates
- Use labels:
kubectl get pods -l app=my-app
Example Workflows
Check deployment health
kubectl rollout status deployment/my-app
kubectl get pods -l app=my-app -o json | jq '.items[] | {name: .metadata.name, status: .status.phase}'
Debug a crashing pod
kubectl describe pod my-pod | tail -20
kubectl logs my-pod --previous --tail=50