负载均衡(HAProxy 或其他)- 粘性会话

发布于 2024-11-17 10:22:47 字数 308 浏览 2 评论 0原文

我正在努力将我的应用程序扩展到多个服务器,一个要求是客户端始终与同一服务器通信(使用太多实时数据以允许在服务器之间有效地反弹)。

我当前的设置是一个小型服务器集群(使用 Linode)。我有一个使用“平衡源”运行 HAProxy 的前端节点,以便 IP 始终指向同一节点。

我注意到“平衡源”的分布不是很均匀。根据我当前的测试设置(2 个后端服务器),当使用 80-100 个源 IP 的样本大小时,一台服务器的连接数通常是 3-4 倍。

有没有办法实现更均衡的分配?显然,粘性会话会阻碍“完美”平衡,但 40/60 的分割比 25/75 的分割更可取。

I'm working on scaling out my app to multiple servers, and one requirement is that a client is always communicating with the same server (too much live data is used to allow bouncing between servers efficiently).

My current setup is a small server cluster (using Linode). I have a frontend node running HAProxy using "balance source" so that an IP is always pointed towards the same node.

I'm noticing that "balance source" is not a very even distribution. With my current test setup (2 backend servers), one server often has 3-4x as many connections when using a sample size of 80-100 source IPs.

Is there any way to achieve a more balanced distribution? Obviously sticky sessions prohibits a "perfect" balance, but a 40/60 split would be preferred to a 25/75 split.

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

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

发布评论

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

评论(2

痞味浪人 2024-11-24 10:22:47

HAProxy 支持修改或插入 cookie,以通过 cookie 参数提供会话持久性。

在后端或监听部分中,添加以下内容:

cookie COOKIENAME prefix

此示例将通过将服务器名称添加到名为 COOKIENAME 的 cookie 来修改现有 cookie。您的客户端将看到类似 server1~someotherdata 的内容,但您的应用程序只会看到 someotherdata 部分。因此您可以在现有的 cookie 上使用它。此外,此方法允许您仅在该 cookie 存在时强制执行会话持久性,这意味着您仍然可以均匀地平衡站点静态部分周围的人员,并且仅在需要时强制执行粘性,但将该 cookie 名称添加到会话中。

还要命名您的服务器,以便您的服务器行如下所示:

server server1 1.2.3.4 cookie server1

更多详细信息位于 HAProxy 配置指南,看起来您也可以使用 appsession 配置参数。

完成此操作后,您可以从列表中选择自己的平衡方法,我倾向于使用 Roundrobin,但一旦进行粘性会话,leastconn 可能会为您提供更好的平衡考虑到。


更多来自文档,以便更轻松地找到参考部分:

cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
              [ postonly ] [ preserve ] [ domain <domain> ]*
              [ maxidle <idle> ] [ maxlife <life> ]
  Enable cookie-based persistence in a backend.
  May be used in sections :   defaults | frontend | listen | backend
                                 yes   |    no    |   yes  |   yes

HAProxy supports modifying or inserting a cookie to provide session persistence with the cookie parameter.

In either backend or listen sections, add the following:

cookie COOKIENAME prefix

This example will modify an existing cookie by adding the name of the server to a cookie called COOKIENAME. Your client will see something like server1~someotherdata but your application will only see the someotherdata part. So you can use this on existing cookies. Additionally this method allows you to only enforce session persitence when that cookie exists, meaning you can still evenly balance people around the static portions of your site and only enforce stickyness when needed, but adding that cookie name to the session.

Also name your servers, so your server lines look like the following:

server server1 1.2.3.4 cookie server1

More detail is in the HAProxy config guide, it also looks like you can use the appsession config parameter as well.

Once you've done this, you can pick your own balance method from the list, I tend to use roundrobin but leastconn might give you a better balance once sticky sessions are taken into account.


More from documentation to make it easier to find reference section:

cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
              [ postonly ] [ preserve ] [ domain <domain> ]*
              [ maxidle <idle> ] [ maxlife <life> ]
  Enable cookie-based persistence in a backend.
  May be used in sections :   defaults | frontend | listen | backend
                                 yes   |    no    |   yes  |   yes
潜移默化 2024-11-24 10:22:47

您可以调整 HA-Proxy 中的平衡算法,但有一些可用。例如roundrobin 或leastconn。

但总体而言,您需要根据为其提供内容的用户域来调整平衡。大多数时候,您需要进行实证测试并根据您的发现重申您的决定。

You can adjust the balancing algorithm in HA-Proxy there are some available though. Like e.g. roundrobin or leastconn.

But you need to adjust in general your balancing according to the domain of users for whom content is served. Most of the times you need to do empirical tests and reiterate your decision according to your findings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文