18.3 Manual load balancing
Why you should do it by hand once
Before AWS picks the server, you should feel what "picking" means. You have two servers. You send some users to the first IP and some users to the second IP. That is manual load balancing. No AWS balancer exists yet. You are the balancer.
What you are doing
You launch two EC2 instances. Nginx answers on both, with different words on the page so you can tell them apart. You open IP 1, then IP 2. Optional DNS round robin means two A records for the same name. You decide the split. Nothing checks health for you.
How to put Nginx on both instances
Repeat this on instance A, then on instance B. Use the sentence for that instance.
- SSH to instance A.
- On Amazon Linux, run
sudo yum install -y nginx. - On Ubuntu, run
sudo apt update. - On Ubuntu, run
sudo apt install -y nginx. - Skip the lines that are not your operating system.
- Run
echo 'This is server A, the shop' | sudo tee /usr/share/nginx/html/index.html. - Run
sudo service nginx start. - Run
sudo service nginx status. - Run
curl -s http://127.0.0.1. - You should see
This is server A, the shop, and the service reported as running. - SSH to instance B.
- Install Nginx the same way, with
sudo yumorsudo apt, matching the operating system. - Run
echo 'This is server B, the api' | sudo tee /usr/share/nginx/html/index.html. - Run
sudo mkdir -p /usr/share/nginx/html/api. - Run
echo 'api on server B' | sudo tee /usr/share/nginx/html/api/index.html. - Run
sudo service nginx start. - Run
curl -s http://127.0.0.1. - Run
curl -s http://127.0.0.1/api/. - You should see
This is server B, the api, and thenapi on server B.
If this box is Apache, the file is /var/www/html/index.html, the service is sudo service httpd start, and the status command is sudo service httpd status. The rest of the steps stay the same.
- In the EC2 console, allow inbound HTTP port 80 from your own IP on both instance security groups.
- Copy instance A's public IPv4. Call it IP1.
- Copy instance B's public IPv4. Call it IP2.
- Open
http://IP1/in the browser. - You should see
This is server A, the shop. - Open
http://IP2/in the browser. - You should see
This is server B, the api. - Open
http://IP2/api/. - You should see
api on server B.
You just did the split yourself. Some people get IP1. Some people get IP2. You told them which one.
How DNS round robin does the same job, still with no health check
Do this only if example.com is a zone you control from Chapter 9. If it is not, write the two records on paper and skip the console clicks.
- Open the DNS zone for example.com.
- Add an A record for example.com pointing at IP1.
- Add a second A record for the same name example.com pointing at IP2.
- Set the TTL to 60 seconds if the form allows it.
- Wait one minute.
- Run
dig example.com +short. - You should see both IP addresses in the answer, or a resolver that hands you one of them and the other on a later query.
- Open example.com in the browser.
- You should see either server A or server B. Refresh. You might see the other one. You might keep seeing the same one. The resolver is allowed to stick.
What goes wrong when one server dies
- In the EC2 console, select instance A.
- Open Instance state.
- Choose Stop instance.
- Wait until the state is Stopped.
- Open
http://IP1/again. - You should see a timeout or a connection error. The browser does not switch itself to IP2.
- Open
http://IP2/. - You should still see server B. Only people who already had IP2 are fine.
- If you added the two A records, run
dig example.com +shortagain. - You should still see IP1 in the answer until the TTL expires and you delete that record.
- Start instance A again.
- Wait until the state is Running.
- You should see that DNS did not notice the stop, and it will not notice the start either.
Ravindra Bagale's Tip
Many students say "I added two A records, so that is a load balancer." Look: if one server stops, the browser still goes to that IP. There is no health check. Pay attention!
Ravindra Bagale's Tip – मराठी
बरेच students म्हणतात "दोन A records ठेवले, म्हणजे load balancer झाला." बघा — एक server बंद पडला तर browser त्याच IP वर जातो. Health check नाही. ध्यान राखो!
Ravindra Bagale's Tip – हिंदी
बहुत students कहते हैं "दो A records रख दिए, मतलब load balancer बन गया." देखो — एक server बंद पड़ा तो browser उसी IP पर जाता है. Health check नहीं है. ध्यान रखो!