How to config my nginx.conf inside the openresty pod to route requests by query args to different k8s pods and keep the connections alive?

9/3/2019

I'm write a distribution system, the service is partition by a query argument

/any/path?accountId=1001xxxx&... # to single pod service-1001
/any/path?accountId=1002xxxx&... # to single pod service-1002

After a huge requests, there remain huge connections with TIME_WAIT state. So i have to conside about setting the keep-alive to the connections.And i meet troubles.

    1. Target host is a domain name and must suffix with namespace,Like:service-1001.prod.svc.cluster.local:8080,service-1001.dev.svc.cluster.local:8080,service-1001.qa.svc.cluster.local:8080,
    1. Environment variables cannot be used in upstream

So,is there some way to get result like:

env NAMESPACE;

upstream service-(.*){
   server service-($1).${NAMESPACE}.svc.cluster.local:8080 resolve;
   keepalive 60;
}

server {
    location / {
            set $target '';
            set_by_lua_file $target lua/traget.lua; # service-1001 or service-1002,just a single pod.
            proxy_set_header Connection "";
            proxy_http_version 1.1;
            proxy_pass http://$target;
    }
}

Thank you very much!

I have known a way to get this,but is not flexible at all:

  upstream service_dev_1001 {
        1001.dev.svc.cluster.local:8080
  }

  upstream service_qa_1001 {
        1001.qa.savc.cluster.local:8080
  }

  upstream service_prod_1001 {
        1001.prod.svc.cluster.local:8080
  }
  location / {
            set $target '';
            set_by_lua_file $target lua/traget.lua; # service_dev_1001,service_qa_1001,...
            proxy_set_header Connection "";
            proxy_http_version 1.1;
            proxy_pass http://$target;
  }
  ....
-- Keto
kubernetes
nginx
openresty

0 Answers