设为首页 收藏本站
查看: 519|回复: 0

[经验分享] Haproxy会话保持

[复制链接]
YunVN网友  发表于 2019-1-1 08:33:31 |阅读模式
原文地址:http://blog.exceliance.fr/2012/03/29/load-balancing-affinity-persistence-sticky-sessions-what-you-need-to-know/
Synopsis
  To ensure high availability and performance of Web applications, it is now common to use a load-balancer.
  While some people uses layer 4 load-balancers, it can be sometime recommended to use layer 7 load-balancers to be more efficient with HTTP protocol.
  NOTE: To understand better the difference between such load-balancers, please read the Load-Balancing FAQ.
A load-balancer in an infrastructure
  The picture below shows how we usually install a load-balancer in an infrastructure:
DSC0000.jpg

  This is a logical diagram. When working at layer 7 (aka Application layer), the load-balancer acts as a reverse proxy.
  So, from a physical point of view, it can be plugged anywhere in the architecture:

  •   in a DMZ
  •   in the server LAN
  •   as front of the servers, acting as the default gateway
  •   far away in an other separated datacenter
Why does load-balancing web application is a problem????
  Well, HTTP is not a connected protocol: it means that the session is totally independent from the TCP connections.
  Even worst, an HTTP session can be spread over a few TCP connections…
  When there is no load-balancer involved, there won’t be any issues at all, since the single application server will be aware the session information of all users, and whatever the number of client connections, they are all redirected to the unique server.
  When using several application servers, then the problem occurs: what happens when a user is sending requests to a server which is not aware of its session?
  The user will get back to the login page since the application server can’t access his session: he is considered as a new user.
  To avoid this kind of problem, there are several ways:

  •   Use a clustered web application server where the session are available for all the servers
  •   Sharing user’s session information in a database or a file system on application servers
  •   Use IP level information to maintain affinity between a user and a server
  •   Use application layer information to maintain persistance between a user and a server
  NOTE: you can mix different technc listed above.
Building a web application cluster
  Only a few products on the market allow administrators to create a cluster (like Weblogic, tomcat, jboss, etc…).
  I’ve never configured any of them, but from Administrators I talk too, it does not seem to be an easy task.
  By the way, for Web applications, clustering does not mean scaling. Later, I’ll write an article explaining while even if you’re clustering, you still may need a load-balancer in front of your cluster to build a robust and scalable application.
Sharing user’s session in a database or a file system
  This Technic applies to application servers which has no clustering features, or if you don’t want to enable cluster feature from.
  It is pretty simple, you choose a way to share your session, usually a file system like NFS or CIFS, or a Database like MySql or SQLServer or a memcached then you configure each application server with proper parameters to share the sessions and to access them if required.
  I’m not going to give any details on how to do it here, just google with proper keywords and you’ll get answers very quickly.
IP source affinity to server
  An easy way to maintain affinity between a user and a server is to use user’s IP address: this is calledSource IP affinity.
  There are a lot of issues doing that and I’m not going to detail them right now (TODO++: an other article to write).
  The only thing you have to know is that source IP affinity is the latest method to use when you want to “stick” a user to a server.
  Well, it’s true that it will solve our issue as long as the user use a single IP address or he never change his IP address during the session.
Application layer persistence
  Since a web application server has to>persistence between a user and a server.
  The information we’ll use is the Session Cookie, either set by the load-balancer itself or using one set up by the application server.
What is the difference between Persistence and Affinity
  Affinity: this is when we use an information from a layer below the application layer to maintain a client request to a single server
  Persistence: this is when we use Application layer information to stick a client to a single server
  sticky session: a sticky session is a session maintained by persistence

  The main advantage of the persistence over affinity is that it’s much more accurate, but sometimes, Persistence is not doable, so we must>  Using persistence, we mean that we’re 100% sure that a user will get redirected to a single server.
  Using affinity, we mean that the user may be redirected to the same server…
What is the interraction with load-balancing???
  In load-balancer you can choose between several algorithms to pick up a server from a web farm to forward your client requests to.
  Some algorithm are deterministic, which means they can use a client side information to choose the server and always send the owner of this information to the same server. This is where you usually do Affinity
DSC0001.jpg

  IE: “balance source”
  Some algorithm are not deterministic, which means they choose the server based on internal information, whatever the client sent. This is where you don’t do any affinity nor persistence

  IE: “balance roundrobin” or “balance leastconn”
  I don’t want to go too deep in details here, this can be the purpose of a new article about load-balancing algorithms…
  You may be wondering: “we have not yet speak about persistence in this chapter”. That’s right, let’s do it.
  As we saw previously, persistence means that the server can be chosen based on application layer information.
  This means that persistence is an other way to choose a server from a farm, as load-balancing algorithm does.
  Actually, session persistence has precedence over load-balancing algorithm.
  Let’s show this on a diagram:
client request --> HAProxy Frontend --> backend choice --> HAproxy Backend  |
  V  YES
  Does the request contain persistence information ---------
  |  |
  NO  V  |
  Server choice by load-balancing algorithm  |
  |  |
  |  |
  Forwarding request to the server


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-658115-1-1.html 上篇帖子: haproxy ssl 配置方式 下篇帖子: mycat+haproxy服务与高可用(3)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表