Dynamic server selection


#1

Hello, we are trying to set up a configuration were the server (not the backend) is dynamically selected by a LUA script.

We came up with this conf which is not working:

global
   lua-load choose-server.lua

defaults
    mode    http

frontend http-in
    bind *:10001

    # The LUA variable is working in case we use to select the backend:
    #use_backend %[lua.choose_backend]
    default_backend dynamic

backend dynamic
    # Instead in this case it is not working
    server s1 %[lua.choose_server]

When starting the server we get this error:
[ALERT] 061/171533 (3343) : parsing [haproxy.conf:16] : 'server s1' : invalid address: '%[lua.choose_server]' in '%[lua.choose_server]'

Is this not supported? Is there any other way to achieve the same result?

Thanks


#2

Hi curtoni -

I think you need to spell out your servers in the backend, and use a different keyword to choose which specific one. Maybe something like:

backend dynamic
  use-server %[lua.choose_server]
  server s1 <ip>
  server s1 <ip>

I haven’t tested the config, so you may need to play around with it.

Here’s docs on the use-server keyword: https://cbonte.github.io/haproxy-dconv/configuration-1.6.html#4.2-use-server

Hope that helps!

  • Andrew

#3

I don’t mean to drag this thread up from the dead, but I have the same use case as curtoni, and I’m not sure that the solution that @ahayworth proposed is working properly. I’m using the following configuration on my testing instance:

# Some configuration options are redacted - the actual configuration is perfectly valid except for this issue.
global
    lua-load /root/nexus_route.lua
defaults
    mode    http
backend eta
    use-server %[lua.get_server_for_nexus] if { path_beg -i /api/nexus/new /nexus/table /socket.io/nexus }
    # Server declarations

And I’m getting the following error when I start HAProxy: config : backend 'eta' : unable to find server '%[lua.get_server_for_nexus]' referenced in a 'use-server' rule.

Does anyone know if the post above is still valid for version 1.8.3?