Hello!
On Tue, Mar 1, 2016 at 4:51 PM, RJoshi <rohit....@gmail.com> wrote:
> In my current openresty deployment, I have an upstream_server.conf which
> contains 20+ upstream blocks. As we add a new endpoint(ELB), I have to
> manually update this configuration file and restart nginx. Upstream backend
> information is stored in the MySQL database along with service endpoint and
> other routing information.
>
>
> 1. Can I achieve above without restarting nginx using balancer_by_lua? I can
> read the DB and get all domain/port/conn-pool size etc.
>
This looks like a perfect use case of balancer_by_lua*. But you'll need to
1. load the peers metadata from MySQL nonblockingly via
lua-resty-mysql in the context of earlier phase handlers like
access_by_lua*, and
2. pass the metadata obtained in access_by_lua* or alike to
balancer_by_lua* via the ngx.ctx magic table.
It's better to
1. cache the metadata by means of lua_shared_dict and/or
lua-resty-lrucache to reduce expensive mysql queries to a neglectable
level, and
2. enable backend connection pool via the standard keepalive directive
in your upstream {} block:
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
> 2. Do I need multiple upstream backend { } to match my current config? If
> yes, h How can I register multiple upstream backend { } sections
> dynamically?
Nope. You only need a single upstream block. You do the host dispatch
in balancer_by_lua* itself. So that you can run thousands or even
millions of sites with a single upstream {} block and a single
connection pool.
> 3. Does balancer.set_current_peer(host, port) add the host/port to current
> upstream connection pool or just used for routing current request?
>
The sock address you set in Lua is used for both the current request
and any subsequent requests that can reuse this connection (if a later
request sets a different peer address, then it cannot reuse this very
connection in the pool).
Regards,
-agentzh