使用Haproxy LoadBalancer使用自定义哈希功能

发布于 2025-01-26 08:35:58 字数 525 浏览 3 评论 0原文

我已经在前端使用Haproxy配置了负载平衡器,在后端我有4个服务器使用Apache Web服务器服务请求。

#HAProxy for web servers
frontend web-frontend
  bind IPADDRESS_LB:80
  mode http
  default_backend web-backend

backend web-backend
  balance roundrobin
  server web-server1 IPADDRESS1:80 check
  server web-server2 IPADDRESS2:80 check
  server web-server3 IPADDRESS3:80 check
  server web-server4 IPADDRESS4:80 check

我的要求是由循环机制中的任何一种机器提供的。但是现在,我想实现自己的算法,以根据请求参数(例如IP)发送请求。

就像根据我可以将请求路由到一个后端服务器的结果来实现自己的哈希功能一样。

我该怎么做?

I have configured a load balancer using HAProxy on frontend and on the backend I have 4 server serving the request using Apache web server.

#HAProxy for web servers
frontend web-frontend
  bind IPADDRESS_LB:80
  mode http
  default_backend web-backend

backend web-backend
  balance roundrobin
  server web-server1 IPADDRESS1:80 check
  server web-server2 IPADDRESS2:80 check
  server web-server3 IPADDRESS3:80 check
  server web-server4 IPADDRESS4:80 check

My requests are served by either of the machines in a round robin mechanism. But now I want to implement my own algorithm to send the request based on the request parameters, eg IP.

Like Implementing my own hash function based on the result of which I can route my request to either of the backend servers.

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2025-02-02 08:35:58

您可以参数化在哈希中使用的内容(源具有示例: Haproxy的博客

在您的情况下,只需使用balance source基于源(ip)的哈希:(

#HAProxy for web servers
frontend web-frontend
  bind IPADDRESS_LB:80
  mode http
  default_backend web-backend

backend web-backend
  balance source
  hash-type consistent
  server web-server1 IPADDRESS1:80 check
  server web-server2 IPADDRESS2:80 check
  server web-server3 IPADDRESS3:80 check
  server web-server4 IPADDRESS4:80 check

hash-type一致< /code>确保不同的IP哈希给出相同的结果)

或基于 url参数命名ip

#HAProxy for web servers
frontend web-frontend
  bind IPADDRESS_LB:80
  mode http
  default_backend web-backend

backend web-backend
  balance url_param ip
  hash-type consistent
  server web-server1 IPADDRESS1:80 check
  server web-server2 IPADDRESS2:80 check
  server web-server3 IPADDRESS3:80 check
  server web-server4 IPADDRESS4:80 check

要将哈希基于多个参数,您可以:

  1. 添加一个标题的标题包含您想要使用此标头的数据
  2. 以进行平衡的数据。

示例来自 /a>:

  backend bk_static
    http-request set-header X-LB %[req.hdr(Host),lower]%[req.uri,lower]
    balance hdr(X-LB)
    hash-type consistent

You can parametrize what to use in the hash (source with examples : HaProxy's blog)

In your case, simply use balance source to hash based on the source (IP) :

#HAProxy for web servers
frontend web-frontend
  bind IPADDRESS_LB:80
  mode http
  default_backend web-backend

backend web-backend
  balance source
  hash-type consistent
  server web-server1 IPADDRESS1:80 check
  server web-server2 IPADDRESS2:80 check
  server web-server3 IPADDRESS3:80 check
  server web-server4 IPADDRESS4:80 check

(hash-type consistent makes sure that distinct IP hashes give the same result)

Or based on the URL parameter named ip :

#HAProxy for web servers
frontend web-frontend
  bind IPADDRESS_LB:80
  mode http
  default_backend web-backend

backend web-backend
  balance url_param ip
  hash-type consistent
  server web-server1 IPADDRESS1:80 check
  server web-server2 IPADDRESS2:80 check
  server web-server3 IPADDRESS3:80 check
  server web-server4 IPADDRESS4:80 check

To base the hash on multiple parameters, you can :

  1. add a header to the request containing the data you want a hash of
  2. use this header for balancing.

Example from HaProxy's blog :

  backend bk_static
    http-request set-header X-LB %[req.hdr(Host),lower]%[req.uri,lower]
    balance hdr(X-LB)
    hash-type consistent
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文