All Products
Search
Document Center

Server Load Balancer:Configure access control based on listeners and ports by using security groups

Last Updated:Dec 09, 2024

To regulate access from requests, you can configure a security group to implement access control based on protocols, ports, and IP addresses. Security groups provide finer-grained access control than access control lists. Security groups not only support access control based on protocols and ports, but also support access control policies that regulate access from IPv6 addresses.

Scenario

  • Before the ALB instance is added to a security group, the listener ports of the ALB instance allow all requests by default.

  • After the ALB instance is added to a security group which does not contain Deny rules, the listener ports of the ALB instance allow all requests by default. If you want to allow requests only from specific IP addresses to your ALB instance, you must also create a Deny rule.

If you want to control inbound access to your Application Load Balancer (ALB) instance, you can add the ALB instance to a security group and configure security group rules based on your security requirements.

Important
  • ALB outbound traffic refers to response packets that are returned to users. To ensure service continuity, ALB security groups do not block outbound packets. You do not need to create outbound rules.

  • When you create a security group, we recommend that you do not add the local IP address of ALB to Deny rules whose priority is 1. Otherwise, the security group rules may conflict with the managed security groups of ALB and interrupt communication between ALB and backend servers. To view the local IP address of an ALB instance, log on to the ALB console and go to the details page of the ALB instance.

If you want to block access to some listener ports but allow access to other listener ports, you can configure the security group to control access to the ports. The following table describes how to enable access control on ports for the ALB instance by configuring a security group in three different scenarios. In the scenarios, an HTTP listener that listens on port 80 and an HTTP listener that listens on port 81 are configured for the ALB instance.

Scenario

Security group rule

Expected result

References

Before the ALB instance is added to a security group

By default, the listener ports of the ALB instance allow all requests.

  • Clients can access the HTTP port 80 of the ALB instance

  • Clients can access the HTTP port 81 of the ALB instance

Step 5: Test network connectivity before the ALB instance is added to the security group

Add the ALB instance to a security group

Block requests to HTTP port 81

Note

This section describes only the security group rules that are relevant to this topic. Other default rules are not described.

  • Clients can access the HTTP port 80 of the ALB instance

  • Clients cannot access the HTTP port 81 of the ALB instance

Step 6: Add the ALB instance to a security group and verify the result

Modify the security group rules

  • Block requests to the HTTP port 80

  • Block requests to the HTTP port 81

Note

This section describes only the security group rules that are relevant to this topic. Other default rules are not described.

  • Clients cannot access the HTTP port 80 of the ALB instance

  • Clients cannot access the HTTP port 81 of the ALB instance

Step 7: Modify the security group rules and verify the result

Limits

Important

By default, security groups are unavailable. To use security groups, contact your account manager.

Category

Security group type

Description

Security groups supported by ALB

  • Basic security groups

  • Advanced security groups

  • The security group must be in a virtual private cloud (VPC), and the security group and ALB instance must be in the same VPC.

  • An ALB instance can be added to at most four security groups, which must be of the same type. The same ALB instance cannot be added to a basic security group and an advanced security group at the same time.

    To add an ALB instance in a basic security group to an advanced security group, remove the ALB instance from the basic security group before you can add the ALB instance to an advanced security group. To add an ALB instance in an advanced security group, remove the ALB instance from all basic security groups first.

For more information about basic security groups and advanced security groups, see Basic security groups and advanced security groups.

Security groups not supported by ALB

Managed security group

For more information about managed security groups, see Managed security groups.

Prerequisites

  • A virtual private cloud (VPC) named VPC1 is created. For more information, see Create and manage a VPC.

  • Two Elastic Compute Service (ECS) instances named ECS01 and ECS02 are created in VPC1. ECS01 and ECS02 function as backend servers of the ALB instance. Different applications are deployed on ECS01 and ECS02.

    • For more information about how to create an ECS instance, see Create an instance by using the wizard.

    • The following sample code show how to deploy test applications on ECS01 and ECS 02.

      Commands for deploying an application on ECS01

      yum install -y nginx
      systemctl start nginx.service
      cd /usr/share/nginx/html/
      echo "Hello World !  This is ECS01." > index.html

      Commands for deploying an application on ECS02

      yum install -y nginx
      systemctl start nginx.service
      cd /usr/share/nginx/html/
      echo "Hello World !  This is ECS02." > index.html

Configure resources

Step 1: Create a server group for the ALB instance

  1. Log on to the ALB console.
  2. In the top navigation bar, select the region in which you want to create a server group. In this example, China (Hangzhou) is selected.

  3. In the left-side navigation pane, choose ALB > Server Groups.

  4. On the Server Groups page, click Create Server Group.

  5. In the Create Server Group dialog box, configure the parameters and click Create.

    The following table describes the parameters that are relevant to this topic. Use the default values for the other parameters. For more information, see Create and manage server groups.

    Parameter

    Description

    Server Group Type

    Select a type of server group. In this example, Server is selected.

    Server Group Name

    Enter a name for the server group.

    VPC

    Select the VPC in which you want to create the server group. In this example, VPC1 is selected.

    Backend Server Protocol

    Select a backend protocol. In this example, HTTP is selected

    Scheduling Algorithm

    Select a scheduling algorithm. In this example, Weighted Round-robin is selected.

  6. In the The server group is created dialog box, click Add Backend Server.

  7. On the Backend Servers tab, click Add Backend Server.

  8. In the Add Backend Server panel, select ECS01 and ECS02 and click Next.

  9. Specify ports and weights for the backend servers and click OK.

Step 2: Create an ALB instance and configure listeners

  1. Log on to the ALB console.
  2. On the Instances page, click Create ALB.

  3. On the buy page, configure the following parameters.

    The following section describes only some of the parameters. Other parameters use the default values. For more information, see Create an ALB instance.

    • Region: the region in which you want to create the ALB instance. In this example, China (Hangzhou) is selected.

    • Network Type: the network type of the ALB instance. In this example, Internet is selected.

    • VPC: the VPC in which you want to create the ALB instance. In this example, VPC1 is selected.

  4. Click Buy Now and complete the payment.

  5. Return to the Instances page and click the ID of the ALB instance.

  6. Click the Listener tab and then click Quick Create Listener.

  7. In the Quick Create Listener dialog box, configure the parameters and click OK. In this example, an HTTP listener that listens on port 80 is created. The following table describes the parameters.

    Parameter

    Description

    Listener Protocol

    Select a listener protocol. In this example, HTTP is selected.

    Listener Port

    Enter a listener port. In this example, port 80 is specified.

    Server Group

    Select Server Type and select a server group from the drop-down list next to Server Type. In this example, the server group created in Step 1 is selected.

  8. On the Listener tab, click Quick Create Listener.

  9. In the Quick Create Listener dialog box, configure the parameters and click OK. In this example, an HTTP listener that listens on port 81 is created. The following table describes the parameters.

    Parameter

    Description

    Listener Protocol

    Select a listener protocol. In this example, HTTP is selected.

    Listener Port

    Enter a listener port. In this example, port 81 is specified.

    Server Group

    Select Server Type and select a server group from the drop-down list next to Server Type. In this example, the server group created in Step 1 is selected.

Step 3: Configure a CNAME record

In actual business scenarios, we recommend that you use CNAME records to map custom domain names to the domain name of your ALB instance.

  1. In the left-side navigation pane, choose ALB > Instances.

  2. On the Instances page, copy the domain name of the ALB instance.

  3. Perform the following steps to create a CNAME record:

    Note

    If your domain name is not registered by using Alibaba Cloud Domains, you must add your domain name to Alibaba Cloud DNS before you can configure a DNS record. For more information, see Manage domain names.

    1. Log on to the Alibaba Cloud DNS console.

    2. On the Authoritative DNS Resolution page, find your domain name and click DNS Settings in the Actions column.

    3. On the DNS Settings tab of the domain name details page, click Add DNS Record.

    4. In the Add DNS Record panel, configure the parameters and click OK. The following table describes the parameters.

      Parameter

      Description

      Record Type

      Select CNAME from the drop-down list.

      Hostname

      Enter the prefix of the domain name. In this example, @ is entered.

      Note

      If you use a root domain name, enter @.

      DNS Request Source

      Select Default.

      Record Value

      Enter the CNAME, which is the domain name of the ALB instance.

      TTL

      Select a time-to-live (TTL) value for the CNAME record to be cached on the DNS server. In this example, the default value is used.

Step 4: Create a security group

Before you can add the ALB instance to a security group, you need to create a security group in the ECS console.

  1. Log on to the ECS console.

  2. In the left-side navigation pane, choose Network & Security > Security Groups.

  3. In the top navigation bar, select the region in which you want to create a security group. In this example, China (Hangzhou) is selected.

  4. On the Security Groups page, click Create Security Group.

  5. On the Create Security Group page, configure the parameters in the Basic Information section.

    The following section describes only the parameters that are relevant to this topic. For more information about other parameters, see Create a security group.

    • Quick Create Listener: In this example, VPC1 is selected.

    • Security Group Type: In this example, Basic Security Group is selected.

  6. Add an access rule to the security group.

    1. On the Inbound tab, click Add Rule.

    2. Configure the parameters and click Create Security Group. The following table describes the parameters.

      Parameter

      Description

      Action

      Select Deny.

      Priority

      The default value 1 is used.

      Protocol Type

      Select Custom TCP.

      Note

      To add a security group rule for a QUIC listener, select Custom UDP.

      Port Range

      Enter the port number 81.

      Authorization Object

      Select All IPv4 Addresses (0.0.0.0/0) and All IPv6 Addresses ((::/0).

      Description

      Enter a description for the security group rule.

Step 5: Test the network connectivity before the ALB instance is added to the security group

Test the network connectivity between a client and ECS01 and ECS02. In this example, a client that has Internet access is used.

  1. Enter the domain name and port number into the address bar of the browser, such as http://Domain name:80 and press Enter. The following figure shows that the ALB instance can access the HTTP port 80.

    image

  2. Enter the domain name and port number into the address bar of the browser, such as http://Domain name:81 and press Enter. The following figure shows that the ALB instance can access the HTTP port 81.

    image

Step 6: Add the ALB instance to the security group and verify the result

Add the ALB instance to the security group and test whether the security group rule takes effect on the ports.

  1. Log on to the ALB console.
  2. On the Instances page, click the ID of the ALB instance that you want to manage. On the Instance Details tab, click the Security Groups tab.

  3. On the Security Groups tab, click Create Security Group. In the Add ALB Instance to Security Group dialog box, select the security group created in Step 4: Create a security group, and click OK.

  4. In the left-side panel, click the ID of the security group that you want to manage. You can click the Inbound Policies or Outbound Policies tab to view the security group rules.

    The following table describes only the inbound rules that are relevant to this topic. The ALB instance uses the following security group rules.

    Action

    Priority

    Protocol type

    Port range

    Authorization object

    Allow

    1

    Custom TCP

    Destination: 80/80

    Source: All IPv4 Addresses (0.0.0.0/0)

    Allow

    1

    Custom TCP

    Destination: 80/80

    Source: All IPv6 Addresses (::/0)

    Deny

    1

    Custom TCP

    Destination: 81/81

    Source: All IPv4 Addresses (0.0.0.0/0)

    Deny

    1

    Custom TCP

    Destination: 81/81

    Source: All IPv6 Addresses (::/0)

  5. After you modify the security group rules, test the accessibility of the ports.

    1. Enter the domain name and port number into the address bar of the browser, such as http://Domain name:80 and press Enter. The following figure shows that the ALB instance can access the HTTP port 80.

      image

    2. Enter the domain name and port number into the address bar of the browser, such as http://Domain name:81 and press Enter. If you cannot access the application, open the command-line interface (CLI) and run the following command: curl http://Domain name:81. The following figure shows that the client cannot access the HTTP port 81 on the ALB instance. This result indicates that the security group rules take effect on port 81.

      image

Step 7: Modify the security group rules and verify the result

Modify the security group rules and test whether the security group rules can take effect on the ports.

  1. Return to the Instances page and click the ID of the ALB instance. On the Instance Details tab, click the Security Groups tab.

  2. In the Basic Information section, click the ID of the security group. You can also click ECS Console in the upper-right corner of the Security Groups tab to go to the Security Group Details page.

  3. On the Security Group Detail page, find the security group rule that allows access to TCP port 80 and click Modify in the Actions column. Set Action to Deny and click Save.

    The following table describes only the security group rules that are relevant to this topic. The following table describes the new security group rules.

    Action

    Priority

    Protocol type

    Port range

    Authorization object

    Deny

    1

    Custom TCP

    Destination: 80/80

    Source: All IPv4 Addresses (0.0.0.0/0)

    Deny

    1

    Custom TCP

    Destination: 80/80

    Source: All IPv6 Addresses (::/0)

    Deny

    1

    Custom TCP

    Destination: 81/81

    Source: All IPv4 Addresses (0.0.0.0/0)

    Deny

    1

    Custom TCP

    Destination: 81/81

    Source: All IPv6 Addresses (::/0)

  4. After you modify the security group rules, test the accessibility of the ports.

    1. Open the CLI and run the following command: curl http://Domain name:80. The following figure shows that the client cannot access the HTTP port 80 on the ALB instance. This result indicates that the security group rules take effect on port 80.

      image

    2. Open the CLI and run the following command: curl http://Domain name:81. The following figure shows that the client cannot access the HTTP port 81 on the ALB instance. This result indicates that the security group rules take effect on port 81.

      image

References