Migrating services from an NLB with a Yandex Managed Service for Kubernetes cluster as a target to an L7 ALB using Terraform
To migrate a service from a network load balancer to an L7 load balancer:
- See the service migration recommendations.
- Create your infrastructure. At this step, you will associate the Smart Web Security profile with a virtual host of the L7 load balancer.
- Install an Application Load Balancer ingress controller and create resources in your Managed Service for Kubernetes cluster. At this step, you will associate your Smart Web Security profile with the L7 load balancer.
- Test the L7 load balancer.
- Migrate user traffic from the network load balancer to the L7 load balancer.
Service migration recommendations
-
In addition to DDoS protection at OSI L7 using Yandex Smart Web Security, we recommend enabling DDoS protection at L3-L4. To do this, reserve a public static IP address with DDoS protection in advance and use this address for the L7 load balancer's listener.
If the network load balancer's listener already uses a public IP address with DDoS protection, you can save it and use it for the L7 load balancer.
If the network load balancer's listener uses a public IP address without DDoS protection, DDoS protection at L3-L4 when migrating to an L7 load balancer can only be achieved by changing the public IP for your service.
When using L3-L4 DDoS protection, configure a trigger threshold for the L3-L4 protection mechanisms aligned with the amount of legitimate traffic to the protected resource. To set up this threshold, contact support
.Also, set the MTU value to
1450
for the targets downstream of the load balancer. For more information, see Setting up MTU when enabling DDoS protection. -
We recommend performing migration during the hours when the user load is at its lowest. If you plan to keep your public IP address, bear in mind that migration involves moving this IP address from the load balancer to the L7 load balancer. Your service will be unavailable during this period. Under normal conditions, this may last for several minutes.
-
When using an L7 load balancer, requests to backends come with the source IP address from the range of internal IP addresses of the subnets specified when creating the L7 load balancer. The original IP address of the request source (user) is specified in the
X-Forwarded-For
header. If you want to log public IP addresses of users on the web server, reconfigure it. -
For the L7 load balancer, two resource units will be created in each of the subnets specified when creating the
Ingress
resource. TheIngress
resource annotations do not support specifying the minimum number of resource units for an L7 load balancer. The system automatically scales the resource unit group based on the load balancer node’s external workload. -
The features of the Application Load Balancer load balancer may differ from those of your load balancer deployed in the Managed Service for Kubernetes cluster. See Application Load Balancer ingress controller description and operating principles.
-
We recommend setting up backend health checks on your Application Load Balancer. Health checks enable the load balancer to timely spot unavailable backends and reroute traffic to alternative backends. Once the application is updated, traffic will again be distributed across all backends.
For more information, see Tips for configuring Yandex Application Load Balancer health checks and Annotations (metadata.annotations).
Create your infrastructure
-
If you do not have Terraform yet, install it.
-
Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.
-
Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it
. -
Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.
-
Download the configuration file to the same working directory based on the protocol you are using:
HTTP
: alb-k8s-http.tf configuration fileHTTPS
: alb-k8s-https.tf configuration file
These files describe:
- Subnets for the L7 load balancer.
- Security group for the L7 load balancer.
- Static address for the L7 load balancer.
- Importing a TLS certificate to Certificate Manager (if using
HTTPS
). - Smart Web Security profile.
-
Specify the following variables in the configuration file:
domain_name
: Your service domain name.network_id
: ID of the network containing the VMs from the network load balancer's target group.certificate
(forHTTPS
): Path to the self-signed custom certificate.private_key
(forHTTPS
): Path to the private key file.
-
Make sure the Terraform configuration files are correct using this command:
terraform validate
Terraform will show any errors found in your configuration files.
-
Create the required infrastructure:
-
Run this command to view the planned changes:
terraform plan
If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply
-
Confirm updating the resources.
-
Wait for the operation to complete.
-
All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console
. -
Install an Application Load Balancer ingress controller and create resources in your Managed Service for Kubernetes cluster
-
Install the Yandex Application Load Balancer ingress controller.
-
Create an IngressClass resource for the L7 load balancer's Ingress controller:
-
Create a YAML file and describe the
IngressClass
resource in it.IngressClass
resource example:apiVersion: networking.k8s.io/v1 kind: IngressClass metadata: labels: app.kubernetes.io/component: controller name: ingress-alb spec: controller: ingress.alb.yc.io/yc-alb-ingress-controller
-
Use the following command to create the
IngressClass
resource:kubectl apply -f <IngressClass_resource_file>
-
-
Create an
Ingress
resource:-
Read the descriptions of the
Ingress
resource fields and annotations and see the example. -
Create a YAML file and describe the
Ingress
resource in it:-
Complete the annotations section for the L7 load balancer settings:
-
ingress.alb.yc.io/subnets
: IDs of the subnets in the three availability zones for the L7 load balancer nodes. Specify the IDs separated by commas with no spaces. -
ingress.alb.yc.io/security-groups
: ID of one or more security groups for the L7 load balancer. For multiple groups, specify their IDs separated by commas with no spaces. -
ingress.alb.yc.io/external-ipv4-address
: Previously reserved static public IP address. -
ingress.alb.yc.io/group-name
: Name of theIngress
resource group.Ingress
resources are grouped together, each group served by a separate Application Load Balancer instance with a dedicated public IP address. -
ingress.alb.yc.io/security-profile-id
: ID of the previously created Smart Web Security security profile.Warning
The security profile will be linked to the virtual host of the L7 load balancer. Specifying your security profile is the key step to connecting Smart Web Security.
-
ingress.alb.yc.io/autoscale-min-zone-size
: Minimum number of resource units per availability zone, based on expected load.We recommend selecting the number of resource units based on the load expressed in:
- Number of requests per second (RPS)
- Number of concurrent active connections
- Number of new connections per second
- Traffic processed per second
-
-
For the
ingressClassName
field, enter the name of theIngressClass
resource you created earlier. -
When using HTTPS, complete the tls section:
hosts
: Your service domain name the TLS certificate corresponds to.secretName
: TLS certificate of your service in Yandex Certificate Manager, inyc-certmgr-cert-id-<certificate_ID>
format.
-
Complete the rules section in line with your rules for distribution of incoming traffic among backends depending on the domain name (
host
field) and requested resource (http.paths
field).-
host
: Your service domain name. -
pathType
: Type of reference to the requested resource:Exact
: Request URI path must match thepath
field value.Prefix
: Request URI path must start with thepath
field value.
-
path
: Incoming request URI path (ifExact
) or its prefix (ifPrefix
). -
backend
: Reference to a backend or group of backends to process the requests with the specified domain name and URI path. Specify either a service backend (service
) or a backend group (resource
) but not both.-
service
: Managed Service for Kubernetes backend service for processing requests:name
: Managed Service for Kubernetes service name. TheService
resource this field refers to must be described in line with this configuration.port
: Service portIngress
is going to address. For the service port, specify either a number (number
) or a name (name
) but not both.
Warning
The Managed Service for Kubernetes services used as backends must be of the
NodePort
type. -
resource
: Reference to theHttpBackendGroup
group of backends to process the requests. A backend group can route traffic to either Managed Service for Kubernetes services or Yandex Object Storage buckets. When using a backend group, advanced Application Load Balancer functionality is available. You can also specify relative backend weights to allocate traffic to them in proportion.kind
:HttpBackendGroup
name
: Backend group name. The name must match the value specified in themetadata.name
field of theHttpBackendGroup
resource. TheHttpBackendGroup
resource this field refers to must be described in line with this configuration.apiGroup
:alb.yc.io
-
-
Ingress
resource example:apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: <resource_name> annotations: ingress.alb.yc.io/subnets: <IDs_of_subnets_in_three_availability_zones> ingress.alb.yc.io/security-groups: <L7_load_balancer_security_group_ID> ingress.alb.yc.io/external-ipv4-address: <static_public_IP_address> ingress.alb.yc.io/group-name: <resource_group_name> ingress.alb.yc.io/security-profile-id: <Smart_Web_Security_security_profile_ID> ingress.alb.yc.io/autoscale-min-zone-size: <minimum_number_of_L7_load_balancer_resource_units_per_zone> spec: ingressClassName: <IngressClass_resource_name> tls: - hosts: - <service_domain_name> secretName: yc-certmgr-cert-id-<certificate_ID> rules: - host: <service_domain_name> http: paths: - path: / pathType: Prefix backend: service: name: <Kubernetes_service_name> port: number: <443_or_another_port_number>
-
-
Use the following command to create the
Ingress
resource:kubectl apply -f <Ingress_resource_file>
-
-
An L7 load balancer will be deployed based on the
Ingress
resource configuration. Wait until its creation is complete andIngress
has a public IP address linked. You will need this IP address to check requests. You can view resource info using this command:kubectl get ingress <Ingress_resource_name> -w
Test the L7 load balancer
Run a test request to the service through the L7 load balancer, for example, using one of these methods:
-
Add this record to the
hosts
file on your workstation:<L7_load_balancer_public_IP_address> <service_domain_name>
. Delete the record after the test. -
Execute the request using cURL
depending on the protocol type:curl http://<service_domain_name> \ --resolve <service_domain_name>:<service_port>:<public_IP_address_of_L7_load_balancer>
curl https://<service_domain_name> \ --resolve <service_domain_name>:<service_port>:<public_IP_address_of_L7_load_balancer>
Migrate user traffic from the network load balancer to the L7 load balancer
Select one of these migration options:
Keep the public IP address for your service
-
If your external network load balancer uses a dynamic public IP address, convert it to a static one.
-
Delete all listeners in the network load balancer to release the static public IP address. This will make your service unavailable through the network load balancer.
-
In the L7 load balancer, assign to the listener the public IP address previously used by the network load balancer:
-
Open the YAML file that describes the
Ingress
resource. -
Under
annotations
, in theingress.alb.yc.io/external-ipv4-address
field, specify the public IP address previously assigned to the network load balancer. -
Apply the changes using this command:
kubectl apply -f <Ingress_resource_file>
-
-
Wait for the
Ingress
resource to finish switching its public IP address. You can view resource information using this command:kubectl get ingress <Ingress_resource_name> -w
After the IP address changes, your service will again be available through the L7 load balancer.
-
Navigate to the L7 load balancer:
- In the management console
, navigate to the folder with the Managed Service for Kubernetes cluster. - Select Managed Service for Kubernetes.
- Select the cluster.
- Select
Network on the left and then the Ingress tab on the right. For yourIngress
resource, follow the L7 load balancer link in the Load balancer column. - Monitor the L7 load balancer's user traffic on the load balancer statistics charts.
- In the management console
-
Delete the released static public IP address previously reserved for the L7 load balancer.
-
Open the configuration file you used to create the L7 load balancer (
alb-k8s-http.tf
oralb-k8s-https.tf
). -
Delete the
yandex_vpc_address
resource description from the file:resource "yandex_vpc_address" "static-address" { description = "Static public IP address for the Application Load Balancer" name = "alb-static-address" external_ipv4_address { zone_id = "ru-central1-a" ddos_protection_provider = "qrator" } }
-
Apply the changes:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validate
If the configuration is correct, you will get this message:
Success! The configuration is valid.
-
Run this command:
terraform plan
You will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply
-
Type
yes
and press Enter to confirm the changes.
-
-
-
Optionally, delete the network load balancer after migrating user traffic to the L7 load balancer.
Do not keep the public IP address for your service
-
To migrate user traffic from a network load balancer to an L7 load balancer, in the DNS service of your domain's public zone, update the
A
record value for the service domain name to point to the public IP address of the L7 load balancer. If the public domain zone was created in Yandex Cloud DNS, update the record using this guide.Note
The propagation of DNS record updates depends on the time-to-live (TTL) value and the number of links in the DNS request chain. This process can take a while.
-
As the DNS record updates propagate, monitor the increase in requests to the L7 load balancer:
- In the management console
, navigate to the folder with the Managed Service for Kubernetes cluster. - Select Managed Service for Kubernetes.
- Select the cluster.
- Select
Network on the left and then the Ingress tab on the right. For yourIngress
resource, follow the L7 load balancer link in the Load balancer column. - Monitor the L7 load balancer's user traffic on the load balancer statistics charts.
- In the management console
-
Monitor the decrease in traffic on the network load balancer using the
processed_bytes
andprocessed_packets
load balancer metrics. You can create a dashboard to visualize these metrics. No traffic on the external network load balancer over time indicates the L7 load balancer is now handling all user traffic. -
Optionally, delete the network load balancer after migrating user traffic to the L7 load balancer.