All Products
Search
Document Center

Add elastic container instances to an SLB instance

Last Updated: Sep 07, 2021

Server Load Balancer (SLB) is a service that distributes network traffic to backend servers to increase the throughput of your applications. You can use SLB to prevent service interruptions caused by single points of failure (SPOFs) and improve the availability of applications. This topic describes how to add elastic container instances as backend servers to an SLB instance and configure a listener to forward Internet traffic to the elastic container instances.

Background information

SLB consists of the following parts:

  • SLB instance: a running SLB service that receives traffic from frontend applications and forwards the traffic to backend servers.

  • Listener: a device that checks client requests and forwards them to backend servers.

  • Backend servers: a group of servers that receive frontend requests.

Note

SLB is categorized into Classic Load Balancer (CLB) and Application Load Balancer (ALB). Elastic container instances can be added only to CLB instances.

Before you begin

  1. Create multiple elastic container instances.

    In this example, create two elastic container instances that run the NGINX web server. Enable the logging feature when you create the elastic container instances. For more information, see Use an NGINX image to create an elastic container instance.

    slb1
  2. Create a CLB instance.

    Create a CLB instance that has Internet access. For more information, see Create a CLB instance.

    slb2

Procedure

After you add elastic container instances as backend servers to the CLB instance and configure a listener, you can forward requests to the elastic container instances.

Note

If an elastic container instance is in a final state such as the Succeeded or Failed state, the instance cannot be added to a CLB instance.

You can call CLB API operations to add the elastic network interfaces (ENI) that is bound to your elastic container instance to the CLB instance and configure a listener. Perform the following steps:

  1. Call the DescribeContainerGroups operation provided by Elastic Container Instance to query the information about your elastic container instance.

    Obtain the following information about your elastic container instance from the response parameters:

    • The ID of your ENI instance, which is indicated by the EniInstanceId parameter.

    • The internal IP address of your elastic container instance, which is indicated by the IntranetIp parameter.

  2. Call the AddBackendServers operation to add a backend server.

    The following table describes the main request parameters. For more information, see AddBackendServers.

    Parameter

    Type

    Example

    Description

    LoadBalancerId

    String

    lb-2ze7o5h52g02kkzz******

    The ID of the CLB instance.

    BackendServers

    String

    [{ "ServerId": "eni-6wejdtelaz2bv526****", "Weight": "100", "Type": "eni", "ServerIp": "172.16.12.**", "Port":"80","Description":"test" },{ "ServerId": "eni-6wejdtelaz2bv321****", "Weight": "100", "Type": "eni", "ServerIp": "172.16.12.**", "Port":"80","Description":"test" }]

    The list of backend servers that you want to add to the CLB instance. Configure the following parameters:

    • ServerId: The ID of the backend server. Enter the ID of the ENI instance that is bound to the elastic container instance.

    • Weight: The weight of the backend server. The value ranges from 0 to 100. The default value is 100. If the value is set to 0, no requests are forwarded to the backend server.

    • Description: The description of the backend server. The description must be 1 to 80 characters in length.

    • Type: The type of the backend server. Set this parameter to eni.

    • ServerIp: The IP address of the backend server. Enter the internal IP address of the elastic container instance.

  3. Call one of the following CLB listener-related API operations to create a listener.

    • CreateLoadBalancerTCPListener: creates a TCP listener.

    • CreateLoadBalancerUDPListener: creates a UDP listener.

    • CreateLoadBalancerHTTPListener: creates an HTTP listener.

    • CreateLoadBalancerHTTPSListener: creates an HTTPS listener.

    In this example, a TCP listener is created. The following table describes the main request parameters that are used to create the TCP listener. For more information, see CreateLoadBalancerTCPListener.

    Parameter

    Type

    Example

    Description

    Bandwidth

    Integer

    -1

    The maximum bandwidth of the listener. Valid values:

    • -1: For a pay-by-traffic Internet-facing CLB instance, set this value to -1. This value indicates that the bandwidth is unlimited.

    • 1 to 5120: For a pay-by-bandwidth Internet-facing CLB instance, you can specify the maximum bandwidth of each listener. The sum of the maximum bandwidth values of all listeners cannot exceed the maximum bandwidth of the CLB instance. Unit: Mbit/s.

    BackendServerPort

    Integer

    80

    The backend port that is used by the CLB instance.

    Valid values: 1 to 65535.

    LoadBalancerId

    String

    lb-2ze7o5h52g02kkzz******

    The ID of the CLB instance.

    ListenerPort

    Integer

    80

    The frontend port that is used by the CLB instance.

    Valid values: 1 to 65535.

  4. Call the StartLoadBalancerListener operation to enable the listener.

Verify that the elastic container instance is added to the CLB instance

  1. Run the following command multiple times to query the public IP address of the CLB instance:

    curl 39.106.**.** 80
  2. View the logs of the elastic container instances. The requests are distributed to different elastic container instances by the CLB instance.