1---2name: kubectl-cli3description: Uses kubectl to inspect and change Kubernetes clusters: resources, logs, exec, port-forward, rollouts, and apply. Apply when the user works with pods, deployments, services, namespaces, events, or cluster debugging from a shell.4---56# kubectl (`kubectl`)78## When to use910- List, describe, create, update, or delete API resources (pods, deployments, services, configmaps, secrets, CRDs, etc.).11- Stream logs, run commands in containers (`exec`), or port-forward to workloads.12- Inspect rollouts, scale workloads, or diff/apply manifests.13- Troubleshoot with events, `describe`, metrics (`top`), or debug sessions.1415## Prerequisites1617- `kubectl` installed (`kubectl version --client`). Documentation: [kubectl reference](https://kubernetes.io/docs/reference/kubectl/).18- **Cluster access**: valid kubeconfig (default `~/.kube/config`, or `KUBECONFIG` with one or more files).19- **Context**: `kubectl config current-context`; switch with `kubectl config use-context` or pass `--context` per command.20- **Namespace**: use `-n <namespace>` or `-A` / `--all-namespaces` when not using `default`.2122## Patterns2324- **Help**: `kubectl --help`, `kubectl <cmd> --help`, `kubectl explain <resource>[.<fieldPath>]`.25- **Output**: `-o wide`, `-o yaml`, `-o json`, `-o jsonpath='...'` for scripts; `--show-labels` when labels matter.26- **Selection**: `-l key=value`, resource/name arguments, `-f file` or `-k dir` (kustomize).27- **Dry run**: `kubectl apply --dry-run=server|client` (and `kubectl delete`/`create` dry-run flags per command help) before destructive changes.28- **Imperative vs declarative**: quick edits with `kubectl edit` / `kubectl patch`; repeatable changes with manifests + `kubectl apply`.2930## Core commands (orienting)3132| Area | Examples |33|------|----------|34| Discovery | `kubectl api-resources`, `kubectl get ns`, `kubectl get pods -n <ns>` |35| Inspect | `kubectl describe <type> <name> -n <ns>`, `kubectl get all -n <ns>` |36| Logs | `kubectl logs <pod> -n <ns>`, `kubectl logs -f ...`, `kubectl logs <pod> -c <container>` |37| Exec | `kubectl exec -it <pod> -n <ns> -- <cmd>` |38| Port forward | `kubectl port-forward svc/<name> <local>:<remote> -n <ns>` |39| Apply | `kubectl apply -f manifest.yaml`, `kubectl apply -k overlays/prod` |40| Rollout | `kubectl rollout status deployment/<name> -n <ns>`, `kubectl rollout undo ...` |41| Cluster | `kubectl cluster-info`, `kubectl top nodes`, `kubectl top pods -n <ns>` |4243## Safety4445- **Secrets**: avoid printing secret contents in logs; redact tokens in command output shared with users.46- **Destructive**: `delete`, `drain`, `replace --force`, and some `apply`/`patch` operations can disrupt workloads—match intent to the user’s request.47- **Production**: prefer explicit `--context` and `-n` in shared scripts to avoid the wrong cluster or namespace.4849## References5051- **Official documentation:** [kubectl reference](https://kubernetes.io/docs/reference/kubectl/)52- **Local help:** `kubectl help`, `kubectl <cmd> --help`, `kubectl explain <resource>` (authoritative for your installed `kubectl` version)