kubectl 常用命令速查
常用命令
kubectl config current-context
kubectl config get-contexts
kubectl config use-context prod
kubectl get pods -A -o wide
kubectl get deploy,svc,ingress -n app
kubectl describe pod <pod> -n app
kubectl logs <pod> -n app -f
kubectl logs <pod> -n app --previous
kubectl exec -it <pod> -n app -- sh
kubectl apply -f manifest.yaml
kubectl diff -f manifest.yaml
kubectl delete -f manifest.yaml
kubectl rollout status deploy/app -n app
kubectl rollout history deploy/app -n app
kubectl rollout undo deploy/app -n app
kubectl scale deploy/app --replicas=3 -n app
kubectl port-forward svc/app 8080:80 -n app
kubectl cp app/pod:/tmp/file ./file
kubectl top pod -n app
kubectl get events -n app --sort-by=.lastTimestamp
场景速查
| 目的 |
命令 |
| 查资源定义 |
kubectl explain deploy.spec.strategy |
| 查 API 资源 |
kubectl api-resources |
| 查权限 |
kubectl auth can-i --list -n app |
| 临时 Pod |
kubectl run tmp --rm -it --image=busybox -- sh |
| 调试节点 |
kubectl debug node/<node> -it --image=busybox |
| 导出 YAML |
kubectl get deploy app -n app -o yaml |
| 按标签筛选 |
kubectl get pods -l app=web -n app |
| 删除卡住的 Pod |
kubectl delete pod <pod> --grace-period=0 --force |
易错点
- 先确认 context 和 namespace;很多“资源不存在”其实是连错集群。
apply 和 create 的语义不同;重复执行优先 apply。
kubectl diff 能预览变更,生产变更前先跑。
delete --force 可能留下孤儿资源;只在卡死时用。
exec 需要容器内有 shell;distroless 用 kubectl debug。
port-forward 只绑本地,进程退出即失效;不要当长期暴露方案。
rollout undo 依赖 revision history;历史清理后无法回滚。
相关命令
kubectl — 完整命令页
k9s — 终端 UI
helm / kustomize — 清单管理