Home

kubectl describe 常规使用

除了 kubectl logs 和 Events 之外,kubectl describe 也是 Kubernetes 排障时最常用的命令之一。它不像 kubectl get 那么简略,也不像直接看 YAML 那么偏原始定义,更适合快速看资源当前状态、事件和调度信息。

基础用法

查看 Pod 详情

kubectl describe pod xxxxxx-postgresql

# 指定命名空间
kubectl describe pod my-pod -n xxxxxx-postgresql

# 查看所有 Pod
kubectl describe pods

查看其他资源

# 查看 Deployment
kubectl describe deployment xxxxxx-mariadb

# 查看 Service
kubectl describe service xxxxxx-service

# 查看 Node
kubectl describe node node-mariadb

# 查看 ConfigMap
kubectl describe configmap mariadb-config

# 查看 Secret(敏感信息会隐藏)
kubectl describe secret mariadb-secret

Pod 排查

查看事件

排查 Pod 时,最值得先看的通常是最下方的 Events 区域。

kubectl describe pod ops-elasticsearch -n prod

PendingImagePullBackOffCrashLoopBackOff 这类问题,很多时候从这里就能直接看出原因。

例如某个 Pod 一直 Pending,描述信息里可能直接提示:

Warning  FailedScheduling  node didn't have enough memory

这就说明问题不是镜像、不是容器,而是调度阶段资源不足。

查看容器状态

describe 里还会列出容器当前状态、重启次数、退出码和上一次退出原因,这些信息非常适合拿来判断是不是启动后崩溃。

kubectl describe pod my-elasticsearch | grep -A 10 "State:"
kubectl describe pod my-elasticsearch | grep "Restart Count"

例如容器反复重启,常见输出可能是:

Last State: Terminated
  Reason: Error
  Exit Code: 137

137 往往意味着容器被 OOM Kill,可以优先检查内存限制是否过低。

查看资源配置

除了状态本身,也可以顺手确认 Pod 的资源请求和限制。

kubectl describe pod xxxxxx-redis | grep -A 5 "Limits:"
kubectl describe pod xxxxxx-redis | grep -A 5 "Requests:"
kubectl describe pod ops-not-kafka | grep "QoS Class"

当集群资源紧张时,这部分信息很适合用来排查哪些 Pod 的 requests 配得过高。

Node 排查

查看节点状态

kubectl describe node worker-node-1
kubectl describe node worker-node-1 | grep -A 10 "Conditions:"

重点关注的状态包括:

  • Ready
  • MemoryPressure
  • DiskPressure
  • PIDPressure

如果 Pod 无法调度到某个节点,describe node 往往会直接给出线索。例如:

DiskPressure: True
Message: kubelet has disk pressure

这就说明节点磁盘压力过大,通常需要清理日志、镜像或无效数据。

查看节点资源分配

kubectl describe node worker-node-1 | grep -A 20 "Allocated resources:"

这一段很适合判断节点是不是已经被资源申请压满。

查看节点上的 Pod

kubectl describe node worker-node-1 | grep -A 50 "Non-terminated Pods:"

这个视角很适合快速判断某个节点上到底压了哪些工作负载。

Service 排查

查看 Service 配置

kubectl describe service ops-not-service
kubectl describe svc my-service | grep Selector
kubectl describe svc my-service | grep Endpoints

Service 访问异常时,优先看三项:

  • Selector
  • Endpoints
  • 端口映射

例如 Endpoints 为空:

Endpoints: <none>

这种情况通常就是标签不匹配,Service 没有选中任何 Pod。

查看 Service 类型和端口

kubectl describe svc ops-not-service | grep Type
kubectl describe svc ops-not-service | grep Port

Deployment / StatefulSet 排查

查看副本状态

kubectl describe deployment xxxxxx-rabbitmq
kubectl describe deploy xxxxxx-rabbitmq | grep Replicas
kubectl describe deploy xxxxxx-rabbitmq | grep -A 5 "Conditions:"

发版后如果只有部分 Pod 更新成功,可以从这里确认是否卡在滚动更新阶段。

Replicas: 3 desired | 2 updated | 3 total | 2 available
Conditions:
  Progressing: False
  Reason: ProgressDeadlineExceeded

这通常意味着新版本 Pod 没有按预期就绪,常见原因是镜像、探针或配置错误。

查看更新策略

kubectl describe deploy xxxxxx-rabbitmq | grep -A 3 "StrategyType:"

存储排查

查看 PVC 状态

kubectl describe pvc xxxxxx-pvc

如果 PVC 一直 Pending,这里通常能看出到底是没匹配到 StorageClass、没绑定到 PV,还是后端存储 provision 失败。

总结

kubectl describe 最大的价值,不是“信息多”,而是它把资源当前状态、调度结果、事件记录和依赖关系放到了同一个视角里。排障时如果你不知道先看哪里,可以先从下面这个顺序开始:

  1. 先看 Events
  2. 再看状态和重启原因
  3. 再看资源配置、关联对象和节点条件

大多数 Kubernetes 的常见问题,走完这三步基本都会有方向。

Docker 网络 存储