How To Load Balance Multiple Servers Using Haproxy In AWS


So today, I am going to walk you through step by step procedure for creating many servers and load balance them in AWS. So for load balancing, we need to have servers created and I have written step by step instructions for creating different servers in AWS and host different applications we are going to load balance them. These are the articles.
There are a lot of ways you can load balance your servers, but I am going to create one EC2 instance and install Haproxy to load balance the servers. You can follow this (How To Create AWS EC2 Instance And Host PHP Applications) until step ten (step 10) to create an Ubuntu server in EC2 so that we can install Haproxy and configure it for load balancing. So once you have created the server and connect to it, then you can follow the following steps.
Step 1
Install Haproxy using the following commands:
  1. sudo add-apt-repository ppa:vbernat/haproxy-1.8  
  2. sudo apt-get update  
  3. sudo apt-get install haproxy  
Step 2
Now edit haproxy default configuration file /etc/haproxy/haproxy.cfg and start configuration.
  1. sudo nano  /etc/haproxy/haproxy.cfg  
This is how the default configuration looks like we are going to keep it as it is.
  1. global  
  2.     log /dev/log    local0  
  3.     log /dev/log    local1 notice  
  4.     chroot /var/lib/haproxy  
  5.     stats socket /run/haproxy/admin.sock mode 660 level admin  
  6.     stats timeout 30s  
  7.     user haproxy  
  8.     group haproxy  
  9.     daemon  
  11.     # Default SSL material locations  
  12.     ca-base /etc/ssl/certs  
  13.     crt-base /etc/ssl/private  
  15.     # Default ciphers to use on SSL-enabled listening sockets.  
  16.     # For more information, see ciphers(1SSL). This list is from:  
  17.     #  
  18.     ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256::RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS  
  19.     ssl-default-bind-options no-sslv3  
  21. defaults  
  22.     log global  
  23.     mode    http  
  24.     option  httplog  
  25.     option  dontlognull  
  26.         timeout connect 5000  
  27.         timeout client  50000  
  28.         timeout server  50000  
  29.     errorfile 400 /etc/haproxy/errors/400.http  
  30.     errorfile 403 /etc/haproxy/errors/403.http  
  31.     errorfile 408 /etc/haproxy/errors/408.http  
  32.     errorfile 500 /etc/haproxy/errors/500.http  
  33.     errorfile 502 /etc/haproxy/errors/502.http  
  34.     errorfile 503 /etc/haproxy/errors/503.http  
  35.     errorfile 504 /etc/haproxy/errors/504.http  
Step 3
Now we are going to add Haproxy listener I mean we are going to tell Haproxy to where to listen and for that, add the following to your configuration file.
  1. frontend Local_Server  
  2.     bind  //Your public IP  
  3.     mode http  
  4.     default_backend My_Web_Servers  
For me, my IP is so for me, it will be like below for the frontend. You can use IP, or you can simply put bind *:80.
  1. frontend Local_Server  
  2.     bind  
  3.     mode http  
  4.     default_backend My_Web_Servers  
Step 4
Now add backend web servers
  1. backend My_Web_Servers  
  2.    balance roundrobin  
  3.    server <server1 name> <private IP 1>:80 check  
  4.    server <server2 name> <private IP 2>:80 check  
So, in my previous article, I have created 3 servers that are ubuntu with apache, ubuntu with Ngnix, and Widows with IIS so, I am going to use my all the server's IP for load balancing.
  1. backend My_Web_Servers  
  2.    balance roundrobin  
  3.    server web1 check  
  4.    server web2 check  
  5.    server web3 check    
Now save the file using CTRL+O and CTRL+X. We need to check if our configuration file is valid using the below command.
  1. haproxy -c -f /etc/haproxy/haproxy.cfg  
If this command outputs nothing, then restart Haprocy using the below command and you're ready to go.
  1. sudo service haproxy restart  
Now open your haproxy machine IP in a browser and refresh. With each request, it will redirect the load to different servers, like this:
Output 1
How To Load Balance Multiple Servers Using Haproxy In AWS
Output 2
How To Load Balance Multiple Servers Using Haproxy In AWS 
You can see that the IP is the same, but it forwards the request to different servers. I hope this is helpful.
Thanks for reading!