Kubernetes ExternalName 服务添加 Headers

Kubernetes ExternalName Service Add Headers

TLDR:我是 运行 使用 AKS 的 kubernetes 集群。我创建了一个外部名称服务来代理到外部地址。我想在请求中添加 'Host' header。我应该怎么做?

有关更多上下文,我有一个处理所有传入流量的入口控制器。我想将该流量的一个子集(基于路由)路由到外部 Azure 函数。我已经使用我的 Azure 函数的主机名设置了一个 ExternalName 服务,并将流量路由到它。但是,由于 Microsoft 处理路由到其功能的方式,header.

中需要具有正确 FQDN 的 'Host' 值

我不想让发送原始请求的人将其包含在 header 中,因此我想在将流量代理到 Azure 函数时代表他们添加它。

这是我的服务文件:

kind: Service
apiVersion: v1
metadata:
  name: azure-function-proxy-service
  labels:
    app: proxy-service
spec:
  type: ExternalName
  externalName: azure-function.azurewebsites.net

以及相关入口规则代码:

- host: hostto.proxy.net
http:
  paths:
  - path: /route/to/proxy
    backend:
      serviceName: azure-function-proxy-service
      servicePort: 80

我认为您可以使用 ingress-nginx 中的 Canary 功能。 如果您添加注释 nginx.ingress.kubernetes.io/canary: "true" 您将能够使用以下规则

  • nginx.ingress.kubernetes.io/canary-by-header: The header to use for notifying the Ingress to route the request to the service specified in the Canary Ingress. When the request header is set to always, it will be routed to the canary. When the header is set to never, it will never be routed to the canary. For any other value, the header will be ignored and the request compared against the other canary rules by precedence.

  • nginx.ingress.kubernetes.io/canary-by-cookie: The cookie to use for notifying the Ingress to route the request to the service specified in the Canary Ingress. When the cookie value is set to always, it will be routed to the canary. When the cookie is set to never, it will never be routed to the canary. For any other value, the cookie will be ingored and the request compared against the other canary rules by precedence.

  • nginx.ingress.kubernetes.io/canary-weight: The integer based (0 - 100) percent of random requests that should be routed to the service specified in the canary Ingress. A weight of 0 implies that no requests will be sent to the service in the Canary ingress by this canary rule. A weight of 100 means implies all requests will be sent to the alternative service specified in the Ingress.

您还可以阅读这篇文章 canary deployment with ingress-nginx,其中解释了如何使用 canary-weightcanary-by-header

我发现我真正想做的是修改通过我的入口控制器的请求的 header。我发现这样做的最佳选择是 nginx.ingress.kubernetes.io/configuration-snippet,但它没有给我想要的细粒度控制。

最终我不得不建立一个额外的 nginx 实例来代理请求,这样我就可以完全控制 nginx 配置。