Fault-tolerant website with load balancing via Yandex Application Load Balancer using the management console
To create an infrastructure for your website with load balancing in three availability zones with an ALB load balancer using the Yandex Cloud management console:
- Get your cloud ready.
- Create a cloud network.
- Create security groups.
- Create an instance group.
- Upload the website files.
- Create a backend group.
- Create an HTTP router.
- Create an L7 load balancer.
- Configure DNS.
- Run a fault tolerance test.
If you no longer need the resources you created, delete them.
Get your cloud ready
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 infrastructure support cost includes:
- Fee for continuously running VMs (see Yandex Compute Cloud pricing ).
- Fee for a dynamic public IP address (see Yandex Virtual Private Cloud pricing).
- Fee for load balancing (see Application Load Balancer pricing).
- Fee for public DNS queries and DNS zones if using Yandex Cloud DNS (see Cloud DNS pricing).
Create a cloud network
All the resources created in the use case will belong to the same cloud network.
To create a network:
- In the management console
, select Virtual Private Cloud. - Click Create network.
- Specify the Name of the network:
mysite-network
. - In the Advanced field, select Create subnets.
- Click Create network.
Create security groups
Security groups include rules that allow the load balancer to receive incoming traffic and redirect it to the VMs so they can receive the traffic.
To create security groups for the load balancer and an instance group:
-
In the management console
, select Virtual Private Cloud. -
In the left-hand panel, select
Security groups. -
Create a security group for the load balancer:
-
Click Create security group.
-
Name the group, e.g.,
alb-sg
. -
Select the network to assign the security group to.
-
Under Rules, create the following rules using the instructions below the table:
Traffic
directionDescription Port
rangeProtocol Source /
target typeSource /
targetOutgoing
any
All
Any
CIDR
0.0.0.0/0
Incoming
ext-http
80
TCP
CIDR
0.0.0.0/0
Incoming
ext-https
443
TCP
CIDR
0.0.0.0/0
Incoming
healthchecks
30080
TCP
Load balancer healthchecks
— - Select the Egress or Ingress tab.
- Click Add.
- In the Port range field of the window that opens, specify a single port or a port range for traffic to come to or from.
- In the Protocol field, specify the appropriate protocol or leave Any to allow traffic transmission over any protocol.
- In the Destination name or Source field, select the purpose of the rule:
CIDR
: Rule will apply to the range of IP addresses. In the CIDR blocks field, specify the CIDR and subnet masks that traffic will come to or from. To add multiple CIDRs, click Add.Security group
: Rule will apply to the VMs from the current group or the selected security group.Load balancer healthchecks
: Rule allowing a load balancer to health-check VMs.
- Click Save. Repeat the steps to create all rules from the table.
-
Click Save.
-
-
Similarly, create a security group named
alb-vm-sg
for an instance group, with the same network and the following rules:Traffic
directionDescription Port
rangeProtocol Origin type Source Incoming
balancer
80
TCP
Security group
alb-sg
Incoming
ssh
22
TCP
CIDR
0.0.0.0/0
Creating an instance group
Your application backends will be deployed on the VM instance of the target group. The target group will be connected to the load balancer to enable requests to the application backend endpoints.
To create an instance group with the minimum configuration:
- In the management console
, select Compute Cloud. - Open the Instance groups tab and click Create group of virtual machines.
- Under Basic parameters:
- Name the instance group, e.g.,
website-vm-group
. - Select the service account from the list or create a new one. To be able to create, update, and delete VMs in the group, assign the
editor
role to the service account. By default, all operations with security groups are performed on behalf of the service account.
- Name the instance group, e.g.,
- Under Allocation, select three availability zones (
ru-central1-a
,ru-central1-b
, andru-central1-d
) to ensure fault tolerance of your hosting. - Under Instance template, click Define and specify your VM template:
-
Under Boot disk image, navigate to the Marketplace tab, select LEMP and click Use.
-
Under Disks and file storages, specify:
- Disk Type:
HDD
- Size:
3 GB
- Disk Type:
-
Under Computing resources, specify:
- Platform:
Intel Cascade Lake
- Guaranteed vCPU performance:
5%
- vCPU:
2
- RAM:
1 GB
- Platform:
-
Under Network settings:
- Select a cloud network and its subnets.
- In the Public IP address field, select Auto.
- Select the
alb-vm-sg
security group.
-
Under Access, specify the data for accessing the instance:
- In the Service account field, select the service account to link to the VM.
- Under Login, enter a username.
- In the SSH key field, paste the contents of the public key file.
To establish an SSH connection, you need to create a key pair. For more information, see Connecting to a Linux VM via SSH.
-
Click Save.
-
- Under Scaling, specify the instance group Size:
3
. - Under Integration with Application Load Balancer, select Create target group and enter
alb-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:
-
On the Virtual machines tab, click the VM name in the list.
-
Copy Public IPv4 address from the Network section.
-
Connect to the VM over 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 backend group
You must link the target group created with the VM group to the backend group that defines traffic allocation settings.
For the backends, the groups will implement health checks: the load balancer will periodically send health check requests to the VMs and expect a response for a certain period.
To create a backend group:
- Select Application Load Balancer in the folder where the instance group was created.
- Open the Backend groups tab.
- Click Create backend group.
- Name the backend group, e.g.,
website-bg
. - Under Backends, click Add.
- Name the backend, e.g.,
backend-1
. - In the *Target groups field, select the
alb-tg
target group you created earlier. - Specify Port the backend VMs will use to receive incoming traffic from the load balancer:
80
. - Click Add health check.
- Specify Port the backend VMs will use to accept health check connections:
80
. - Specify Path the load balancer will use for health checks:
/
. - Click Create.
Create an HTTP router
The backend group should be linked to an HTTP router that defines HTTP routing rules.
To create an HTTP router and add a route to it:
- Open the HTTP routers tab.
- Click Create HTTP router.
- Name the router, e.g.,
website-router
. - Click Add virtual host.
- Name the virtual host, e.g.,
alb-host
. - In the Authority field, enter the website's domain name:
alb-example.com
. - Click Add route.
- Enter a name, e.g.,
route-1
. - In the Backend group field, select
website-bg
, which you created earlier. - Leave all other settings unchanged and click Create.
Create an L7 load balancer
To create a load balancer:
- Open the Load balancers tab.
- Click Create L7 load balancer.
- Name the load balancer, e.g.,
website-alb
. - Under Network settings, select the network your VM group is connected to and the previously created security group,
alb-sg
. - Under Allocation, select the subnets for the load balancer's nodes in each availability zone and enable inbound traffic.
- Under Listeners, click Add listener.
- Name the listener, e.g.,
alb-listener
. - Under Public IP address, enable outbound traffic.
- Set
80
as the port. - In the HTTP router field, select the previously created
website-router
. - Click Create.
Configure DNS
You need to link the alb-example.com
domain name you want to use for your website to the load balancer's IP address using DNS records.
-
In the management console
, select Application Load Balancer. -
Copy the IP address of the load balancer that you created.
-
On the site of your DNS hosting provider, go to the DNS settings.
-
Create or edit the A record and CNAME record for
alb-example.com
:alb-example.com. A <L7_load_balancer_IP_address> alb-example.com. CNAME
If you use Yandex Cloud DNS, follow this guide to configure the records:
Configuring DNS records for Cloud DNS
To get access to public zone domain names, you need to delegate the domain. Specify the addresses of the
ns1.yandexcloud.net
andns2.yandexcloud.net
servers in your account on your registrar's website.- In the management console
, select Cloud DNS. - If you do not have a public DNS zone, create one:
- Click Create zone.
- Specify the zone Name:
alb-zone
. - In the Zone field, enter the website's domain name with a trailing dot:
alb-example.com.
. - Select a Type of the zone:
Public
. - Click Create.
- Create an A record in the zone:
- Click Create record.
- Name: Leave empty.
- Type: Leave set to
A
. - TTL (record time to live): Keep the default value.
- Data: Enter the public IP address of the
website-alb
load balancer. - Click Create.
- Create a CNAME record:
- Click Create record.
- Name:
www
. - Type: Select
CNAME
. - TTL (record time to live): Keep the default value.
- Data: Enter
alb-example.com
. - Click Create.
- In the management console
Run a fault tolerance test
-
In the management console
, select Compute Cloud. -
Go to the page of the VM from the previously created group.
-
Copy Public IPv4 address from the Network section.
-
Connect to the VM over SSH.
-
Stop the web service to simulate a failure on the web server:
sudo service nginx stop
-
Open your website in a browser. The website should open, even though one of the web servers has failed.
-
After the check is complete, restart the web service:
sudo service nginx start
How to delete the resources you created
To stop paying for the resources you created:
- Delete the non-billable resources that block the deletion of billable resources:
- Delete the
website-vm-group
instance group. - If you used Yandex Cloud DNS, delete the DNS records and delete the DNS zone.