惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Hacker News: Ask HN
Hacker News: Ask HN
S
Securelist
Security Latest
Security Latest
S
Schneier on Security
T
Threat Research - Cisco Blogs
Latest news
Latest news
Cyberwarzone
Cyberwarzone
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
NISL@THU
NISL@THU
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
Blog — PlanetScale
Blog — PlanetScale
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CERT Recently Published Vulnerability Notes
The Hacker News
The Hacker News
月光博客
月光博客
WordPress大学
WordPress大学
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
L
LINUX DO - 最新话题
Schneier on Security
Schneier on Security
Engineering at Meta
Engineering at Meta
www.infosecurity-magazine.com
www.infosecurity-magazine.com
aimingoo的专栏
aimingoo的专栏
D
Docker
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
T
Troy Hunt's Blog
L
Lohrmann on Cybersecurity
T
The Blog of Author Tim Ferriss
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LangChain Blog

博客园 - 左扬

Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:生产级 Controller 实践:并发安全、资源清理与高可用设计 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:DynamicClient 操作 CRD:无需代码生成的动态操作 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:控制器与 APIServer 完整交互流程:从 Watch 到缓存同步 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:错误处理与重试机制:WorkQueue 限速器详解 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:Leader 选举机制:高可用控制器的必备技能 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:Controller 开发模式完整实战 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:SharedInformerFactory 与等待缓存同步 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:从认证配置到 Deployment 操作 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:版本对应、架构组件与组件关系 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:Informer 源码深度解析:从底层原理到实战应用 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:Reflector 源码深度解析 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:ListWatcher 源码深度解析 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:Indexer 与 ThreadSafeStore 核心原理与源码深度剖析 Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析:DeltaFIFO 核心原理与源码深度剖析 Kubernetes 编程 / Operator 专题【左扬精讲】—— workqueue 核心原理与实战 Kubernetes 编程 / Operator 专题【左扬精讲】—— runtime.Codec 资源编解码:serializer 与 codec 差异、编解码数据结构、codec 核心调用链路 Kubernetes 编程 / Operator 专题【左扬精讲】—— Scheme 资源注册机制全解 Kubernetes 编程 / Operator 专题【左扬精讲】—— Kubernetes 自定义资源的内部版本与外部版本:从源码看版本定义机制 Kubernetes 编程 / Operator 专题【左扬精讲】—— Kubernetes 1.36.1 核心 API 数据结构全解 Kubernetes 编程 / Operator 专题【左扬精讲】—— Kubernetes 构建过程 【AIOPS】一文读懂LLM【左扬精讲】:从诞生到普及,解锁大语言模型的核心密码 【AIOPS】AI Agent 专题【左扬精讲】核心功能篇:MCP-VictoriaMetrics Hooks 源码精讲:Hooks 可观测性的无侵入式实现 【AIOPS】AI Agent 专题【左扬精讲】核心功能篇:MCP-VictoriaMetrics Golang 配置解析源码精讲 ——SRE 自定义 Agent 核心技巧 【AIOPS】AI Agent 专题【左扬精讲】核心功能篇:MCP-VictoriaMetrics Golang 并发模型解析 ——SRE 应对高并发采集的调优思路 【AIOPS】AI Agent 专题【左扬精讲】基础架构篇:MCP-VictoriaMetrics Golang 源码整体架构拆解 ——SRE 必懂的核心模块与数据流 OpenTelemetry 开发实战【左扬精讲】—— 云原生可观测体系构建与分布式追踪二次开发 Kubernetes 编程 / Operator 专题【左扬精讲】—— Operator 开发实战项目 7 —— 基于流量预测模型的智能弹性扩缩容 Operator 实战(AIOps 模型训练与智能扩容(下篇)—— 预测式弹性扩缩容 Operator 落地实现) Kubernetes 编程 / Operator 专题【左扬精讲】—— Operator 开发实战项目 7 —— 基于流量预测模型的智能弹性扩缩容 Operator 实战(AIOps 模型训练与智能扩容(上篇)—— 时序预测模型构建与离线训练) Kubernetes 编程 / Operator 专题【左扬精讲】—— Operator 开发实战项目 6 —— 基于运维专家知识库的智能故障诊断与排查 Operator 实战 Kubernetes 编程 / Operator 专题【左扬精讲】—— Operator 开发实战项目 5 —— 基于大语言模型(LLM)的实时日志流智能监测 Operator 实现 Kubernetes 编程 / Operator 专题【左扬精讲】—— Operator 开发实战项目 4 —— 基于 Operator 实现大模型私有化部署与管理 Kubernetes 编程 / Operator 专题【左扬精讲】—— Operator 开发实战项目 3 —— 面向 AI / 算力调度场景:GPU 竞价实例资源池统一调度管理 Operator 开发 Kubernetes编程 / Operator专题【左扬精讲】—— Operator 开发实战项目 2 —— 面向零售 / 电商潮汐流量难题:多云多集群数据中心级全链路弹性伸缩 DataCenter Scaler Operator 从 0 到 1 全链路开发 Kubernetes编程 / Operator专题【左扬精讲】—— 深入理解Kubebuilder注解:为什么Operator开发离不开这些特殊注释 Kubernetes编程 / Operator专题【左扬精讲】—— Operator 开发实战项目1 —— Applicaion Operator(通用应用生命周期管理 Operator 实战) Pod 镜像拉取失败?kubectl edit pods修改镜像地址的底层原理与实操 (该方法仅为临时应急方案,并非长期解决方案) Kubernetes编程/Operator专题精讲—— 理解控制器模式 —— 控制器模式的核心原理与实现逻辑(从原理到实践) 【AIOPS】AI Agent 专题【左扬精讲】模型微调实战:一站式平台 LLaMA-Factory 【AIOPS】AI Agent 专题【左扬精讲】基于 k8s+vLLM+Ray 分布式部署全指南:架构设计、资源调度与性能优化 【AIOPS】AI Agent专题【左扬精讲】非量化版DeepSeek分布式部署全指南:精度保障、显存规划与Ollama/vLLM选型 【AIOPS】AI Agent 专题【左扬精讲】零开发框架实现 ReAct Agent(Go SRE友好)
Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析: Controller 调试与诊断工具:从日志分析到问题定位
左扬 · 2026-06-13 · via 博客园 - 左扬

Kubernetes 编程 / Operator 专题【左扬精讲】—— Client-go 源代码分析: Controller 调试与诊断工具:从日志分析到问题定位

当我们开发 Kubernetes Controller 时,最头疼的问题就是:程序跑起来了,但不知道发生了什么。事件有没有收到?WorkQueue 积压了多少任务?为什么 Reconcile 总是失败?

这一篇文章,我们来系统地学习 Controller 的调试和诊断方法,从 kubectl 命令到日志分析,从 metrics 到事件排查。

Kubernetes 调试 诊断 kubectl v1.36.1

🔓 学习重点提示  — 建议先通读全文,再重点回顾标注内容

★ 重点掌握(必须)
   • kubectl describe/logs:查看资源和日志的基本方法
   • kubectl debug:如何进入 Pod 调试网络问题
   • kubectl api-resources:查看集群支持的所有资源类型

☆ 次重点(了解即可)
   • kubectl events 命令


一、日志分析:Controller 调试的第一步

日志是调试 Controller 的第一手资料。Kubernetes Controller 通常使用 klog(Kubernetes 的日志库),它会输出 INFO、WARNING、ERROR 等级别的日志。

查看 Controller 日志

# 查看 Controller Pod 的日志
kubectl logs -n <namespace> <controller-pod-name>

# 实时跟踪日志
kubectl logs -n <namespace> <controller-pod-name> -f

# 查看上一个退出的容器的日志(如果 Pod 重启过)
kubectl logs -n <namespace> <controller-pod-name> --previous

# 只看 ERROR 级别的日志
kubectl logs -n <namespace> <controller-pod-name> | grep -i error

关键日志关键词

当分析 Controller 日志时,关注以下关键词:

关键词含义排查建议
"Waiting for caches to sync" 正在等待 Informer 缓存同步 正常启动日志
"error syncing" Reconcile 失败 查看具体错误信息和 key
"requeued after rate limit" 任务因限速被重新入队 正常行为,说明任务处理失败了
"failed to get" 从 Lister 获取资源失败 缓存可能未同步完成

二、kubectl describe:查看资源详情和事件

kubectl describe 可以显示资源的详细信息和关联事件,是排查问题的利器。

# 查看 Controller Pod 的详细信息(包含 Events)
kubectl describe pod -n <namespace> <controller-pod-name>

# 查看 Deployment 的详细信息(查看 Controller 创建的 Pod 数量)
kubectl describe deployment -n <namespace> <controller-deployment-name>

# 查看 ReplicaSet(Controller 创建的实际 Pod 管理者)
kubectl describe replicaset -n <namespace> <controller-replicaset-name>

# 查看具体资源的 OwnerReferences(确认 Pod 是谁创建的)
kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.metadata.ownerReferences}' | jq

Events 字段解读

describe 输出中的 Events 部分经常被忽视,但它包含了大量有价值的信息:

Events:
  Type     Reason                  Age   From                     Message
  ----     ------                  ----  ----                     -------
  Normal   Scheduled               5m    default-scheduler          Successfully assigned my-pod to node-1
  Normal   Pulling               5m    kubelet, node-1            Pulling image "nginx:latest"
  Normal   Pulled                4m    kubelet, node-1            Successfully pulled image "nginx:latest"
  Normal   Created               4m    kubelet, node-1            Created container nginx
  Normal   Started               4m    kubelet, node-1            Started container nginx
  Warning  BackOff               2m    kubelet, node-1            Back-off restarting failed container

三、kubectl get events:查看集群事件

Events 是 Kubernetes 集群中发生的重要事件的记录,可以帮助我们追踪资源变化。

# 查看最近的事件(按时间排序)
kubectl get events -n <namespace> --sort-by='.lastTimestamp'

# 只看 WARNING 级别的事件
kubectl get events -n <namespace> --field-selector type=Warning

# 查看某个资源相关的所有事件
kubectl get events -n <namespace> --field-selector involvedObject.name=<resource-name>

# 查看某个 Controller 创建的所有 Pod 的事件
kubectl get events -n <namespace> --field-selector involvedObject.namespace=<namespace>,involvedObject.uid=<controller-uid>

# 以 JSON 格式查看完整的事件信息
kubectl get events -n <namespace> -o json | jq '.items[] | {reason: .reason, message: .message, involvedObject: .involvedObject.name}'

四、kubectl api-resources:了解集群资源类型

kubectl api-resources 显示集群支持的所有资源类型,包括内置资源和 CRD。

# 查看所有资源类型(简洁输出)
kubectl api-resources

# 查看所有资源类型及其详细信息
kubectl api-resources -o wide

# 只看特定 API 组的资源
kubectl api-resources --api-group=apps
kubectl api-resources --api-group=example.com  # 自定义 API 组

# 查看资源支持的 verbs(操作)
kubectl api-resources -o wide | grep pods

# 查看特定资源的 API 路径
kubectl api-resources --api-group=apps --verbs=get -o name

输出示例:

NAME                              SHORTNAMES   APIVERSION                    NAMESPACED   KIND
pods                              po           v1                            true         Pod
deployments                       deploy       apps/v1                       true         Deployment
configmaps                       cm           v1                            true         ConfigMap
customresourcedefinitions         crd          apiextensions.k8s.io/v1       false        CustomResourceDefinition
foos                                          example.com/v1                 true         Foo

五、kubectl debug:高级调试技巧

kubectl debug 是 Kubernetes 1.23+ 引入的功能,可以直接在 Pod 中执行调试命令。

# 在 Controller Pod 中执行调试命令
kubectl debug -it <controller-pod-name> -n <namespace> --image=busybox -- sh

# 复制 Pod 到一个带调试工具的新 Pod
kubectl debug <controller-pod-name> -n <namespace> --image=curlimages/curl -- curl http://apiserver:8001/healthz

# 复制 Pod 并修改 command(用于调试启动问题)
kubectl debug <controller-pod-name> -n <namespace> --image=busybox --share-processes --copy-to=debug-pod -- sh

# 查看进程信息(需要 --share-processes)
kubectl exec -it debug-pod -n <namespace> -- ps aux

# 网络调试:测试 APIServer 连通性
kubectl exec -it <controller-pod-name> -n <namespace> -- sh -c "nslookup kubernetes.default.svc"

六、常见问题排查清单

问题 1:Controller 没有启动

排查步骤:

# 1. 查看 Pod 是否存在
kubectl get pods -n <namespace> | grep controller

# 2. 查看 Pod 状态
kubectl get pod <controller-pod> -n <namespace> -o wide

# 3. 查看 Pod 启动失败原因
kubectl describe pod <controller-pod> -n <namespace> | tail -50

# 4. 查看镜像拉取日志
kubectl describe pod <controller-pod> -n <namespace> | grep -A5 "Events:"

问题 2:Controller 启动后卡住

排查步骤:

# 1. 查看日志,确认是否在等待缓存同步
kubectl logs <controller-pod> -n <namespace> | grep -i "cache\|sync"

# 2. 确认 APIServer 是否可达
kubectl exec <controller-pod> -n <namespace> -- curl -k https://kubernetes.default.svc/healthz

# 3. 查看 RBAC 权限是否足够
kubectl auth can-i --list --namespace=<namespace> --as=system:serviceaccount:<namespace>:<sa-name>

问题 3:Reconcile 总是失败

排查步骤:

# 1. 搜索错误日志
kubectl logs <controller-pod> -n <namespace> | grep -i "error\|failed\|cannot"

# 2. 查看 WorkQueue 积压情况(如果有 metrics)
kubectl port-forward <controller-pod> 8080:8080 -n <namespace>
curl http://localhost:8080/metrics | grep workqueue

# 3. 查看目标资源是否存在
kubectl get <resource-type> <resource-name> -n <namespace>

# 4. 查看资源是否有 finalizer 或其他保护机制
kubectl get <resource-type> <resource-name> -n <namespace> -o jsonpath='{.metadata.finalizers}'

七、日志级别调优

通过调整日志级别,可以获得更详细或更简洁的日志输出。

# 在 Pod spec 中设置日志级别
# -v=0: 最小日志(默认)
# -v=2: 显示 HTTP 请求和响应
# -v=4: 显示 Informer 事件
# -v=6: 显示详细的调试信息
# -v=10: 最大日志级别

spec:
  containers:
  - name: controller
    args:
    - --v=4  # 设置日志级别为 4

🌟 实用技巧
在生产环境中,建议使用 -v=2 或 -v=4,既能获得足够的信息,又不会因为日志量太大影响性能。需要调试时临时提升到 -v=6 或更高。


八、总结

这一节我们学习了 Controller 的调试和诊断方法:

  • 日志分析:kubectl logs、关键日志关键词、grep 技巧
  • 资源详情:kubectl describe、Events 字段解读
  • 集群事件:kubectl get events、筛选和排序
  • 资源类型:kubectl api-resources、API 路径
  • 高级调试:kubectl debug、网络调试
  • 问题排查清单:启动失败、卡住、Reconcile 失败的处理步骤

下一节我们将学习 生产级实践与常见问题排查,这是整个系列的最后一篇,将总结生产环境中需要注意的关键问题。敬请期待!


Kubernetes 编程 / Operator 专题【左扬精讲】—— Controller 调试与诊断工具 · 来源:Kubernetes v1.36.1 client-go 源码分析