【问题标题】:Custom load balancing in HAProxyHAProxy 中的自定义负载均衡
【发布时间】:2018-04-29 00:30:20
【问题描述】:

我正在使用 HAProxy 来平衡我的 HTTP 请求。我想知道是否有任何方法可以根据每个服务器返回的响应自定义后端服务器的选择。我有一个 servlet 可以返回响应(连接到它的客户端数量)。我想使用此信息并将请求路由到编号最小的后端服务器。

我的 HAProxy 配置如下:

listen http_front xx.xx.xx.xx:8080
  mode http
  option httpchk GET /servlet/GetClientCountServlet
  server app1 xx.xx.xx.xx:8080 check port 8080
  server app2 xx.xx.xx.xx:8080 check port 8080
  server app3 xx.xx.xx.xx:8080 check port 8080

【问题讨论】:

    标签: load-balancing haproxy


    【解决方案1】:

    leastconn 平衡模式不适合您的用例吗?否则我可以使用 Lua 脚本自定义使用 HAProxy 完成负载平衡的方式

    【讨论】:

      【解决方案2】:

      当我正在寻找相同方向的解决方案时,这可能会有所帮助:

      通过自定义 lua 脚本进行负载平衡

      创建一个名为 least_sessions.lua 的文件并添加以下代码:

      local function backend_with_least_sessions(txn)
          -- Get the frontend that was used
          local fe_name = txn.f:fe_name()
      
          local least_sessions_backend = ""
          local least_sessions = 99999999999
      
          -- Loop through all the backends. You could change this
          -- so that the backend names are passed into the function too.
          for _, backend in pairs(core.backends) do
      
      
              -- Look at only backends that have names that start with
              -- the name of the frontend, e.g. "www_" prefix for "www" frontend.
              if backend and backend.name:sub(1, #fe_name + 1) == fe_name .. '_' then
                  local total_sessions = 0
      
                  -- Using the backend, loop through each of its servers
                  for _, server in pairs(backend.servers) do
      
                      -- Get server's stats
                      local stats = server:get_stats()
      
                      -- Get the backend's total number of current sessions
                      if stats['status'] == 'UP' then
                          total_sessions = total_sessions + stats['scur']
                          core.Debug(backend.name .. ": " .. total_sessions)
                      end
                  end
      
                  if least_sessions > total_sessions then
                      least_sessions = total_sessions
                      least_sessions_backend = backend.name
                  end
              end
          end
      
          -- Return the name of the backend that has the fewest sessions
          core.Debug("Returning: " .. least_sessions_backend)
          return least_sessions_backend
      end
      
      core.register_fetches('leastsess_backend', backend_with_least_sessions)
      

      此代码将遍历所有以与当前前端相同字母开头的后端,例如为前端 www 查找后端 www_dc1 和 www_dc2。然后它将找到当前会话最少的后端并返回其名称。

      使用 lua-load 指令将文件加载到 HAProxy 中。然后,将 use_backend 行添加到您的前端,以将流量路由到具有最少活动会话的后端。

      global
          lua-load /path/to/least_sessions.lua
      
      frontend www
          bind :80
          use_backend %[lua.leastsess_backend]
      
      backend www_dc1
          balance roundrobin
          server server1 192.168.10.5:8080 check maxconn 30
      
      backend www_dc2
          balance roundrobin
          server server1 192.168.11.5:8080 check maxconn 30
      

      更多详情: https://www.haproxy.com/de/blog/5-ways-to-extend-haproxy-with-lua/

      【讨论】:

        猜你喜欢
        • 2021-09-27
        • 1970-01-01
        • 1970-01-01
        • 2021-10-12
        • 2017-04-12
        • 2018-11-16
        • 2016-03-08
        • 1970-01-01
        相关资源
        最近更新 更多