Creating an L7 load balancer with DDoS protection using the management console or CLI
To create an L7 load balancer with DDoS protection using the Yandex Cloud management console or CLI:
- Get your cloud ready.
- Create a cloud network.
- Create security groups.
- Create an instance group.
- Reserve a static public IP address.
- Create a backend group.
- Create an HTTP router.
- Create an L7 load balancer.
- Test the load balancer.
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:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders here.
Required paid resources
The infrastructure support cost for a DDoS-protected load balancer includes:
- Fee for continuously running VMs (see Yandex Compute Cloud pricing).
- Fee for using a public static IP address (see Yandex Virtual Private Cloud pricing).
- Fee for filtering incoming traffic to a public IP address with DDoS protection (see Yandex Virtual Private Cloud pricing).
- Fee for using computing resources of the L7 load balancer (see Application Load Balancer pricing).
Prepare a service account
Alert
When creating instance groups, keep the limits in mind. To ensure Instance Groups runs properly, don't manually modify or delete any of the resources it created: target group Application Load Balancer, VMs, or disks. Instead of this, change or delete the entire group.
By default, all operations in Instance Groups are performed on behalf of a service account. If you don't have a service account, create one.
To be able to create, update, and delete VMs in the group, as well as integrate the group with an Application Load Balancer L7 load balancer, assign the editor role to the service account.
Create a cloud network
All resources you create in the tutorial will belong to the same cloud network.
To create a network:
- In the management console
, select Virtual Private Cloud. - Click Create network.
- Enter
ddos-networkas the network name. - In the Advanced field, select Create subnets.
- Click Create network.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
-
Create a cloud network in the default folder:
yc vpc network create \ --name ddos-networkFor more information about the
yc vpc network createcommand, see the CLI reference. -
Create subnets in each availability zone by specifying the cloud network ID using the
--network-nameflag:yc vpc subnet create \ --name ddos-network-ru-a \ --network-name ddos-network \ --zone ru-central1-a \ --range 192.168.0.0/24yc vpc subnet create \ --name ddos-network-ru-b \ --network-name ddos-network \ --zone ru-central1-b \ --range 192.168.1.0/24yc vpc subnet create \ --name ddos-network-ru-d \ --network-name ddos-network \ --zone ru-central1-d \ --range 192.168.2.0/24For more information about the
yc vpc subnet createcommand, see the CLI reference.
Create security groups
Security groups include rules that allow the load balancer to receive inbound traffic and redirect it to the VMs so they can receive it. In this tutorial, we will create two security groups: one for the load balancer and another one for all VMs.
To create security groups:
-
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.
-
Specify the security group Name:
ddos-sg-balancer. -
Select Network
ddos-network. -
Under Rules, create the following rules using the instructions below the table:
Traffic
directionDescription Port range Protocol Source /
destinationAdd OutboundanyAllAnyCIDR0.0.0.0/0Inboundext-http80TCPCIDR0.0.0.0/0Inboundext-https443TCPCIDR0.0.0.0/0Inboundhealthchecks30080TCPLoad 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 range of ports open for inbound or outbound traffic.
-
In the Protocol field, specify the appropriate protocol or leave
Anyto allow traffic transmission over any protocol. -
In the Destination name or Source field, select the rule purpose:
CIDR: Rule will apply to the range of IP addresses. In the CIDR blocks field, specify the CIDR and subnet masks 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 these steps to create all rules from the table.
-
-
Click Save.
-
-
Similarly, create a security group for the VM named
ddos-sg-vmswith the sameddos-networkand the following rules:Traffic
directionDescription Port range Protocol Source CIDR blocks Inboundbalancer80TCPSecurity groupddos-sg-balancerInboundssh22TCPCIDR0.0.0.0/0
To create a security group for your load balancer, run this command:
yc vpc security-group create \
--name ddos-sg-balancer \
--rule "direction=egress,port=any,protocol=any,v4-cidrs=[0.0.0.0/0]" \
--rule "direction=ingress,port=80,protocol=tcp,v4-cidrs=[0.0.0.0/0]" \
--rule "direction=ingress,port=443,protocol=tcp,v4-cidrs=[0.0.0.0/0]" \
--rule "direction=ingress,port=30080,protocol=tcp,predefined=loadbalancer_healthchecks" \
--network-name ddos-network
To create a security group for your VM, run this command:
yc vpc security-group create \
--name ddos-sg-vms \
--rule "direction=ingress,port=22,protocol=tcp,v4-cidrs=[0.0.0.0/0]" \
--rule "direction=ingress,port=80,protocol=tcp,security-group-id=<security_group_ID>" \
--network-name ddos-network
Where security-group-id is the ddos-sg-balancer security group ID.
For more information about the yc vpc security-group create command, see the CLI reference.
Create an instance group
Your application backends will be deployed on the VM instance from the target group. The VM target group will be connected to the load balancer, thus enabling requests to the backend endpoints of your application. A minimum configuration instance group is sufficient for this tutorial.
To create an instance group:
-
In the management console
, select Compute Cloud. -
In the left-hand panel, select
Instance groups. Click Create group of virtual machines. -
Under Basic parameters:
- Enter the instance group Name:
ddos-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, as well as integrate the group with an Application Load Balancer L7 load balancer, assign the
editorrole to the service account.
- Enter the instance group Name:
-
Under Allocation, select multiple availability zones to ensure the fault tolerance of your hosting.
-
Under Instance template, click Define to configure the base VM instance:
-
Under General information, enter a Description for the template.
-
Under Boot disk image, open the Marketplace tab and click Show all Marketplace products. Select LEMP and click Use.
-
Under Disks and file storages, specify:
- Type:
HDD - Disk Size:
3 GB
- Type:
-
Under Computing resources, specify:
- Platform:
Intel Cascade Lake - vCPU:
2 - Guaranteed vCPU performance:
5% - RAM:
1 GB
- Platform:
-
Under Network settings:
- Select the cloud network named
ddos-networkand its subnets. - In the Public address field, select
Auto. - Select the
ddos-sg-vmssecurity group.
- Select the cloud network named
-
Under Access, specify the VM access credentials:
- Under Login, enter the 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 Creating an SSH key pair.
-
Click Save.
-
-
Under Scaling, specify the instance group Size:
2. -
Under Integration with Application Load Balancer, select Create target group and enter
tg-ddosas the group name. Read more about target groups. -
Click Create.
-
Get the resource IDs required to create an instance group using the following commands:
- yc iam service-account get <service_account_name>: For the service account.
- yc vpc network get ddos-network: For
ddos-network. - yc vpc subnet get <subnet_name>: For the
ddos-network-ru-a,ddos-network-ru-b, andddos-network-ru-dsubnets. - yc compute image get-latest-by-family lemp --folder-id standard-images: For the boot disk image.
- yc vpc security-group get ddos-sg-vms: For the
ddos-sg-vmssecurity group.
-
Create a YAML file named
specification.yaml. -
Add to it the description of the base instance configuration:
name: ddos-group service_account_id: <service_account_ID> // The service account must have the editor role. description: "DDoS alb scenario" instance_template: platform_id: standard-v3 resources_spec: memory: 1g cores: 2 core_fraction: 5 boot_disk_spec: mode: READ_WRITE disk_spec: image_id: <image_ID> type_id: network-hdd size: 3g network_interface_specs: - network_id: <cloud_network_ID> subnet_ids: - <subnet_ID_in_ru-central1-a> - <subnet_ID_in_ru-central1-b> - <subnet_ID_in_ru-central1-d> primary_v4_address_spec: {} security_group_ids: - <security_group_ID> deploy_policy: max_unavailable: 1 max_expansion: 0 scale_policy: fixed_scale: size: 2 allocation_policy: zones: - zone_id: ru-central1-a - zone_id: ru-central1-b - zone_id: ru-central1-d application_load_balancer_spec: target_group_spec: name: tg-ddosWhere
security_group_idsis theddos-sg-vmssecurity group ID. -
Create an instance group in the default folder:
yc compute instance-group create \ --file specification.yamlResult:
done (25s) id: cl1qjhlcdofg******** folder_id: b1g86q4m5vej******** created_at: "2021-08-30T19:25:02.031Z" name: ddos-group description: DDoS scenario instance_template: platform_id: standard-v2 resources_spec: memory: "1073741824" cores: "2" core_fraction: "5" boot_disk_spec: mode: READ_WRITE disk_spec: type_id: network-hdd size: "3221225472" image_id: fd8r6kq84o7b******** network_interface_specs: - network_id: enp3srbi9u49******** subnet_ids: - e9b17pi15695******** - e2lt87g1rlig******** - b0c7kl8riq24******** primary_v4_address_spec: {} security_group_ids: - enpi08rif04d******** scheduling_policy: {} scale_policy: fixed_scale: size: "2" deploy_policy: max_unavailable: "1" startup_duration: 0s strategy: PROACTIVE allocation_policy: zones: - zone_id: ru-central1-a - zone_id: ru-central1-b - zone_id: ru-central1-d load_balancer_state: {} managed_instances_state: target_size: "2" service_account_id: aje2stn6id9k******** status: ACTIVE application_load_balancer_spec: target_group_spec: name: first-target-group application_load_balancer_state: target_group_id: ds78imh0ds2e********For more information about the
yc compute instance-group createcommand, see the CLI reference.
Reserve a static public IP address
Note
You cannot do this using the CLI.
To protect a load balancer against DDoS attacks, reserve a static public IP address with the DDoS protection option enabled:
- In the management console
, select Virtual Private Cloud. - In the left-hand panel, select
IP addresses and click Reserve address. - Select the availability zone where you want to reserve the IP address.
- Enable DDoS protection.
- Click ** Reserve**.
Create a backend group
You must link the target group created along with the instance 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 over a certain period.
To create a backend group:
- In the management console
, select Application Load Balancer. - In the left-hand panel, select
Backend groups. Click Create backend group. - Enter the backend group Name:
ddos-backend-group. - Under Backends, click Add.
- Enter the backend Name:
backend-1. - In the Target groups field, select the
tg-ddosgroup. - Specify the Port the backend VMs will use to receive inbound traffic from the load balancer:
80. - Click Add health check.
- Specify the Port the backend VMs will use to accept health check connections:
80. - Specify the Path the load balancer will use for health checks:
/. - Click Create.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
-
Create a backend group:
yc alb backend-group create ddos-backend-groupResult:
id: a5dg2cv4ngne******** name: ddos-backend-group folder_id: aoerb349v3h4******** created_at: "2021-08-08T20:46:21.688940670Z"For more information about the
yc alb backend-group createcommand, see the CLI reference. -
Add a backend and a health check to the group:
yc alb backend-group add-http-backend \ --backend-group-name ddos-backend-group \ --name backend-1 \ --weight 1 \ --port 80 \ --target-group-id=<target_group_ID> \ --http-healthcheck timeout=1s,interval=1s,port=80,path=/Where:
--backend-group-name: Backend group name.--name: Backend name.--weight: Backend weight.--port: Port.--target-group-id: Target group ID.--http-healthcheck: Resource health check properties.port: Port.timeout: Timeout.interval: Interval.host: Host address.path: Path.
Result:
done (21s) id: ds7fea2pggr2******** name: ddos-backend-group folder_id: b1g86q4m5vej******** http: backends: - name: backend-1 backend_weight: "1" port: "80" target_groups: target_group_ids: - ds78ate00f8e******** healthchecks: - timeout: 1s interval: 1s healthcheck_port: "80" http: path: / created_at: "2021-08-08T07:59:22.922603189Z"For more information about the
yc alb backend-group add-http-backendcommand, see the CLI reference.
Create an HTTP router
The backend group should be linked to an HTTP router that defines the HTTP routing rules.
To create an HTTP router and add a route to it:
- In the management console
, select Application Load Balancer. - In the left-hand panel, select
HTTP routers. Click Create HTTP router. - Specify the HTTP router Name:
ddos-router. - Click Add virtual host.
- Specify the virtual host Name:
ddos-host. - Specify the Authority value:
alb-with-ddos.com. - Click Add route.
- Enter
route-1as the Name. - In the Path field, select
Starts withand specify the/path. - In the Action field, keep
Routing. - From the Backend group list, select the group you created earlier.
- Do not change the other settings. Click Create.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
-
Run this command:
yc alb http-router create ddos-routerResult:
id: a5dcsselagj4******** name: ddos-router folder_id: aoerb349v3h4******** created_at: "2021-08-08T21:04:59.438292069Z"For more information about the
yc alb http-router createcommand, see the CLI reference. -
Create a virtual host by specifying the HTTP router name:
yc alb virtual-host create ddos-host \ --http-router-name ddos-router --authority alb-with-ddos.comFor more information about the
yc alb virtual-host createcommand, see the CLI reference. -
Add a route by specifying the router name and the routing settings:
yc alb virtual-host append-http-route route-1 \ --virtual-host-name ddos-host \ --http-router-name ddos-router \ --prefix-path-match / \ --backend-group-name ddos-backend-group \ --request-timeout 60sResult:
done (1s) name: ddos-host routes: - name: route-1 http: match: path: prefix_match: / route: backend_group_id: ds7fea2pggr2******** timeout: 60sFor more information about the
yc alb virtual-host append-http-routecommand, see the CLI reference.
Create a load balancer
To create a load balancer:
-
In the management console
, select Application Load Balancer. -
In the left-hand menu, select Load balancers.
-
Click Create L7 load balancer.
-
Enter the load balancer name:
ddos-protect-alb. -
Under Network settings, select
ddos-networkand theddos-sg-balancersecurity group. -
Under Allocation, select the subnets for the load balancer nodes in each availability zone and enable traffic.
-
Under Listeners, click Add listener. Configure the listener settings:
- Specify the listener name:
ddos-listener. - Under Public IP address, enable traffic.
- Set the port to
80. - Select the List type and specify the previously reserved IP address with DDoS protection.
- Specify the listener name:
-
In the HTTP router field, select
ddos-router. -
Click Create.
-
Create a load balancer with nodes in the cloud network subnets:
yc alb load-balancer create ddos-protect-alb \ --network-name ddos-network \ --location subnet-name=ddos-network-ru-a,zone=ru-central1-a \ --location subnet-name=ddos-network-ru-b,zone=ru-central1-b \ --location subnet-name=ddos-network-ru-d,zone=ru-central1-dFor more information about the
yc alb load-balancer createcommand, see the CLI reference. -
Add a listener:
yc alb load-balancer add-listener ddos-protect-alb \ --listener-name ddos-listener \ --http-router-id <HTTP_router_ID> \ --external-ipv4-endpoint port=80, address=<IP_address_with_DDoS_protection>For more information about the
yc alb load-balancer add-listenercommand, see the CLI reference.
After creating the load balancer, test it.
Test the load balancer
Check that the service is available on the alb-with-ddos.com host. To do this, run the following command:
curl \
--header "Host: alb-with-ddos.com" \
http://<L7_load_balancer_IP_address>
Result:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
How to delete the resources you created
To shut down the hosting and stop paying for the resources you created:
-
Delete the non-billable resources that prevent the deletion of billable resources:
-
Delete the instance group named
ddos-group. -
Delete the static public IP address you reserved.