Fault-tolerant website with load balancing via Yandex Network Load Balancer using the management console
To create a fault-tolerant website with load balancing via Yandex Network Load Balancer using the Yandex Cloud management console:
- Get your cloud ready.
- Create an instance group.
- Upload the website files.
- Create a network load balancer.
- Test the fault tolerance.
If you no longer need the resources you created, delete them.
Get your cloud ready
Sign up in Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or register a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders.
Required paid resources
The website support cost includes:
- Fee for disks and continuously running VMs (see Yandex Compute Cloud pricing).
- Fee for using dynamic or static public IP addresses (see Yandex Virtual Private Cloud pricing).
- Fee for a network load balancer and traffic balancing (see Network Load Balancer pricing).
Set up your environment
- Create a cloud network named
nlb-network
and subnets. - Create a service account named
nlb-sa
and assign theeditor
role to it. - Create an SSH key pair.
Create an instance group
-
In the management console
, select Compute Cloud. -
In the left-hand panel, select
Instance groups and click Create group of virtual machines. -
Under Basic parameters:
- Enter a name for the instance group, e.g.,
nlb-vm-group
. - Select the
nlb-sa
service account.
- Enter a name for the instance group, e.g.,
-
Under Allocation, select the
ru-central1-a
andru-central1-b
availability zones to ensure fault tolerance of your hosting. -
Under Instance template, click Define to configure a basic instance:
-
Under Boot disk image, open the Marketplace tab and click Show all Marketplace products. Choose a product:
Click Use.
-
Under Disks and file storages, specify:
- Type:
HDD
- Size:
3 GB
- Type:
-
Under Computing resources, open the Custom tab and specify the following:
- Platform:
Intel Ice Lake
- vCPU:
2
- Guaranteed vCPU performance:
20%
- RAM:
1 GB
- Platform:
-
Under Network settings:
- Select the cloud network named
nlb-network
and its subnets. - In the Public address field, select
Auto
.
- Select the cloud network named
-
Under Access, specify the VM access credentials:
- In the Service account field, select the service account to link to the VM.
- Specify the username in the Login field.
- In the SSH key field, paste the contents of the public key file.
-
Click Save.
-
-
Under Scaling, specify the instance group size:
2
. -
Under Integration with Network Load Balancer, select Create target group and enter
nlb-tg
as the group name. -
Click Create.
It may take a few minutes to create an instance group. As soon as all VMs change their status to RUNNING
, you can upload the website files to them.
Upload the website files
To test the web server, upload the website files to each VM. You can use the index.html
file from this archive
For each VM in the created group, do the following:
-
Get the VM public IP address.
-
Connect to the VM via SSH.
-
Grant your user write permissions for the
/var/www/html
directory:sudo chown -R "$USER":www-data /var/www/html
-
Upload the website files to the VM via SCP
.Linux/macOSWindowsUse the
scp
command line utility:scp -r <path_to_directory_with_files> <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 network load balancer
When creating a network load balancer, you need to add a listener to handle incoming traffic, attach the target group created along with the instance group, and configure health checks for its targets.
-
In the management console
, select the folder where you need to create a load balancer. -
In the list of services, select Network Load Balancer and click Create a network load balancer.
-
Enter a name for the load balancer, e.g.,
nlb-1
. -
Under Listeners:
-
Click Add listener and specify the following settings:
- Name:
nlb-listener
- Port:
80
- Target port:
80
- Name:
-
Click Add.
-
-
Under Target groups:
-
Click Add target group and select the
nlb-tg
target group you created earlier. If there is only one target group, it will be selected automatically. -
Under Health check, click Configure and specify the following:
- Name:
health-check-1
. - Healthy threshold:
5
. This stands for the number of successful checks required to consider the VM ready to receive traffic. - Unhealthy threshold:
5
. This stands for the number of failed checks before traffic is no longer routed to the VM.
- Name:
-
Click Apply.
-
-
Click Create.
After you create a network load balancer, test the fault tolerance.
Test the fault tolerance
-
Get the public IP address of any VM from the group you created.
-
Connect to the VM via SSH.
-
Stop the web service to simulate a failure on the web server:
LAMPLEMPsudo service apache2 stop
sudo service nginx stop
-
Get the listener IP address.
-
Open the website in the browser using the listener address.
The connection should be successful, even though one of the web servers has failed.
-
When the check is complete, start the web service again:
LAMPLEMPsudo service apache2 start
sudo service nginx start
How to delete the resources you created
To shut down the hosting and stop paying for the resources you created:
- Delete the network load balancer.
- Delete the instance group.
- Delete the static public IP address if you reserved one.