kubectl 常用命令速查表
上下文、Pod、部署、回滚、Service 等九类命令,含可直接套用的示例
共 90 条命令
kubectl config get-contexts上下文与清单查询列出所有集群上下文,切错集群是常见事故
kubectl config get-contexts
kubectl config use-context prod上下文与清单查询切换当前上下文,操作前先确认这一条
kubectl config use-context prod-cn
kubectl config current-context上下文与清单查询查看当前在哪个集群,脚本里可做前置校验
kubectl config current-context
kubectl config set-context --current --namespace=staging上下文与清单查询把默认命名空间固定下来,省去每次 -n
kubectl config set-context --current --namespace=staging
kubectl get ns上下文与清单查询列出命名空间
kubectl get ns
kubectl get all -n app上下文与清单查询一次看某命名空间下的主要资源
kubectl get all -n app
kubectl get pods -o wide上下文与清单查询多显示 IP 与所在节点,排查调度与网络必备
kubectl get pods -o wide -n app
kubectl get pods -A上下文与清单查询查所有命名空间的 Pod
kubectl get pods -A --field-selector=status.phase!=Running
kubectl get pods -w上下文与清单查询持续监听列表变化,滚动更新时观察用
kubectl get pods -w -n app
kubectl get pod web-0 -o yaml上下文与清单查询导出某个资源的完整 YAML(含运行时状态)
kubectl get pod web-0 -o yaml > pod-backup.yaml
kubectl get deploy web -o yaml --export上下文与清单查询导出时去掉集群生成的字段,方便重新套用
kubectl get deploy web -o yaml -n app
kubectl explain deploy.spec.strategy上下文与清单查询在终端查字段含义与取值范围,不用翻文档
kubectl explain deploy.spec.strategy --recursive
kubectl api-resources上下文与清单查询列出集群支持的所有资源类型与简写
kubectl api-resources --namespaced=true
kubectl apply -f deploy.yaml上下文与清单查询声明式应用配置,重复执行安全
kubectl apply -f k8s/ -n app
kubectl delete -f deploy.yaml上下文与清单查询按文件删除资源
kubectl delete -f k8s/ -n app
kubectl create ns staging上下文与清单查询创建命名空间
kubectl create ns staging
kubectl describe pod <pod>Pod 操作查看 Pod 详情,末尾 Events 段是排障重点
kubectl describe pod web-0 -n app
kubectl logs <pod>Pod 操作查看容器日志
kubectl logs web-0 -n app --tail=200
kubectl logs -f <pod> -c appPod 操作跟踪指定容器日志,多容器 Pod 必须带 -c
kubectl logs -f web-0 -c app -n app
kubectl logs --previous <pod>Pod 操作看上一次崩溃前的日志,CrashLoopBackOff 必用
kubectl logs web-0 --previous -n app
kubectl logs --since=10m <pod>Pod 操作只看最近一段时间的日志
kubectl logs web-0 --since=10m --timestamps -n app
kubectl exec -it <pod> -- shPod 操作进入容器,多数精简镜像没有 bash
kubectl exec -it web-0 -n app -- /bin/sh
kubectl exec <pod> -- envPod 操作查看容器内实际生效的环境变量
kubectl exec web-0 -n app -- printenv
kubectl cp ./local.txt <pod>:/tmp/Pod 操作在本地与 Pod 之间复制文件
kubectl cp ./dump.sql app/web-0:/tmp/dump.sql
kubectl port-forward <pod> 8080:80Pod 操作把 Pod 端口转发到本地,调试未暴露的服务
kubectl port-forward pod/web-0 8080:80 -n app
kubectl run tmp --rm -it --image=busybox -- shPod 操作临时排错容器,退出即清理
kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- sh
kubectl get pod <pod> -o jsonpath="{.status.podIP}"Pod 操作取 Pod IP,方便写进脚本
kubectl get pod web-0 -o jsonpath="{.status.podIP}" -n appkubectl delete pod <pod>Pod 操作删除 Pod,被控制器管理的会自动重建
kubectl delete pod web-0 -n app
kubectl delete pod <pod> --grace-period=0 --forcePod 操作强制删除卡在 Terminating 的 Pod
kubectl delete pod web-0 --grace-period=0 --force -n app
kubectl get pod <pod> -o jsonpath="{.status.containerStatuses[*].restartCount}"Pod 操作看重启次数,判断是否在反复崩溃
kubectl get pod web-0 -o jsonpath="{.status.containerStatuses[*].restartCount}" -n appkubectl get deploy部署与扩缩容列出 Deployment 及副本就绪情况
kubectl get deploy -n app -o wide
kubectl scale deploy/web --replicas=3部署与扩缩容调整副本数,扩容最直接的方式
kubectl scale deploy/web --replicas=3 -n app
kubectl set image deploy/web web=app:1.1部署与扩缩容滚动更新镜像版本
kubectl set image deploy/web web=registry.example.com/app:1.1 -n app
kubectl edit deploy/web部署与扩缩容直接编辑线上配置,保存即生效
kubectl edit deploy/web -n app
kubectl patch deploy/web --type=merge -p "{\"spec\":{\"replicas\":2}}"部署与扩缩容用一行 JSON 改字段,脚本里比 edit 合适
kubectl patch deploy/web --type=merge -p "{\"spec\":{\"replicas\":2}}" -n appkubectl autoscale deploy/web --min=2 --max=10 --cpu-percent=70部署与扩缩容配置水平自动扩缩容
kubectl autoscale deploy/web --min=2 --max=10 --cpu-percent=70 -n app
kubectl get rs部署与扩缩容查看 ReplicaSet,历史版本都留在这里
kubectl get rs -n app
kubectl get deploy web -o jsonpath="{.spec.template.spec.containers[*].image}"部署与扩缩容确认线上实际跑的是哪个镜像
kubectl get deploy web -o jsonpath="{.spec.template.spec.containers[*].image}" -n appkubectl annotate deploy/web note="release-1.1"部署与扩缩容打注释,记录发布信息
kubectl annotate deploy/web note="release-1.1" --overwrite -n app
kubectl label deploy/web tier=web部署与扩缩容打标签,供 Service 选择器与选择器查询使用
kubectl label deploy/web tier=web --overwrite -n app
kubectl rollout status deploy/web发布与回滚观察滚动更新进度,发布后必看
kubectl rollout status deploy/web -n app --timeout=120s
kubectl rollout history deploy/web发布与回滚查看历史版本与变更原因
kubectl rollout history deploy/web -n app
kubectl rollout undo deploy/web发布与回滚回滚到上一个版本,出事后最快止血
kubectl rollout undo deploy/web -n app
kubectl rollout undo deploy/web --to-revision=2发布与回滚回滚到指定版本,先看 history 再定
kubectl rollout undo deploy/web --to-revision=2 -n app
kubectl rollout restart deploy/web发布与回滚原地重启全部 Pod,配置变更后常用
kubectl rollout restart deploy/web -n app
kubectl rollout pause deploy/web发布与回滚暂停发布,配合 set image 可在观察后再放行
kubectl rollout pause deploy/web -n app
kubectl rollout resume deploy/web发布与回滚恢复被暂停的发布
kubectl rollout resume deploy/web -n app
kubectl get svcService 与网络列出 Service 及其 ClusterIP 与端口
kubectl get svc -n app -o wide
kubectl get endpoints webService 与网络查看 Service 背后真实的 Pod IP,为空说明选择器没匹配上
kubectl get endpoints web -n app
kubectl describe svc webService 与网络查看 Service 详情,含选择器与端口映射
kubectl describe svc web -n app
kubectl expose deploy/web --port=80 --target-port=8080Service 与网络为 Deployment 快速建一个 Service
kubectl expose deploy/web --port=80 --target-port=8080 --type=ClusterIP -n app
kubectl port-forward svc/web 8080:80Service 与网络转发 Service 端口,不依赖 Ingress 就能本地验证
kubectl port-forward svc/web 8080:80 -n app
kubectl get ingressService 与网络列出 Ingress 与暴露的域名
kubectl get ingress -n app
kubectl describe ingress webService 与网络查看 Ingress 规则与后端解析结果
kubectl describe ingress web -n app
kubectl get netpolService 与网络列出网络策略,Pod 之间不通先查它
kubectl get netpol -n app
kubectl get svc web -o jsonpath="{.spec.clusterIP}"Service 与网络取 ClusterIP,写进配置或脚本用
kubectl get svc web -o jsonpath="{.spec.clusterIP}" -n appkubectl get cm配置与密钥列出 ConfigMap
kubectl get cm -n app
kubectl create cm app-config --from-file=app.yaml配置与密钥从文件创建 ConfigMap
kubectl create cm app-config --from-file=config/app.yaml -n app
kubectl describe cm app-config配置与密钥查看 ConfigMap 内容
kubectl describe cm app-config -n app
kubectl get secret配置与密钥列出 Secret,值默认是 base64 不是加密
kubectl get secret -n app
kubectl create secret generic db --from-literal=password=changeit配置与密钥从字面量创建 Secret
kubectl create secret generic db --from-literal=password=changeit -n app
kubectl get secret db -o jsonpath="{.data.password}" | base64 -d配置与密钥解出 Secret 明文,排查配置不生效
kubectl get secret db -o jsonpath="{.data.password}" -n app | base64 -dkubectl get sa配置与密钥列出 ServiceAccount,Pod 访问 API 用的身份
kubectl get sa -n app
kubectl auth can-i get pods --as=system:serviceaccount:app:web配置与密钥验证某个身份有没有权限,RBAC 排查用
kubectl auth can-i get pods --as=system:serviceaccount:app:web -n app
kubectl get nodes节点与调度列出节点与状态
kubectl get nodes -o wide
kubectl describe node <node>节点与调度查看节点详情,含已分配资源与污点
kubectl describe node node-1
kubectl top nodes节点与调度查看节点实际资源占用,需装 metrics-server
kubectl top nodes
kubectl top pods节点与调度查看 Pod 资源占用,定位谁在吃内存
kubectl top pods -n app --sort-by=memory
kubectl cordon <node>节点与调度标记节点不可调度,但不影响已有 Pod
kubectl cordon node-1
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data节点与调度驱逐节点上的 Pod,维护前必做
kubectl drain node-1 --ignore-daemonsets --delete-emptydir-data
kubectl uncordon <node>节点与调度恢复节点可调度
kubectl uncordon node-1
kubectl taint nodes node-1 dedicated=gpu:NoSchedule节点与调度给节点加污点,只让能容忍的 Pod 调度上去
kubectl taint nodes node-1 dedicated=gpu:NoSchedule
kubectl get events --sort-by=.lastTimestamp排障按时间倒序看事件,集群层面的问题都在这
kubectl get events --sort-by=.lastTimestamp -n app | tail -30
kubectl get events -w排障实时监听事件流,复现问题时开着
kubectl get events -w -n app
kubectl get pod --field-selector=status.phase=Failed排障筛出所有失败状态的 Pod
kubectl get pod --field-selector=status.phase=Failed -A
kubectl get pod <pod> -o jsonpath="{.status.conditions[?(@.type==\"Ready\")].reason}"排障取未就绪原因,比翻 describe 快
kubectl get pod web-0 -o jsonpath="{.status.conditions[?(@.type==\"Ready\")].reason}" -n appkubectl debug -it <pod> --image=busybox --target=app排障给已有 Pod 注入临时容器,共享进程命名空间
kubectl debug -it web-0 --image=busybox:1.36 --target=app -n app
kubectl debug node/<node> -it --image=busybox排障在节点上开排错 Pod,能看宿主机文件系统
kubectl debug node/node-1 -it --image=busybox:1.36
kubectl logs <pod> --all-containers排障一次看 Pod 内所有容器日志
kubectl logs web-0 --all-containers --prefix -n app
kubectl wait --for=condition=Ready pod/web-0 --timeout=60s排障等待 Pod 就绪,脚本里做同步点
kubectl wait --for=condition=Ready pod/web-0 --timeout=60s -n app
kubectl get pod web-0 -o json | jq .status排障配合 jq 精查状态字段,比 jsonpath 好写
kubectl get pod web-0 -o json -n app | jq .status
kubectl get pod web-0 -o yaml --show-managed-fields排障看字段到底被谁改的,排查配置被篡改
kubectl get pod web-0 -o yaml --show-managed-fields -n app
kubectl cluster-info集群与资源查看控制面地址与附加组件位置
kubectl cluster-info
kubectl version --short集群与资源查看客户端与服务端版本,注意版本偏差
kubectl version --short
kubectl apply --dry-run=server -f deploy.yaml集群与资源服务端试跑,真校验字段与准入策略但不落盘
kubectl apply --dry-run=server -f deploy.yaml -n app
kubectl diff -f deploy.yaml集群与资源预览 apply 会改哪些字段,变更前先看
kubectl diff -f deploy.yaml -n app
kubectl get pv,pvc集群与资源查看存储卷与声明,Pending 多为存储类问题
kubectl get pv,pvc -n app
kubectl get hpa集群与资源查看自动扩缩容状态与当前指标
kubectl get hpa -n app
kubectl get crd集群与资源列出集群安装的自定义资源定义
kubectl get crd | head -30
kubectl api-resources --verbs=list --namespaced -o name集群与资源列出支持列举的命名空间级资源,备份脚本常用
kubectl api-resources --verbs=list --namespaced -o name
这个工具不好用,或者遇到 bug?
去反馈