Add a custom header per rule on Kubernetes Ingress with Traefik

3/19/2018

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

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.

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.

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

-- stefanobaldo
kubernetes
traefik

1 Answer

3/19/2018

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

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: /

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

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.

-- Anton Kostenko
Source: StackOverflow