EasyDebug.NET

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 app
kubectl 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 app
kubectl 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 app
kubectl 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 app
kubectl 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 app
kubectl 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 -d
kubectl 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 app
kubectl 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?

去反馈
站长的博客
提个建议