HA Proxy
Doc
Install & enable
config
- /etc/haproxy/haproxy.cfg
- Sections ref
- global, defaults, frontend, backend
global section
- global section are process-wide and often OS-specific.
- maxconn parameter specifies the maximum number of concurrent connections
- user name and group name for which the haproxy process belongs.
- daemon parameter specifies that haproxy runs as a background process
Defaults section
- TCP works in Lower Layers (Networking concepts and OSI model), Http data is also carried by TCP protocol but HTTP has more information about the http request
Note :
- respective forwarded for must be configured in web server log format (eg: %{X-Forwarded-For}i in apache log format)
- Nginx check rocket chat setting (it has XForward header)
Frontend section (with SSL)
- defines the IP address and port on which the proxy listens
style 2 (https)
Enable stats in frontend
- Add the following in front end section
Backend section
- The back-end system is a pool of real servers, and defines the load balancing (Scheduling) algorithms
sample 1 (roundrobin with check interval)
roundrobin
- Distributes each request sequentially around the pool of real servers
- All the real servers are treated as equals without regard to capacity or load
- (Disadvantage) Different requests takes different time, so load may increase on some servers if equally distributed
- 'check' for health (availability)
- inter = 2seconds is health check interval
- rise = 4, no.of.consecutive health checkups before considering the server is UP (so 4 times with 2 seconds interval = 8s)
- fall = 3, no.of.consecutive health checkups before considering the server is DOWN
sample 2 (url_param)
URL Parameter
- This static algorithm can only be used on an HTTP backend
- Value of Specified URL parameter in the query string is hashed and divided by the total weight of running servers
- If the parameter is missing from the URL, the scheduler defaults to Round-robin scheduling
- say www.mysite.com/?userid=A request goes to app1
- say www.mysite.com/?userid=B request goes to app2
- say www.mysite.com/?userid=X request goes to app1
- say www.mysite.com/?userid=ZV request goes to app2
So algorithm hashes userid param & decides automatically which server to go always
sample 3 (hdr)
- Distributes requests to servers by checking a particular header name in each source HTTP request and performing a hash calculation divided by the weight of all running servers.
- If the header is absent, the scheduler defaults to Round-robin scheduling.
- say request from firefox always goes to app1
- say request from chrome always goes to app2
sample 4 (first)
first
- The first server with available connection slots receives the connection. Once a server reaches its maxconn value, the next server is used
- So once app1 reaches it's limit (1000 connections here), then requests goes to app2
- Useful in cloud scenario where extra server is required/ used on demand only when server1 is full occupied
sample 5 (source)
source
- The same client IP always reaches the same server as long as the server is up
- It is generally used in TCP mode where cookies cannot be inserted
- Requests from same client always goes to same server (a kind of source ip hashing)
sample 6 (leastconn)
leastconn :
- All the real servers are treated as equals without regard to capacity or load
- ideal for an environment where a group of servers have different capacities, request process time varies..etc
- Can use Weights
- As the name specifies, server with least connections get the new request
URI
This algorithm hashes either the left part of the URI (before the question mark) or the whole URI . This ensures that the same URI will always be directed to the same server.
- This is used with proxy caches and anti-virus proxies in order to maximize the cache hit rate. (Http backend)
Access Control List
- blog ref
- doc ref
- to make decision based on request/ response/environmental status
- syntaxacl {name} {criteria} {flags} {operators} {pattern}
- We can create some named conditions & can load some backend based on condition expression
sample 1 (checking header)
sample 2
- Multiple conditions can have same name, so any one conditions satisfies it becomes true (like above image url_static)
Sticky session
- HTTP is not a connected protocol: it means that the session is totally independent from the TCP connections. Session information is saved on the Web server. So for sticky session cookies can be used
- Alternate is to use shared storage for session or save sessions in other database or use state less (REST) api!
Insert new cookie only if doesn't exists
- For new requests if specified (SERVERUSED here) cookie doesn't exists then it's created with respective value (s01/ s02) based on server picked by algorithm
- For requests with existing cookie, cookie value will be compared with config & matching server will be picked
- nocache adds "Cache-Control: nocache" header so that shared cache on the internet doesn't cache it
- Disadvantage : Client will be redirected to same server even if no real session is created in end application code, so better use existing cookie managed (created/ deleted) by application
Existing cookie
- so prefix (if cookie exists) like JSESSIONID=s01~i12KJF23JKJ1EKJ21213KJ while request coming from back end & removes it while going to backend. So for backend it's same unchanged cookie & value
- So sticky if only session cookie exists else roundrobin
Note
- If both haproxy & webserver are on same machine, web server should listen to different port not 80
More ref
- Google cloud best floating ip practices
- howtoforge setup haproxy
- custom cookie routing in nginx
- Header & cookie routing in nginx
- Nginx Plus HA
- digital ocean
Full config
Temp
- /etc/selinux/config
Theory
Why load balancing ?
- Software/ Hardware has maximum capacity, connections cannot be processed more than that capacity . So to increase capacity of our service load balancing is needed.
Solutions
- using DNS
- But has issues like DNS Cache, if one server fails? ..etc
- using load balancer
HAProxy
It is a
- TCP proxy : It can accept a TCP connection from a listening socket, connect to a server and attach these sockets together allowing traffic to flow in both directions
HTTP reverse-proxy: It presents itself as server that sits in front of web servers and forwards client (e.g. web browser) requests to those web servers. Reverse proxies are typically implemented to help increase security (say hiding ports)
SSL terminator: Set up SSL/ TLS connection (say https), If our web servers have HTTPS enabled, the HAProxy will appear a hacker making Man-In-The-Middle Attack, so the SSL certificates must be defined on your HAPROXY system
TCP normalizer: abnormal traffic such as invalid packets or incomplete connections (SYN floods) can be dropped
HTTP normalizer: Can be configured to process only valid/complete http requests. (This protects against a lot of protocol-based attacks).
Server load balancer: Load balance TCP whole connections or per HTTP requests. Server health checks, statistics/ monitoring.
Traffic Regulator: some rate limiting at various points, protect the servers against overloading, adjust traffic priorities based on the contents, and even pass such information to lower layers and outer network components by marking packets
IT is Fast, reliable & open source project
SPOF
- Single point of failure
- Use keepalived/ heartbeat/ pacemaker ..etc