Getting started with Yandex Application Load Balancer
This guide will help you create your first L7 load balancer, connect a target group to it, and set up traffic to the test application back end.
Getting started
- Log in or sign up to the management console
. If you are not signed up yet, navigate to the management console and follow the instructions. - On the Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not yet have a billing account, create one. - If you do not have a folder yet, create one. While creating a folder, you can also create a default virtual network with subnets in all availability zones.
Create a VM and launch a test web server on it
-
Create a VM named
test-vm1
in theru-central1-a
availability zone. -
Connect to the VM and start a test web server on it to respond to requests at port
80
:sudo python3 -m http.server 80
-
Make sure the web server returns a list of folders in the directory. Open the terminal on your computer and run this command:
curl -v <public_IP_address_of_test_VM>
Create a target group
Your application back ends will be deployed on the VM instance of the target group. The target group will be connected to the load balancer so that requests might be sent to the backend endpoints of your application.
In this example, we will assume there is only one VM in the target group.
- In the management console
, select the folder to create your target group in. - In the list of services, select Application Load Balancer.
- In the left-hand menu, select Target groups.
- Click Create target group.
- Give your target group a name, e.g.,
test-target-group
. - Select the
test-vm1
VM. - Click Create.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
Run this command:
yc alb target-group create test-target-group \
--target subnet-name=<VM_subnet_name>,ip-address=<VM_internal_IP_address>
Create a backend group
Backend groups contain settings for traffic balancing and target resource health check. Create a group and add one backend to it.
- In the left-hand menu, select Backend groups.
- Click Create backend group.
- Enter the backend group name:
test-backend-group
. - Under Backends, click Add. Specify the backend settings:
- Enter the backend name:
backend-1
. - In the Target group list, select
test-target-group
. - Specify the Port:
80
.
- Enter the backend name:
- Expand the Protocol settings field and set the parameters:
- Select the
HTTP
type.
- Select the
- Click Add health check and set up the check:
- Timeout, s:
1
- Interval:
3
- Healthy threshold:
2
- Unhealthy threshold:
2
- Type:
HTTP
- Path:
/
- Timeout, s:
- Click Create.
-
Create a backend group:
yc alb backend-group create test-backend-group
-
Create a backend and health check:
yc alb backend-group add-http-backend \ --backend-group-name test-backend-group \ --name backend-1 \ --port 80 \ --target-group-name test-target-group \ --target-group-id <target_group_ID> \ --http-healthcheck healthy-threshold=2,unhealthy-threshold=2,timeout=1s,interval=3s,path=/
Create an HTTP router
HTTP routers define the rules for routing requests sent to backends and allow you to modify requests directly in the balancer.
- In the left-hand menu, select HTTP routers.
- Click Create HTTP router.
- Enter the router name:
test-http-router
. - Under Virtual hosts, click Add virtual host.
- Enter the host name:
test-virtual-host
. - Click Add route.
- Enter the Name:
test-route
. - In the Path field, select
Starts with
and specify the path/
. - In the Action field, keep
Routing
. - In the Backend group list, select
test-backend-group
. - Leave all other settings unchanged and click Create.
-
Create an HTTP router:
yc alb http-router create test-http-router
-
Create a virtual host:
yc alb virtual-host create test-virtual-host --http-router-name test-http-router
-
Add a route:
yc alb virtual-host append-http-route test-route \ --http-router-name test-http-router \ --prefix-path-match / \ --backend-group-name test-backend-group \ --virtual-host-name test-virtual-host
Create an L7 load balancer
A load balancer receives requests and distributes them across target group VMs according to the rules specified in the HTTP router. Load balancers use listeners to receive traffic.
As an example, let's create a balancer with a node in the same subnet and same availability zone.
-
In the left-hand menu, select Load balancers.
-
Click Create L7 load balancer.
-
Enter the load balancer name:
test-load-balancer
. -
Under Network settings, select the network whose subnets will host the load balancer nodes.
-
Under Allocation, select the subnets for the load balancer nodes in each availability zone and enable traffic.
Note
When creating an L7 load balancer using the management console, make sure there are subnets in all availability zones even if you are not going to accept traffic in them. If any availability zone is missing subnets, create them. To do this, click Create new.
-
Under Listeners, click Add listener. Set the listener settings:
- Enter the listener name:
test-listener
. - Under Public IP address, enable traffic.
- Set the port to
80
. - Select the Automatically type.
- Enter the listener name:
-
In the HTTP router field, select
test-http-router
. -
Click Create.
-
Create a load balancer with a node in the same subnet:
yc alb load-balancer create test-load-balancer \ --network-name <network_name> \ --location subnet-name=<ru-central1-a_subnet_name>,zone=ru-central1-a \ --location subnet-name=<ru-central1-b_subnet_name>,zone=ru-central1-b \ --location subnet-name=<ru-central1-d_subnet_name>,zone=ru-central1-d
-
Add a listener:
yc alb load-balancer add-listener test-load-balancer \ --listener-name test-listener \ --http-router-id <HTTP_router_ID> \ --external-ipv4-endpoint port=80
Test the load balancer
In the terminal, run the following command:
curl -v <public_IP_address_of_load_balancer>:80
The response must include an HTTP response with the code 200
and a list of folders from the root directory of the test VM in HTML markup.
After that, you can add other VMs to the target group, create new backends for your application, and build routes to the application endpoints.