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 |
apply 和 create 的语义不同;重复执行优先 apply。kubectl diff 能预览变更,生产变更前先跑。delete --force 可能留下孤儿资源;只在卡死时用。exec 需要容器内有 shell;distroless 用 kubectl debug。port-forward 只绑本地,进程退出即失效;不要当长期暴露方案。rollout undo 依赖 revision history;历史清理后无法回滚。合上页面,先回答,再展开对照。
kubectl apply 和 create 的语义有什么不同?kubectl rollout undo 依赖什么?apply 是声明式、可重复执行;create 在资源已存在时会失败。
小程序版
微信扫码,随时查命令