How to dynamically add an upstream in Nginx?
Asked Answered
D

4

6

I mean add an upstream but not a server in an upstream.

That means I don't have an upstream block like:

upstream backend {
    # ...
}

I want create an upstream block dynamically. That is something like:

content_by_lua_block {
    upstream_block.add('backend');
    upstream_block.add_server('backend', '127.0.0.1', 8080);
    upstream_block.add_server('backend', '127.0.0.1', 8081);
    upstream_block.add_server('backend', '127.0.0.1', 8082);
    upstream_block.del_server('backend', '127.0.0.1', 8080);
}

proxy_pass http://backend
Demulcent answered 22/2, 2017 at 5:5 Comment(2)
I can't imagine any reason for this. Just set a variable and proxypass to itCallboard
@AlexeyTen Imagnine this: I have many machine groups, and each group has its own machines. They are all dynaimical.Demulcent
D
1

I found a nginx module called ngx_http_dyups_module matches my question.

Demulcent answered 23/2, 2017 at 2:51 Comment(0)
M
4

You may use balancer_by_lua* and https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/balancer.md

You will have a full control which upstream is selected for given request.

You may self provision you code or use existing upstream config as the source using https://github.com/openresty/lua-upstream-nginx-module

Maintain answered 23/2, 2017 at 9:41 Comment(0)
D
1

I found a nginx module called ngx_http_dyups_module matches my question.

Demulcent answered 23/2, 2017 at 2:51 Comment(0)
C
0

My example on how to dynamically add upstream servers based on CPU count.

Servers. I used openresty and configured it to listen on multiple ports.

worker_processes auto;
error_log logs/openresty.err ;
events {
  worker_connections 1000;
}
http {
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  logs/openresty.log main;


  server {
    listen 127.0.0.1:8080;
    listen 127.0.0.1:8081;
    listen 127.0.0.1:8082;
    listen 127.0.0.1:8083;
    listen 127.0.0.1:8084;
    listen 127.0.0.1:8085;
    listen 127.0.0.1:8086;
    listen 127.0.0.1:8087;
    listen 127.0.0.1:8088;
    listen 127.0.0.1:8089;
    listen 127.0.0.1:8090;
    server_name *.*;
    location / {
      content_by_lua_block {
        --[[ local NumCores = tonumber(os.getenv("NUMBER_OF_PROCESSORS"))
        local NumCores=10
        ]]
        --
        -- local f = io.popen("ps -ef | grep nginx | wc -l ")
        local f = io.popen("/usr/sbin/sysctl  -n hw.ncpu ")
        ngx.print('CPU count: '..f:read())
        f:close()
      }
    }
  }
}

And the reverse proxy, dynamically add upstream servers based on CPU count.

error_log logs/reverse_openresty.err ;
events {
  worker_connections 1000;
}
http {
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  logs/reverse_openresty.log main;
  upstream backend {
    server 0.0.0.1;   # just an invalid address as a place holder
    balancer_by_lua_block {
      local balancer = require "ngx.balancer"
      local start_port=8080
      local f = io.popen("/usr/sbin/sysctl  -n hw.ncpu ") -- get cpu count
      local cpu_count=tonumber(f:read())
      f:close()
      local max_port=start_port+cpu_count-2
      repeat
        local ok, err = balancer.set_current_peer('127.0.0.1', start_port)
        if not ok then
            ngx.log(ngx.ERR, "failed to set the current peer: ", err)
            return ngx.exit(500)
        end
        start_port=start_port+1
      until start_port>max_port
    }
    keepalive 10;  # connection pool
  }
  server {
    listen 80;
    location / {
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_pass http://backend; # force using http. as node server.js only have http
      }
  }
}

The configuration is tested on MacOs.

Cumulative answered 9/6, 2019 at 15:9 Comment(1)
+1, I know this doesn't answer the question, but it is the first full running example of using balancer_by_lua_block.Woolcott
P
0

I'm using ngx_dynamic_upstream. it's really good at production. i'd forked original from owner and checked source codes for just in case.

Pinzler answered 25/12, 2019 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.