简体   繁体   English

使用Traefik在Kubernetes Ingress上为每个规则添加自定义标头

[英]Add a custom header per rule on Kubernetes Ingress with Traefik

I'm moving to kubernetes using traefik as my Ingress Controller. 我要搬到kubernetes使用traefik作为我的入口控制。

I have a single backend that should respond to 3000+ websites. 我有一个应该响应3000多个网站的后端。 Depending on the host, I need to add a custom header to the request before proxy passing it to the backend. 根据主机的不同,我需要在代理将代码传递给后端之前为请求添加自定义标头。

I can use the ingress.kubernetes.io/custom-request-headers annotation to add a custom header to the request but it's an annotation for the whole Ingress, so I would need to create 3000+ Ingresses, one for each website. 我可以使用ingress.kubernetes.io/custom-request-headers注释为请求添加自定义标头,但它是整个Ingress的注释,因此我需要创建3000+ Ingress,每个网站一个。

Is there another way to do this? 还有另一种方法吗? Creating 3000+ Ingresses is the same thing as creating one Ingress with 3000+ rules? 创建3000+ Ingress与创建一个具有3000+规则的Ingress是一回事吗?

Yes, you need to create one Ingress object per one host, if you want different headers her host. 是的,如果你想让她的主机使用不同的标头,你需要为每个主机创建一个Ingress对象。

You can do it by Traefik: 你可以通过Traefik来做到这一点:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: traeffic-custom-request-header
  annotations:
    ingress.kubernetes.io/custom-request-headers: "mycustomheader: myheadervalue"
spec:
  rules:
  - host: custom.configuration.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /

Also, the same thing you can do by Nginx Ingress Controller. 此外,你可以通过Nginx Ingress Controller做同样的事情。

It has the support for configuration snipper . 它支持configuration snipper Here is an example of using it to set a custom header per Ingress object: 以下是使用它为每个Ingress对象设置自定义标头的示例:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-configuration-snippet
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      more_set_headers "Request-Id: $request_id";
spec:
  rules:
  - host: custom.configuration.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /

BTW, you can use several different ingress controllers on your cluster, so it does not need to migrate everything to only one type of Ingress. 顺便说一下,您可以在群集上使用多个不同的入口控制器,因此不需要将所有内容迁移到只有一种类型的Ingress。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM