跳到主要内容

了解准入 Webhook

准入 Webhook 会在 Kubernetes 接受创建或更新请求前校验 Ingress Controller 资源,从而在配置错误造成运行时故障或服务中断之前将其发现。

独立的 APISIX 与 API7 Ingress Controller Chart 默认通过 webhook.enabled=true 启用 webhook。APISIX umbrella Chart 默认不启用;需设置 ingress-controller.webhook.enabled=true,设置教程已显式启用该值。

启用后,webhook 会校验:

  • 资源 Schema 和必填字段
  • 对 Secret、Service 等资源的引用
  • 重复或冲突的配置
  • TLS 证书配置

只有请求中存在潜在配置问题时,webhook 才会返回消息。

如果由 Argo CD 渲染 Controller Chart,请参阅使用 Argo CD 管理,了解当前 Webhook 证书限制和确定性的配置方式。

SSL 证书冲突​

当同一 GatewayProxy 范围内的多个资源为同一主机名使用不同 TLS 证书时,会发生证书冲突。准入 Webhook 会拒绝冲突,避免路由和证书选择产生歧义。

SSL configuration conflicts detected:
- Host '<hostname>' is already configured with a different certificate in <resource-type>/<namespace>/<resource-name>

证书冲突通常由重复的 TLS 资源引起,或者在证书轮换期间创建新资源、而不是更新现有资源时发生。拒绝信息会指出已经为该主机名使用不同证书的资源。

GatewayProxy 冲突​

创建或更新 GatewayProxy 资源时,准入 Webhook 会检查它是否与现有 GatewayProxy 资源冲突。Webhook 会拒绝可能导致多个控制器干扰同一控制面实例的冲突。

gateway proxy configuration conflict: GatewayProxy <namespace>/<new-gatewayproxy-name> and <namespace>/<existing-gatewayproxy-name> both target Service <namespace>/<service-name> port <port> while sharing <admin-key>

gateway proxy configuration conflict: GatewayProxy <namespace>/<new-gatewayproxy-name> and <namespace>/<existing-gatewayproxy-name> both target control plane endpoints [<endpoint-url>] while sharing <admin-key>

当 Service 引用或配置的端点重叠,且资源使用相同的内联 Admin API Key 或同一 Secret 引用时,Webhook 会检测到冲突。它比较的是 Secret 引用,不是不同 Secret 中存储的值。因此,将同一 Key 复制到另一个 Secret,可能通过准入检查,即使两个 GatewayProxy 资源都指向同一网关组。内联 Key 和包含该 Key 的 Secret 也可能绕过此比较。

对于 API7 Ingress Controller,请为每个网关组指定一个 GatewayProxy。多个 GatewayProxy 资源必须使用各自的 Admin API Key 指向不同的网关组,可以使用相同的控制面地址。更改 Secret 名称或控制器名称不会隔离网关组的配置。对于 APISIX,请使用独立的目标网关部署。

如果路由消失,但其 Kubernetes 资源仍然存在,请检查是否有相互竞争的 GatewayProxy 资源和控制器部署。恢复步骤参阅解决 GatewayProxy 管理权竞争。

Service 引用缺失​

创建或更新将流量路由到后端服务的资源时,准入 Webhook 会验证被引用的 Kubernetes Service 是否存在。如果资源引用的 Service 不存在,你会看到类似如下警告:

Warning: Referenced Service '<namespace>/<service-name>' not found

与错误不同,警告会允许资源创建,但在后端 Service 存在之前,流量路由会失败。

引用可能因尚未创建 Service,或其名称、命名空间拼写错误而失败。应用依赖该 Service 的资源前,请确认 Service 存在于预期命名空间。

Secret 引用缺失​

当资源为 TLS、身份认证或插件配置引用 Secret 时,准入 Webhook 会检查 Secret 及所需 key 是否存在。

如果资源引用的 Secret 不存在,你会看到类似如下警告:

Warning: Referenced Secret '<namespace>/<secret-name>' not found

如果资源引用的 Secret 不包含所需 key(例如 basic authentication 缺少 password key),你会看到类似如下警告:

Warning: Secret key '<key-name>' not found in Secret '<namespace>/<secret-name>'

与错误不同,警告会允许资源创建,但在被引用 Secret 正确配置前,受影响功能无法正常工作。

请先创建 Secret,再应用引用它的资源。确认 Secret 的名称和命名空间正确,并包含所有必需的 key。

跨命名空间 Consumer Secret​

Consumer 引用另一命名空间中的凭证 Secret 时,Secret 命名空间中必须有允许该引用的 ReferenceGrant,否则请求会被拒绝,错误中包含:

not permitted by any ReferenceGrant

响应不会泄露 Secret 是否存在。请先创建 ReferenceGrant,再应用 Consumer。参阅配置跨命名空间引用。

无效 CSRF 注解​

当 k8s.apisix.apache.org/enable-csrf 为 true 时,Ingress 还必须设置非空的 k8s.apisix.apache.org/csrf-key,否则 webhook 会拒绝创建与更新请求:

annotation "k8s.apisix.apache.org/enable-csrf" is enabled but "k8s.apisix.apache.org/csrf-key" is missing or empty

为 Ingress 注解添加强 key 后重新应用资源。参阅 CSRF 注解参考。

Gateway 引用缺失​

创建或更新引用 GatewayProxy 的 Gateway 或 IngressClass 时,准入 Webhook 会验证 GatewayProxy 是否存在。如果资源引用的 GatewayProxy 不存在,你会看到类似如下警告:

Warning: Referenced GatewayProxy '<namespace>/<gatewayproxy-name>' not found

与错误不同,警告会允许资源创建,但在被引用 GatewayProxy 正确配置之前,受影响功能无法正常工作。

如需解决该问题,请确保 GatewayProxy 存在于正确命名空间,并检查 GatewayProxy 引用是否存在拼写错误。

Webhook 连接​

Webhook Service 监听端口 443,并将请求转发到由 webhook.port 配置的 Pod 端口;该端口默认为 9443。若有 NetworkPolicy 选择了 Controller Pod,它必须允许访问实际生效的 Pod TCP 端口,而不只是 Service 端口 443。

升级后校验 TCPRoute 或 UDPRoute 返回 404 时,请检查手工维护的 ValidatingWebhookConfiguration。当前 API 版本与路径如下:

资源rules[].apiVersionsclientConfig.service.path
TCPRoutev1/validate-gateway-networking-k8s-io-v1-tcproute
UDPRoutev1/validate-gateway-networking-k8s-io-v1-udproute

Chart 管理的 webhook 资源会随 Chart 更新。请手工更新自定义的 webhook 资源,并检查 webhook 端点:

kubectl get validatingwebhookconfiguration <validating-webhook-configuration> \
-o jsonpath='{range .webhooks[*]}{.clientConfig.service.path}{"\t"}{range .rules[*].apiVersions[*]}{.}{" "}{end}{"\n"}{end}' \
| grep -E 'v1-(tcp|udp)route'

每个 TCPRoute 与 UDPRoute 行都应显示其 v1 webhook 路径和 v1 API 版本。若版本仍为 v1alpha2,请同时更新规则与路径。