# Header based ingress routing

**URL:** <https://discuss.kubernetes.io/t/header-based-ingress-routing/6322>\
**Category:** General Discussions\
**Created:** [May 14, 2019, 9:35pm UTC](https://discuss.kubernetes.io/t/header-based-ingress-routing/6322 "2019-05-14T21:35:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rael](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/rael/32/2769_2.png) [@rael](https://discuss.kubernetes.io/u/rael)\
**Post date:** [May 14, 2019, 9:35pm UTC](https://discuss.kubernetes.io/t/header-based-ingress-routing/6322/1 "2019-05-14T21:35:04Z")

</div>

From a slack Question:

> ok so I have a problem to solve… I need to do ingress-type routing for incoming requests which have a special X-ROUTE: header which tells me which sub-application to route to… I know I know… I have tried to get them to use Host: header or URL to separate them but this is not getting changed.

> **[Slack is your productivity platform](https://kubernetes.slack.com/?redir=%2Farchives%2FC09NXKJKA%2Fp1557771501145600%3Fname%3DC09NXKJKA%26perma%3D1557771501145600)**
>
> Slack is a new way to communicate with your team. It’s faster, better organized, and more secure than email.

Traefik 1.7 as Ingress Controller doesn’t support routing on non-path options, as is not supported by the Kubernetes Ingress spec ([Support query matcher for kubernetes ingress · Issue #3840 · traefik/traefik · GitHub](https://github.com/containous/traefik/issues/3840#issuecomment-417348873)).

But with Traefik 2.0, the new Custom Resource Definition from Traefik called IngressRoute extends the Ingress spec and adds support for Traefik features such as Header based routing.

In the following example, we have two services: one exposing an Nginx deployment and other one exposing an Apache deployment. With the IngressRoute CRD, the match for the router will be the header **X-ROUTE** :

```
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: headers
spec:
  entrypoints:
    - web
    - websecure
  routes:
  - match: Headers(`X-ROUTE`,`Apache`)
    kind: Rule
    services:
    - name: apache
      port: 80
  - match: Headers(`X-ROUTE`,`nginx`)
    kind: Rule
    services:
    - name: nginx
      port: 80

```

Full example: [https://github.com/raelga/kubernetes-talks/blob/master/traefik/labs/k8s/default/header-routing.yml](https://github.com/raelga/kubernetes-talks/blob/master/traefik/labs/k8s/default/header-routing.yml)

With the _X-ROUTE: Apache_ header:

```
❯ curl http://46.101.68.190/ -H 'X-ROUTE: Apache'  
<html><body><h1>It works!</h1></body></html>

```

With the _X-ROUTE: nginx_ header:

```
❯ curl http://46.101.68.190/ -H 'X-ROUTE: nginx' 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

```

Traefik 2.0 is in alpha stages, so not suited for production environments at this point.

More information in:

- [Traefik | Traefik | v2.0](https://docs.traefik.io/v2.0/)
- [Traefik](https://talks.godoc.org/github.com/raelga/kubernetes-talks/traefik/traefik.slide)
- [https://github.com/raelga/kubernetes-talks/tree/e0904b39c8796a41755cd4ed148d794a6e99b944/traefik/labs/k8s/traefik](https://github.com/raelga/kubernetes-talks/tree/e0904b39c8796a41755cd4ed148d794a6e99b944/traefik/labs/k8s/traefik)

Aside from Traefik, other Ingress controllers that support Header based routing are:

- Istio - [Istio / Request Routing](https://istio.io/docs/tasks/traffic-management/request-routing/)
- Ambassador - [https://www.getambassador.io/reference/headers](https://www.getambassador.io/reference/headers)

---

<div class="post-metadata">

**Author:** ![Whylie\_MedKnows](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/whylie_medknows/32/5938_2.png) [@Whylie\_MedKnows](https://discuss.kubernetes.io/u/Whylie_MedKnows)\
**Post date:** [September 29, 2020, 8:09am UTC](https://discuss.kubernetes.io/t/header-based-ingress-routing/6322/2 "2020-09-29T08:09:29Z")

</div>

Thanks @rael for providing a sample. I tried header based routing with traefik 2.0 on my docker desktop. I see “work in progress” screen when accessing the dashboard, I was able to get the data from “[http://localhost:30889/api/rawdata](http://localhost:30889/api/rawdata)”. However, when I try to do curl on localhost by giving the X-ROUTE header, I’m getting the error " Could not resolve host: nginx". I see that both nginx and apache pods are up and running. Could you please suggest on how I get this working?

{“kubernetescrd”:{“HTTP”:{“routers”:{“default/headers-26d3385f527366bae985”:{“entryPoints”:null,“service”:“default/headers-26d3385f527366bae985”,“rule”:“Headers(`X-ROUTE`,`Apache`)”},“default/headers-86e05b7392768b133fae”:{“entryPoints”:null,“service”:“default/headers-86e05b7392768b133fae”,“rule”:“Headers(`X-ROUTE`,`nginx`)”}},“services”:{“default/headers-26d3385f527366bae985”:{“loadbalancer”:{“servers”:[{“url”:“[http://10.1.0.219:80](http://10.1.0.219:80)”,“weight”:1}],“method”:“wrr”,“passHostHeader”:true}},“default/headers-86e05b7392768b133fae”:{“loadbalancer”:{“servers”:[{“url”:“[http://10.1.0.218:80](http://10.1.0.218:80)”,“weight”:1}],“method”:“wrr”,“passHostHeader”:true}}}},“TCP”:{},“TLSOptions”:null,“TLSStores”:null}}
