发表于 2019-1-1 08:33:31

Haproxy会话保持

原文地址: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:

  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

  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  |
  VYES
  Does the request contain persistence information ---------
  ||
  NOV|
  Server choice by load-balancing algorithm|
  ||
  ||
  Forwarding request to the server

页: [1]
查看完整版本: Haproxy会话保持