Routing based on request headers using AWS Load Balancer or nginx

1/22/2019

Scenario: I have 2 clients. let's say client A and client B. Both clients have one common id (like UUID). My application(it's c++) is configured like both the clients have to connect in one server.

I am planning to add (AWS)ELB/nginx in front of my application. So Problem is when we received a request from client A it will pass through ELB and connect in one of the nodes. When client B sends the request to ELB then I am not sure that it will connect to the same node where client A is connected. Client B should connect on that node where Client A was connected.

IN MY SCENARIO both clients should connect to the same node. Clients always come in a pair with a common id. WHAT should use in this case?

My application is dockerized and I am using kubenetes for deployments.

-- Sarjan Purohit
aws-elb
aws-load-balancer
kubernetes
load-balancing
nginx

1 Answer

1/22/2019

You can configure your Nginx Ingress Controller and use annotation:

nginx.ingress.kubernetes.io/server-snippet

Your Ingress might look like the following:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
 set $agentflag 0;

 if ($http_user_agent ~* "(Mobile)" ){
 set $agentflag 1;
 }

 if ( $agentflag = 1 ) {
 return 301 https://m.example.com;
 }

Please check a more detailed explanation of Annotations for ingress-nginx on Github.

If you have more than one node, you can schedule POD on the same node by using Node affinity. This can be also achieved by using nodeSelector which is a simple version of Node affinity.

-- Crou
Source: StackOverflow