When you have multiple virtual machines to handle client requests, you start thinking about how to distribute the load between them.
A load balancer is a tool for sorting incoming requests among your virtual machines. The balancer regularly checks the availability (health) of servers, so in case of a problem with the server, traffic is not sent to it.
Creating and configuring a balancer
To create a load balancer, go to your project - > Networking - > Load Balancers - > Create Load Balancer.
To create a balancer, you must add one or more listeners.
A listener is a process that checks for connection requests, using the protocol and port that you configure.
To set up a listener, click the "Add Listener" button.
In the window that opens, specify the name of the listener, the protocol (TCP or HTTP), the port in the range from 1 to 65535, and click the "Create Listener" button.
After you create a listener, you must configure the pool. A pool is a list of virtual machines to which the listener will redirect incoming traffic.
Click on "Add Pool" to start configuring:
Specify the pool name.
Select the balancing algorithm:
- Round robin — requests are distributed between servers within the cluster sequentially: the first request is sent to the first server, the second — to the second server, and so on in a circle.
- Least Connection — new requests are sent to the server with the fewest current connections to clients.
- Source IP — the IP address of the client is used to determine which server receives the request.
Currently, the balancer can only communicate with servers over the HTTP protocol.
If you need to route the requests for a particular session to the same machine that serviced the first request for that session, select "App Cookie" and fill in the "Cookie" field.
Add the virtual machines that will participate in the traffic distribution for the configured listener. When adding a machine, you must specify its port and weight in the distribution:
In the "Health Check" section, select the protocol: TCP, Ping, HTTP.
For the HTTP protocol, you must select the HTTP method and add the URL path.
Also for all protocols specify:
- "Check interval (sec)" — time between sent requests
- "Response time (sec)" — the time to wait for a response from the server
- "Unhealthy Threshold" — the number of failed requests after which traffic will no longer be sent to the virtual machine
- "Healthy Thresholds" — the number of successful requests after which the virtual machine will be considered ready to receive traffic.
Save the changes.
Add the name of the balancer and click "Create Load Balancer" to complete the setup.
A separate public IP is allocated for a load balancer; the IP address cost is included in the cost of the service itself.
Load Balancer statuses
|Status (UI)||Status (API)||Value|
The balancer is working.
All virtual machines in the pool accept requests
|Unhealthy||Draining||A virtual machine from the pool does not accept new requests|
|Degraded||One or more balancer components have an Error status|
The balancer doesn't work.
Virtual machines do not pass status check requests.
All virtual machines in the pool have the Error status