Adding a listener to a network load balancer
To add a listener to a network load balancer:
-
In the management console
, select the folder where you need to add a listener to a load balancer. -
In the list of services, select Network Load Balancer.
-
In the line of the load balancer to add a listener to, click
and select Add listener. -
In the window that opens, set the listener parameters:
-
Name.
-
Protocol:
TCP
orUDP
.Note
By default, the listener uses TCP. To use UDP, submit a request to technical support
. -
Port where the listener will listen for incoming traffic. The possible values are from
1
to32,767
. -
Target port the load balancer will redirect traffic to. The possible values are from
1
to32,767
. -
Click Add.
-
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
To add a listener to a network load balancer, run this command:
yc load-balancer network-load-balancer add-listener <load_balancer_name_or_ID> \
--listener name=<listener_name>,`
`port=<port>,`
`target-port=<target_port>,`
`protocol=<protocol>,`
`external-address=<external_IP_address>,`
`external-ip-version=<IP_version>
Where:
listener
: Listener parameters:name
: Name of the listener.port
: Port where the load balancer will accept incoming traffic. The acceptable values are from1
to32767
.target-port
: Port to which the load balancer will redirect traffic. The acceptable values are from1
to32767
.protocol
: Protocol the listener will use (TCP
orUDP
).external-address
: External IP address of the listener.external-ip-version
: Version of the external IP address (ipv4
oripv6
).
You can get the load balancer ID and name with a list of network load balancers in the folder.
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the documentation on the Terraform
If you don't have Terraform, install it and configure the Yandex Cloud provider.
-
Open the Terraform configuration file and add the
listener
section to the network load balancer description.resource "yandex_lb_network_load_balancer" "foo" { name = "<load_balancer_name>" ... listener { name = "<listener_name>" port = <port_number> external_address_spec { ip_version = "<IP_version>" } } ... }
Where:
name
: Name of the network load balancer.listener
: Listener parameters:name
: Listener name.port
: Port in the range of1
to32767
that the network load balancer will receive incoming traffic on.external_address_spec
: Specification of the listener for the external load balancer:ip_version
: External IP address specification. Set the IP address version:ipv4
oripv6
. The default value isipv4
.
For more information about the
yandex_lb_network_load_balancer
resource in Terraform, see the provider documentation . -
Make sure the settings are correct.
-
Using the command line, navigate to the folder that contains the up-to-date Terraform configuration files with an infrastructure plan.
-
Run the command:
terraform validate
If there are errors in the configuration files, Terraform will point to them.
-
-
Add a listener.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
-
To add a listener to the network load balancer, use the addListener REST API method for the NetworkLoadBalancer resource or the NetworkLoadBalancerService/AddListener gRPC API call.
Examples
Adding a listener to a network load balancer
Add a listener with the following test specifications to the test-load-balancer
:
- Name:
test-listener
- Port:
80
- Target port:
81
- Protocol:
TCP
- IP version:
ipv4
Run the following command:
yc load-balancer network-load-balancer add-listener test-load-balancer \
--listener name=test-listener,`
`port=80,`
`target-port=81,`
`protocol=tcp,`
`external-ip-version=ipv4
-
Open the Terraform configuration file and add the
listener
section to the network load balancer description.resource "yandex_lb_network_load_balancer" "foo" { name = "test-load-balancer" listener { name = "test-listener" port = 80 target_port = 81 protocol = "tcp" external_address_spec { ip_version = "ipv4" } } }
For more information about resources you can create with Terraform, see the provider documentation
. -
Make sure the settings are correct.
-
Using the command line, navigate to the folder that contains the up-to-date Terraform configuration files with an infrastructure plan.
-
Run the command:
terraform validate
If there are errors in the configuration files, Terraform will point to them.
-
-
Create a network load balancer.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
-
Use the addListener API method and include the following information in the request body:
{
"listenerSpec": {
"name": "test-listener",
"port": "80",
"protocol": "TCP",
"targetPort": "81",
"externalAddressSpec": {
"ipVersion": "ipv4"
}
}
}