4
votes

Is it possible to configure an ingress controller in Kubernetes to route the HTTP requests to a service only if the incoming requests have a certain value for a header?

Example

An HTTP request with following header

X-MY-CUSTOM-HEADER: accepted-value

should be forwarded to service1

An HTTP request with following header

X-MY-CUSTOM-HEADER: invalid-value

should be blocked

If is possible could you detail a bit or point to some documentation as I wasn't able to find documentation for such usecase

2
There's an example in Istio docs istio.io/docs/concepts/traffic-management/#precedence . It uses envoy proxy under the hood, so I believe you can use it directly as ingress controller in cluster, though I've no experience with it.Egor Stambakio

2 Answers

3
votes

If you are using an nginx ingress controller you can do it with a Configuration snippet annotation. Then you can add something like this:

nginx.ingress.kubernetes.io/configuration-snippet: |
  map $http_x_custom_header $not_ok {
      default "1";
      Value1  "0";
      Value2  "0";
      Value3  "0";
  }

  if ($not_ok) {
      return 403; 
  }

Some more info here.

0
votes

Traefik 2.0, Istio and Ambassador support Header based routing.

More information from https://discuss.kubernetes.io/t/header-based-ingress-routing/6322