Ravindra BagaleCourses & study guides मराठी Track your progress

Chapter 18: Load Balancing

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.

  1. SSH to instance A.
  2. On Amazon Linux, run sudo yum install -y nginx.
  3. On Ubuntu, run sudo apt update.
  4. On Ubuntu, run sudo apt install -y nginx.
  5. Skip the lines that are not your operating system.
  6. Run echo 'This is server A, the shop' | sudo tee /usr/share/nginx/html/index.html.
  7. Run sudo service nginx start.
  8. Run sudo service nginx status.
  9. Run curl -s http://127.0.0.1.
  10. You should see This is server A, the shop, and the service reported as running.
  11. SSH to instance B.
  12. Install Nginx the same way, with sudo yum or sudo apt, matching the operating system.
  13. Run echo 'This is server B, the api' | sudo tee /usr/share/nginx/html/index.html.
  14. Run sudo mkdir -p /usr/share/nginx/html/api.
  15. Run echo 'api on server B' | sudo tee /usr/share/nginx/html/api/index.html.
  16. Run sudo service nginx start.
  17. Run curl -s http://127.0.0.1.
  18. Run curl -s http://127.0.0.1/api/.
  19. You should see This is server B, the api, and then api 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.

  1. In the EC2 console, allow inbound HTTP port 80 from your own IP on both instance security groups.
  2. Copy instance A's public IPv4. Call it IP1.
  3. Copy instance B's public IPv4. Call it IP2.
  4. Open http://IP1/ in the browser.
  5. You should see This is server A, the shop.
  6. Open http://IP2/ in the browser.
  7. You should see This is server B, the api.
  8. Open http://IP2/api/.
  9. 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.

  1. Open the DNS zone for example.com.
  2. Add an A record for example.com pointing at IP1.
  3. Add a second A record for the same name example.com pointing at IP2.
  4. Set the TTL to 60 seconds if the form allows it.
  5. Wait one minute.
  6. Run dig example.com +short.
  7. 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.
  8. Open example.com in the browser.
  9. 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

  1. In the EC2 console, select instance A.
  2. Open Instance state.
  3. Choose Stop instance.
  4. Wait until the state is Stopped.
  5. Open http://IP1/ again.
  6. You should see a timeout or a connection error. The browser does not switch itself to IP2.
  7. Open http://IP2/.
  8. You should still see server B. Only people who already had IP2 are fine.
  9. If you added the two A records, run dig example.com +short again.
  10. You should still see IP1 in the answer until the TTL expires and you delete that record.
  11. Start instance A again.
  12. Wait until the state is Running.
  13. 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!