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:
- sudo add-apt-repository ppa:vbernat/haproxy-1.8
- sudo apt-get update
- sudo apt-get install haproxy
Step 2
Now edit haproxy default configuration file /etc/haproxy/haproxy.cfg and start configuration.
- sudo nano /etc/haproxy/haproxy.cfg
This is how the default configuration looks like we are going to keep it as it is.
- global
- log /dev/log local0
- log /dev/log local1 notice
- chroot /var/lib/haproxy
- stats socket /run/haproxy/admin.sock mode 660 level admin
- stats timeout 30s
- user haproxy
- group haproxy
- daemon
-
- # Default SSL material locations
- ca-base /etc/ssl/certs
- crt-base /etc/ssl/private
-
- # Default ciphers to use on SSL-enabled listening sockets.
- # For more information, see ciphers(1SSL). This list is from:
- # https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
- ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256::RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
- ssl-default-bind-options no-sslv3
-
- defaults
- log global
- mode http
- option httplog
- option dontlognull
- timeout connect 5000
- timeout client 50000
- timeout server 50000
- errorfile 400 /etc/haproxy/errors/400.http
- errorfile 403 /etc/haproxy/errors/403.http
- errorfile 408 /etc/haproxy/errors/408.http
- errorfile 500 /etc/haproxy/errors/500.http
- errorfile 502 /etc/haproxy/errors/502.http
- errorfile 503 /etc/haproxy/errors/503.http
- 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.
- frontend Local_Server
- bind 192.168.1.12:80
- mode http
- default_backend My_Web_Servers
For me, my IP is 18.223.102.104 so for me, it will be like below for the frontend. You can use IP, or you can simply put bind *:80.
- frontend Local_Server
- bind 18.223.102.104:80
- mode http
- default_backend My_Web_Servers
Step 4
Now add backend web servers
- backend My_Web_Servers
- balance roundrobin
- server <server1 name> <private IP 1>:80 check
- 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.
- backend My_Web_Servers
- balance roundrobin
- server web1 3.21.237.209 check
- server web2 18.219.54.138 check
- server web3 18.218.240.66 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.
- 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.
- 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
Output 2
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!