<p>Suppose we have a service replicated to several pods. The first request to the service should be  randomly(or by a load balancing algorithm) routed to a pod and the mapping 'value_of_certain_header -> pod_location' should be saved somehow so next request will be routed to specific pod.</p>

<p>Are there any Ingress controllers or other approaches for Kubernetes to implement stickiness to specific pod by request header? Basically I need the same behaviour that haproxy does with its sticky tables.</p>

<p>Assuming that 'pod_location' is inserted into HTML header by app that is running on that pod, the <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/" rel="nofollow noreferrer">Ingress</a> (and <a href="https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/" rel="nofollow noreferrer">Ingress Controller</a>) can be used to achieve header based routing.</p>

<p>For example, Traefik v2.0 has the new Custom Resource Definition (CRD) called IngressRoute that extends the Ingress spec and adds support for features such as Header based routing.</p>

<p>In the following example, I 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 <code>X-Route</code>:</p>

<pre><code>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
</code></pre>

<p>Full <a href="https://github.com/raelga/kubernetes-talks/blob/master/traefik/labs/k8s/default/header-routing.yml" rel="nofollow noreferrer">example</a></p>

<p>With the <em>X-ROUTE: Apache</em> header:</p>

<pre><code>curl http://46.101.68.190/ -H 'X-ROUTE: Apache' 
html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;
</code></pre>

<p>With the <em>X-ROUTE: nginx</em> header:</p>

<pre><code>&gt; curl http://46.101.68.190/ -H 'X-ROUTE: nginx'
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to nginx!&lt;/title&gt;
...and so on...
</code></pre>

<p>And Traefik provides additional info with configuration examples for their <a href="https://docs.traefik.io/middlewares/headers/" rel="nofollow noreferrer">middleware</a> .</p>
