Fault-tolerant website with load balancing by Yandex Load Balancer

This scenario describes how to set up a website on a LAMP (Linux, Apache, MySQL, PHP) or LEMP (Linux, nginx, MySQL, PHP) stack with load balancing from Yandex Load Balancer between two availability zones and fault tolerance in one zone.

To set up a fault-tolerant load-balanced website:

  1. Before you start.
  2. Create VMs with a pre-installed web server.
  3. Upload the website files.
  4. Create a target group.
  5. Create a network load balancer.
  6. Test the fault tolerance.

If you no longer need the website, delete all its resources.

Before you start

Before deploying the server, you need to sign up for Yandex.Cloud and create a billing account:

  1. Go to the management console. Then log in to Yandex.Cloud or sign up if don't already have an account.
  2. On the billing page, make sure you linked a billing account, and it has the ACTIVE or TRIAL_ACTIVE status. If you don't have a billing account, create one.

If you have an active billing account, you can create or select a folder to run your VM in from the Yandex.Cloud page.

Learn more about clouds and folders.

Make sure that your folder contains a network with subnets in the availability zones ru-cental1-a and ru-central1-b. To do this, select Virtual Private Cloud on the folder page. If the list contains a network, click on its name to see the list of subnets. If you don't have the right network or subnets, create them.

The cost of hosting a website includes:

Create the virtual machines

The VMs must be created from identical images and their parameters must also be identical.

Create the first VM with a pre-installed web server

Create a virtual machine:

  1. On the folder page, click Create resource and select Virtual machine.

  2. In the Name field, enter a name for the VM: lb-tutorial-web-ru-central1-a.

  3. Select the availability zone: ru-central1-a.

  4. Choose one public image for both VMs:

    • LEMP for Linux, nginx, MySQL, and PHP
    • LAMP for Linux, Apache, MySQL, and PHP
  5. Under Computing resources:

    • Platform: Intel Cascade Lake.
    • vCPU: 2.
    • Guaranteed vCPU share: 5%.
    • RAM: 1 GB.
  6. In the Network settings section, select the subnet to connect the VM to when creating it.

  7. In the Public address field, select Auto.

  8. Specify data required for accessing the VM:

    • Enter the username in the Login field.
    • Under SSH key, paste the contents of the public key file. You need to create a key pair for SSH connection yourself. To create keys, use third-party tools, such as ssh-keygen (on Linux or macOS) or PuTTYgen (on Windows).
  9. Click Create VM.

Create a second VM with a pre-installed web server

Create a second virtual machine:

  1. On the folder page, click Create resource and select Virtual machine.

  2. In the Name field, enter a name for the VM: lb-tutorial-web-ru-central1-b.

  3. Select the availability zone: ru-central1-b.

  4. Choose one public image for both VMs:

    • LEMP for Linux, nginx, MySQL, and PHP
    • LAMP for Linux, Apache, MySQL, and PHP
  5. Under Computing resources:

    • Platform: Intel Cascade Lake.
    • vCPU: 2.
    • Guaranteed vCPU share: 5%.
    • RAM: 1 GB.
  6. In the Network settings section, select the subnet to connect the VM to when creating it.

  7. In the Public address field, select Auto.

  8. Specify data required for accessing the VM:

    • Enter the username in the Login field.
    • Under SSH key, paste the contents of the public key file. You need to create a key pair for SSH connection yourself. To create keys, use third-party tools, such as ssh-keygen (on Linux or macOS) or PuTTYgen (on Windows).
  9. Click Create VM.

Creating the VM may take several minutes. When the VM status changes to RUNNING, you can upload the website files to it.

Public IP addresses are assigned to virtual machines when they're created. They can be used for SSH access.

Upload the website files

As an example, you can create an index.html test file with any text.

For the lb-tutorial-web-ru-central1-a and lb-tutorial-web-ru-central1-b VMs, do the following:

  1. Go to Compute Cloud in the management console and find the public IP address of the VM.

  2. Connect to the VM over SSH.

  3. Grant your user write access to the directory /var/www/html:

    $ sudo chown -R "$USER":www-data /var/www/html
    
  4. Upload the website files to the VM over SCP.

    $ scp -r <path to the file directory> <VM user name>@<VM IP address>:/var/www/html
    

    Use WinSCP to copy the local file directory to /var/www/html on the VM.

Create a target group

  1. Open the Load Balancer section in the folder where the VMs were created.
  2. Open the Target group tab.
  3. Click Create target group.
  4. Enter a name for the target group, such as lb-tg-tutorial-web.
  5. Select the virtual machines lb-tutorial-web-ru-central1-a and lb-tutorial-web-ru-central1-b to add them to the target group.
  6. Click Create target group.

Create a network load balancer

When creating a network load balancer, you need to create a listener that the load balancer will use to receive traffic. You also need to set up health checks for resources in the attached target group.

To create a network load balancer:

  1. Open the Load balancers tab.
  2. Click Create load balancer.
  3. Enter a name for the load balancer, such as lb-tutorial-web.
  4. Click Add listener under Listeners.
  5. In the window that opens, enter a name for the listener, like lb-tut-listener-1.
  6. Set the port to 80.
  7. Click Add.
  8. Turn on Target groups.
  9. Select the previously created target group lb-tg-tutorial-web. If there's only one target group, it's selected automatically.
  10. Under Health check, enter a name for the health check, like health-check-1.
  11. Select the check type: HTTP.
  12. Set the port to 80.
  13. Specify the URL for health checks. You can leave the default path: /.
  14. Specify the response timeout in seconds: 1.
  15. Specify the interval, in seconds, for sending health check requests: 2.
  16. Set the healthy threshold, which is the number of successful checks required to consider the VM ready to receive traffic: 5.
  17. Specify the unhealthy threshold, which is the number of failed checks after which no traffic will be routed the VM: 5.
  18. Click Create load balancer.

Test the fault tolerance

  1. Under Network on the VM page in the management console, find the public IP address of the lb-tutorial-web-ru-central1-a VM.

  2. Connect to the VM over SSH.

  3. Stop the web service to simulate a failure on the web server:

    $ sudo service apache2 stop
    
    $ sudo service nginx stop
    
  4. In the management console, go to Load Balancer and select the load balancer created earlier.

  5. Find the listener IP address under Listeners. Open the website in the browser using the listener address. The connection should be successful, even though one of the web servers has failed.

  6. When the check is complete, start the web service again:

    $ sudo service apache2 start
    
    $ sudo service nginx start
    

How to delete created resources

To stop paying for deployed servers, delete the virtual machines dns-lb-tutorial-web-ru-central1-a and dns-lb-tutorial-web-ru-central1-b, and the load balancer lb-tutorial-web.