Fault-tolerant website with load balancing via Yandex Network Load Balancer using the management console
To create a fault-tolerant site with load balancing via Yandex Network Load Balancer using the Yandex Cloud management console:
- Prepare your cloud.
- Create an instance group.
- Upload the website files.
- Create a network load balancer.
- Run a fault tolerance test.
If you no longer need the resources you created, delete them.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - 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.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost of hosting a website 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).
Prepare the 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
nlb-network
cloud network and its subnets. - In the Public address field, select
Auto
.
- Select the
-
Under Access, specify the data for access to the VM:
- In the Service account field, select the service account to link to the VM.
- Enter 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 that the load balancer will use to receive traffic, attach the target group created together with the instance group, and configure resource health checks in it.
-
In the management console
, select the folder to create a load balancer in. -
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 parameters:
- 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 created earlier. If there is only one target group, it is selected automatically. -
Under Health check, click Configure and specify the following:
- Name:
health-check-1
- Healthy threshold: Number of successful checks to consider the VM ready to receive traffic:
5
- Unhealthy threshold: Number of failed checks to stop routing traffic to the VM:
5
- Name:
-
Click Apply.
-
-
Click Create.
After you create a network load balancer, run a fault tolerance test.
Run a fault tolerance test
-
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 created resources:
- Delete the network load balancer.
- Delete the instance group.
- Delete the static public IP if you reserved one.