Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headers based routing with Kubernetes

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.

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.

like image 532
Zalizniak Avatar asked Oct 30 '25 12:10

Zalizniak


1 Answers

Assuming that 'pod_location' is inserted into HTML header by app that is running on that pod, the Ingress (and Ingress Controller) can be used to achieve header based routing.

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.

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

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>
...and so on...

And Traefik provides additional info with configuration examples for their middleware .

like image 77
Nick Avatar answered Nov 01 '25 04:11

Nick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!