使用Traefik在Kubernetes Ingress上为每个规则添加自定义标头 [英] Add a custom header per rule on Kubernetes Ingress with Traefik

查看:472
本文介绍了使用Traefik在Kubernetes Ingress上为每个规则添加自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用traefik作为我的Ingress Controller迁移到kubernetes.

I'm moving to kubernetes using traefik as my Ingress Controller.

我有一个后端,可以响应3000多个网站.根据主机的不同,我需要先向请求添加自定义标头,然后再将其代理传递给后端.

I have a single backend that should respond to 3000+ websites. Depending on the host, I need to add a custom header to the request before proxy passing it to the backend.

我可以使用ingress.kubernetes.io/custom-request-headers批注向请求添加自定义标头,但这是整个Ingress的批注,因此我需要创建3000多个Ingress,每个网站一个.

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.

还有另一种方法吗?创建3000个以上的Ingress与创建具有3000多个规则的一个Ingress是一回事吗?

Is there another way to do this? Creating 3000+ Ingresses is the same thing as creating one Ingress with 3000+ rules?

推荐答案

是的,如果您要为每个主机创建不同的标头,则需要为每个主机创建一个Ingress对象.

Yes, you need to create one Ingress object per one host, if you want different headers her host.

您可以通过Traefik做到:

You can do it by 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: /

此外,您可以通过 Nginx 入口控制器来完成同样的事情.

Also, the same thing you can do by Nginx Ingress Controller.

它具有对configuration snipper的支持. 此处是使用它为每个Ingress对象设置自定义标头的示例:

It has the support for configuration snipper. Here is an example of using it to set a custom header per Ingress object:

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.

这篇关于使用Traefik在Kubernetes Ingress上为每个规则添加自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆