Load balancing helps the server handle a large number of requests.
As usual, the request from the client goes directly to a single web server, this can cause server congestion, slow processing speed.
Load balancing will be based on algorithms depending on need to control other web servers.
There are 3 types of load balancing algorithms:
- Round robin : this method selects servers sequentially. The load balancer will select the first server in its list for the first request, then move down to the next server in the list in order and start from the beginning when the list is exhausted.
- Least connection : The load balancing system will choose the server with the fewest connections and this method is recommended when access speed is slow.
- Source : with the Source algorithm, the load balancer selects the server based on a sequence of origin IPs of the request, such as the visitor’s IP. This method ensures that a particular user will always connect to the same server.
Load balancing includes 2 types:
- Hardware: Uses hardware to control the request. Devices such as: Foundry, Nortel (Alteon) or Cisco CSS (Arrowpoint), Cisco Local Director…
- Software: Use software to handle requests. Software such as: Nginx, Haproxy, Apache ….
Load balancing includes 2 models: 4 layers and 7 layers.
Here’s how to install 4-layer Soft Load Balancer and use Haproxy:
- Load Balancer
OS: CentOS 7 + haproxy
Private IP: 192.168.1.129:80 - Web Server 1
OS: CentOS 7 + Nginx
Private IP: 192.168.129:8080 - Web Server 2
OS: CentOS 6+ Nginx
Private IP: 192.168.128:80
At the Load Balancer and Web Server 1, open ports 80 and 8080. At Web Server 2, open port 80. Web Server 1 will have the content ‘a,’ and Web Server 2 will have the content ‘b.’
Install Haproxy on Load Balancer
sudo yum install haproxy
Then edit the file /etc/haproxy/haproxy.cfg. Make a backup of this file.
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
stats enable
stats hide-version
stats refresh 30s
stats show-node
stats auth admin:123456
stats uri /haproxy?stats
frontend http-in
bind *:80
default_backend servers
backend servers
balance roundrobin
server webserver1 192.168.1.128:80 check
server webserver2 192.168.1.129:8080 check
Run command sudo service haproxy start
Type in the url 192.168.1.129 and you will see the content change to “a” and “b”.
To see information about web servers we access 192.168.1.129/haproxy?stats with the account admin/123456