
【集群】Kubernetes Webhook 实战:Kueue 调度器准入控制故障排除与性能优化
💡简介
📖 文档定位:本文为 Kueue 调度器 Webhook 机制的实战故障排除篇,与 理论介绍文档 形成互补。理论文档重点解析 Webhook 的基本概念和工作原理,而本文则专注于解决实际部署和测试过程中遇到的 Webhook 相关问题。
适用场景:如果您在部署 Kueue 调度器或进行调度器性能测试时遇到 Webhook 相关的错误,那么本文档将为您提供详细的问题诊断和解决方案。
🖼️背景
问题起源
在 调度器性能测试调试 过程中,我们遇到了一个典型的 Webhook 连接问题:
1 2 3
| Internal error occurred: failed calling webhook "mresourceflavor.kb.io": failed to call webhook: Post "https://kueue-webhook-service.kueue-system.svc:443/mutate-kueue-x-k8s-io-v1beta1-resourceflavor?timeout=10s": dial tcp 10.96.33.70:443: connect: connection refused
|
这个错误不仅影响了测试的顺利进行,也让我们深入思考了 Webhook 在 Kubernetes 调度器中的重要作用。
为什么需要 Webhook?
1. 准入控制需求
- 资源验证:确保创建的资源符合集群策略
- 自动标签:为工作负载添加必要的元数据
- 队列管理:协调工作负载与调度队列的关系
2. 扩展性要求
- 动态配置:无需重启 API 服务器即可添加新的验证规则
- 外部集成:允许外部系统参与资源管理决策
- 安全增强:提供额外的安全验证层
🧠Webhook 在 Kueue 中的作用机制
1. 准入控制机制
Kueue 使用 Webhook 实现以下核心功能:
1.1 资源验证
- Job 验证:检查 Kubernetes Job 是否符合 Kueue 管理要求
- Pod 验证:验证 Pod 的资源请求和限制
- ResourceFlavor 验证:确保资源风味配置正确
1.2 自动标签管理
1 2 3
| kueue.x-k8s.io/managed: "true" kueue.x-k8s.io/queue-name: "default"
|
1.3 队列分配
- 工作负载分类:根据标签将工作负载分配到相应队列
- 资源配额检查:验证工作负载是否超出队列资源限制
2. Webhook 类型
2.1 Mutating Webhook(修改性)
- 作用:修改资源内容
- 时机:在验证性 Webhook 之前执行
- 功能:添加默认标签、注解等
2.2 Validating Webhook(验证性)
- 作用:验证资源是否符合规则
- 时机:在资源持久化到 etcd 之前
- 结果:允许或拒绝请求
3. 工作流程详解
1
| 用户创建 Job → API 服务器接收请求 → Mutating Webhook → Validating Webhook → 持久化到 etcd
|
具体步骤:
- 请求接收:用户提交 Job 创建请求
- Webhook 拦截:API 服务器根据配置拦截请求
- 修改处理:Mutating Webhook 添加必要标签
- 验证处理:Validating Webhook 检查资源合规性
- 结果返回:处理结果返回给 API 服务器
- 资源创建:验证通过后,Job 被创建
🔨Webhook 配置详解
1. 服务配置
1.1 Webhook 服务定义
1 2 3 4 5 6 7 8 9 10 11 12
| apiVersion: v1 kind: Service metadata: name: kueue-webhook-service namespace: kueue-system spec: ports: - port: 443 protocol: TCP targetPort: 9443 selector: control-plane: controller-manager
|
1.2 Webhook 服务器配置
1 2 3
| webhook: port: 9443 timeoutSeconds: 10
|
2. Webhook 规则配置
2.1 Mutating Webhook 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| apiVersion: admissionregistration.k8s.io/v1 kind: MutatingWebhookConfiguration metadata: name: kueue-mutating-webhook-configuration webhooks: - name: mjob.kb.io clientConfig: service: name: kueue-webhook-service namespace: kueue-system path: /mutate-batch-v1-job rules: - apiGroups: ["batch"] apiVersions: ["v1"] operations: ["CREATE"] resources: ["jobs"] failurePolicy: Fail timeoutSeconds: 10
|
2.2 Validating Webhook 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| apiVersion: admissionregistration.k8s.io/v1 kind: ValidatingWebhookConfiguration metadata: name: kueue-validating-webhook-configuration webhooks: - name: vresourceflavor.kb.io clientConfig: service: name: kueue-webhook-service namespace: kueue-system path: /validate-kueue-x-k8s-io-v1beta1-resourceflavor rules: - apiGroups: ["kueue.x-k8s.io"] apiVersions: ["v1beta1"] operations: ["CREATE", "UPDATE"] resources: ["resourceflavors"]
|
3. 证书管理
3.1 TLS 证书配置
1 2 3 4 5 6 7 8 9 10
| apiVersion: v1 kind: Secret metadata: name: kueue-webhook-server-cert namespace: kueue-system type: kubernetes.io/tls data: tls.crt: <base64-encoded-cert> tls.key: <base64-encoded-key>
|
3.2 证书挂载
1 2 3 4 5 6 7 8 9
| volumes: - name: webhook-server-cert secret: secretName: kueue-webhook-server-cert volumeMounts: - name: webhook-server-cert mountPath: /tmp/certs readOnly: true
|
🚨常见问题及解决方案
问题1:Webhook 连接被拒绝
错误现象
1 2 3
| failed calling webhook "mresourceflavor.kb.io": failed to call webhook: Post "https://kueue-webhook-service.kueue-system.svc:443/mutate-kueue-x-k8s-io-v1beta1-resourceflavor?timeout=10s": dial tcp 10.96.33.70:443: connect: connection refused
|
可能原因
- Webhook 服务未启动
- Webhook Pod 未就绪
- 证书未正确生成
- 服务端点未配置
诊断步骤
步骤1:检查 Webhook Pod 状态
1 2 3 4 5
| kubectl get pods -n kueue-system -l control-plane=controller-manager
kubectl describe pod -n kueue-system -l control-plane=controller-manager
|
步骤2:检查 Webhook 服务
1 2 3 4 5
| kubectl get service kueue-webhook-service -n kueue-system
kubectl describe service kueue-webhook-service -n kueue-system
|
步骤3:检查服务端点
1 2 3 4 5
| kubectl get endpoints kueue-webhook-service -n kueue-system
kubectl describe endpoints kueue-webhook-service -n kueue-system
|
步骤4:检查证书
1 2 3 4 5
| kubectl get secret kueue-webhook-server-cert -n kueue-system
kubectl describe secret kueue-webhook-server-cert -n kueue-system
|
解决方案
方案1:等待服务就绪
1 2 3 4 5
| kubectl wait --for=condition=ready pod -l control-plane=controller-manager -n kueue-system --timeout=120s
kubectl wait --for=condition=ready endpoints kueue-webhook-service -n kueue-system --timeout=60s
|
方案2:重新生成证书
1 2 3 4 5
| kubectl delete secret kueue-webhook-server-cert -n kueue-system
kubectl rollout restart deployment kueue-controller-manager -n kueue-system
|
方案3:检查网络连接
1 2 3
| kubectl run test-webhook --image=busybox --rm -it --restart=Never -- \ wget -qO- --no-check-certificate https://kueue-webhook-service.kueue-system.svc:443/healthz
|
问题2:Webhook 超时
错误现象
1
| failed calling webhook: timeout=10s
|
解决方案
方案1:增加超时时间
1 2 3 4
| webhooks: - name: mjob.kb.io timeoutSeconds: 30
|
方案2:优化 Webhook 性能
1 2 3 4 5 6 7 8
| resources: requests: cpu: 500m memory: 512Mi limits: cpu: 1000m memory: 1Gi
|
方案3:调整并发处理
1 2 3 4 5 6
| controller: groupKindConcurrency: Job.batch: 100 Pod: 100 Workload.kueue.x-k8s.io: 100
|
问题3:证书问题
错误现象
1
| x509: certificate signed by unknown authority
|
解决方案
方案1:重新生成证书
1 2 3 4 5
| kubectl delete secret kueue-webhook-server-cert -n kueue-system
kubectl rollout restart deployment kueue-controller-manager -n kueue-system
|
方案2:检查证书配置
1 2 3 4 5
| kubectl get secret kueue-webhook-server-cert -n kueue-system -o yaml
kubectl get secret kueue-webhook-server-cert -n kueue-system -o jsonpath='{.data.tls\.crt}' | base64 -d | openssl x509 -text -noout
|
🔍调试方法
1. 日志分析
查看 Webhook 服务器日志
1 2 3 4 5
| kubectl logs -n kueue-system -l control-plane=controller-manager -c manager
kubectl logs -n kueue-system -l control-plane=controller-manager -c manager -f
|
查看 API 服务器日志
1 2 3 4 5
| kubectl logs -n kube-system kube-apiserver-kind-control-plane
kubectl logs -n kube-system kube-apiserver-kind-control-plane | grep webhook
|
2. 配置检查
检查 Webhook 配置
1 2 3 4 5
| kubectl get mutatingwebhookconfiguration kueue-mutating-webhook-configuration -o yaml
kubectl get validatingwebhookconfiguration kueue-validating-webhook-configuration -o yaml
|
检查服务配置
1 2 3 4 5
| kubectl get service kueue-webhook-service -n kueue-system -o yaml
kubectl get endpoints kueue-webhook-service -n kueue-system -o yaml
|
3. 网络测试
测试服务连通性
1 2 3 4 5 6 7
| kubectl run test-webhook --image=busybox --rm -it --restart=Never -- \ wget -qO- --no-check-certificate https://kueue-webhook-service.kueue-system.svc:443/healthz
kubectl run test-port --image=busybox --rm -it --restart=Never -- \ nc -zv kueue-webhook-service.kueue-system.svc 443
|
🏥实战总结
关键要点
- Webhook 是 Kueue 的核心组件:负责准入控制、资源验证和自动标签管理
- 证书管理至关重要:TLS 证书是 Webhook 安全通信的基础
- 服务就绪检查:确保 Webhook 服务完全就绪是避免连接问题的关键
实践经验
通常可考虑的故障排除流程如下:
- 检查 Pod 状态:确认 Webhook 服务正在运行
- 验证服务配置:检查服务和端点配置
- 检查证书:验证 TLS 证书是否正确
- 测试连通性:确认网络连接正常
- 查看日志:分析错误信息和性能指标
希望本文档能够帮助您更好地理解和解决 Kubernetes 集群中的 Webhook 相关问题!
- 希望这篇博客对你有帮助!如果你有任何问题或需要进一步的帮助,请随时提问。
- 如果你喜欢这篇文章,欢迎动动小手给我一个follow或star。
🗺参考文献
[1] Webhook - Wikipedia
[2] Kubernetes 中的准入控制 - 官方文档
[3] Volcano Webhook Implementation - GitHub
[4] Admission Webhook 良好实践 - Kubernetes官方文档
[5] Webhook Mode - Kubernetes官方文档
[6] Kubernetes API Server 参数 - 官方文档
[7] 什么是Webhook?工作原理?如何实现?缺点? - 掘金
[8] 详细介绍一下webhook技术 - 知乎
[9] Webhook 是什么?详解其工作原理 - CSDN
[10] 什么是 Webhook? - RedHat
[11] kubernetes的webhook开发 - 博客园