Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
© 2022 Yandex.Cloud LLC
Practical guidelines
  • Web service
    • All tutorials
    • Static website in Object Storage
    • Website on LAMP or LEMP stack
    • Fault-tolerant website with load balancing by Network Load Balancer
    • Fault-tolerant website using DNS load balancing
    • Joomla website with PostgreSQL
    • WordPress website
    • WordPress website on a MySQL database
    • Transferring a WordPress website from a different hosting provider to Yandex Cloud
    • 1C-Bitrix website
    • Integrating an L7 load balancer with the Cloud CDN and Object Storage
    • Blue-green and canary deployment of service versions
  • Online stores
    • All tutorials
    • 1C-Bitrix online store
    • Opencart online store
  • Data archive
    • All tutorials
    • Single-node file server
    • Configuring an SFTP server on Centos 7
    • Backup to Object Storage via Acronis Backup
    • Backup to Object Storage via CloudBerry Desktop Backup
    • Backup to Object Storage via Duplicati
    • Backup to Object Storage via Bacula
    • Backup to Object Storage via Veritas Backup Exec
    • Digitizing archives in Yandex Vision
  • Test environment
    • All tutorials
    • Testing applications with GitLab
    • Creating test VMs using GitLab CI
    • High-performance computing on preemptible VMs
    • Emulating multiple IoT devices
    • gRPC service load testing
    • Using Phantom to run a fixed-load HTTPS test
    • Using Pandora to run a step-load HTTPS test
  • Performing infrastructure management
    • All tutorials
    • Getting started with Terraform
    • Uploading Terraform states to Object Storage
    • Getting started with Packer
    • Automating VM image builds using Jenkins
    • Continuous deployment of containerized applications using GitLab
    • Creating a cluster of 1C:Enterprise Linux servers with a Managed Service for PostgreSQL cluster
    • Creating a cluster of 1C:Enterprise Windows servers with SQL Server
    • Migrating to Yandex Cloud using Hystax Acura
    • Emergency recovery in Yandex Cloud using Hystax Acura
    • Configuring a fault-tolerant architecture in Yandex Cloud
  • Building a data platform
    • All tutorials
    • Syncing MySQL data using Yandex DataTransfer
    • Using schema registries with Managed Service for Apache Kafka®
      • Overview
      • Using Managed Schema Registry with Yandex Managed Service for Apache Kafka®
      • Using Confluent Schema Registry with Yandex Managed Service for Apache Kafka®
    • Delivering data using Debezium
    • Migrating databases from Yandex Managed Service for MySQL to MySQL
    • Migrating databases to Managed Service for Microsoft SQL Server
    • Transferring data from PostgreSQL to ClickHouse using Yandex Data Transfer
    • Configuring Yandex Cloud DNS for accessing managed database clusters from other cloud networks
    • Configuring Kafka Connect for Yandex Managed Service for Apache Kafka® clusters
  • Windows in Yandex Cloud
    • All tutorials
    • Deploying Active Directory
    • Deploying Microsoft Exchange
    • Deploying Remote Desktop Services
    • Deploying an Always On availability group
    • Deploying an Always On availability group with an internal network load balancer
    • Deploying Remote Desktop Gateway
  • Network routing
    • All tutorials
    • Routing through a NAT instance
    • Creating a VPN tunnel
    • Installing a Cisco CSR1000v virtual router
    • Installing a Mikrotik CHR virtual router
    • Creating a VPN connection using OpenVPN
  • Data visualization and analytics
    • All tutorials
    • Visualizing data from a CSV file
    • Creating and publishing a chart with a map of Moscow from a CSV file
    • Analyzing a store chain's sales based on data from a ClickHouse DB
    • Analyzing open data on road accidents in Russia
    • Analyzing sales and locations of pizzerias based on data from ClickHouse DB and Marketplace
    • Web analytics with a connection to Yandex.Metrica
    • Web analytics with funnels and cohorts calculated based on Yandex.Metrica data
    • Mobile app analytics based on AppMetrica data
    • Analyzing Yandex Music podcast statistics (for podcasters)
    • Visualizing data with a SQL chart
    • Mobile app customer journey analytics based on AppMetrica data
    • Analyzing Object Storage logs in DataLens
  • Internet of things
    • Tutorials for the internet of things
    • Status monitoring of geographically distributed devices
    • Monitoring sensor readings and event notifications
  • Serverless technologies
    • URL shortener
    • Entering data into storage systems
    • Storing application runtime logs
  1. Web service
  2. Fault-tolerant website with load balancing by Network Load Balancer

Fault-tolerant website with load balancing by Yandex Network Load Balancer

Written by
Yandex Cloud
  • Before you start
    • Required paid resources
  • Create the virtual machines
    • Create the first VM with a pre-installed web server
    • Create a second VM with a pre-installed web server
  • Upload the website files
  • Create a target group
  • Create a network load balancer
  • Test the fault tolerance
  • How to delete created resources

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 Network 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 working, you need to register in 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.

Required paid resources

The cost of hosting a website includes:

  • A fee for the disks and continuously running VMs (see Yandex Compute Cloudpricing).
  • A fee for using dynamic public IP addresses (see Yandex Virtual Private Cloudpricing).
  • A fee for network load balancers and traffic balancing (see pricingYandex Network Load Balancer).

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. Under Image/boot disk selection, click the Cloud Marketplace tab, and select the 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 Ice Lake.
    • vCPU: 2.
    • Guaranteed vCPU share: 20%.
    • 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 Ice Lake.
    • vCPU: 2.
    • Guaranteed vCPU share: 20%.
    • 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.

    Linux/macOS
    Windows
    $ scp -r <path to the file directory> <VM username>@<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:

    LAMP
    LEMP
    $ sudo service apache2 stop
    
    $ sudo service nginx stop
    
  4. In the management console, go to Network 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:

    LAMP
    LEMP
    $ 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.

Was the article helpful?

Language / Region
© 2022 Yandex.Cloud LLC
In this article:
  • Before you start
  • Required paid resources
  • Create the virtual machines
  • Create the first VM with a pre-installed web server
  • Create a second VM with a pre-installed web server
  • Upload the website files
  • Create a target group
  • Create a network load balancer
  • Test the fault tolerance
  • How to delete created resources