负载平衡(HAProxy或其他)-粘性会话 [英] Load Balancing (HAProxy or other) - Sticky Sessions

查看:117
本文介绍了负载平衡(HAProxy或其他)-粘性会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将我的应用扩展到多台服务器,并且一个要求是客户端始终与同一台服务器通信(太多实时数据用于有效地在服务器之间反弹).

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).

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

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.

我注意到余额来源"不是一个非常均匀的分布.根据我当前的测试设置(2台后端服务器),当使用80-100个源IP的样本大小时,一台服务器的连接数量通常是其3-4倍.

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.

有什么方法可以实现更均衡的分配?显然,粘性会话禁止完美"的平衡,但是40/60的分配比25/75的分配更可取.

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.

推荐答案

HAProxy支持修改或插入cookie,以提供具有cookie参数的会话持久性.

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

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

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

有关详细信息,请参见 HAProxy配置指南,就像您也可以使用appsession config参数一样.

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

完成此操作后,您可以从列表中选择自己的余额计算方法,我倾向于使用roundrobin,但是一旦考虑到粘性会话,leastconn可能会为您提供更好的余额.

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

这篇关于负载平衡(HAProxy或其他)-粘性会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆