All Products
Search
Document Center

Elastic Container Instance:Attach elastic container instances to an SLB instance

Last Updated:Dec 28, 2023

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 attach 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

The SLB service consists of an SLB instance, a listener, and a backend server. The following items describe how to attach elastic container instances to an SLB instance.

  1. Create an elastic container instance.

    Before you build an SLB service, you must select a region to create elastic container instances and plan the network based on your business requirements.

  2. Create an SLB instance

    Before you build an SLB service, you must create an SLB instance. Each SLB instance is a service entity that receives network traffic and forwards the traffic to backend servers.

    The SLB service is categorized into Classic Load Balancer (CLB) and Application Load Balancer (ALB). You can select an SLB type based on your business requirements. For information about the differences between CLB and ALB, see SLB overview.

  3. Attach elastic container instances as backend servers to the SLB instance

    Backend servers are a group of servers that receive frontend requests. After you attach elastic container instances as backend servers to an SLB instance, the elastic container instances can receive client requests that are forwarded by the SLB instance. If you use an ALB instance, you must create a server group before you can attach elastic container instances to the ALB instance. If you use a CLB instance, you can directly add elastic container instances to the default server group.

  4. Configure a listener

    A listener checks and forwards client requests to backend servers. You must configure a listener for your SLB instance. A listener includes configuration items such as protocol, ports, and scheduling algorithms.

In this topic, CLB is used as an example to describe how to attach elastic container instances as backend servers. You can follow the same procedure to attach elastic container instances to an ALB instance. For more information, see Use an ALB instance to provide IPv4 services.

Preparations

  1. Create multiple elastic container instances.

    In this example, two elastic container instances that run the NGINX web service are created. When you create the elastic container instances, enable the logging feature. For more information, see Use an NGINX image to create an elastic container instance.

    slb1

  2. Create a CLB instance.

    In this example, a CLB instance that supports Internet access is created. For more information, see Create and manage a CLB instance.

    slb2

Procedure

After you attach elastic container instances as backend servers to the CLB instance and configure a listener, the CLB instance forwards requests to the elastic container instances.

Note

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

You can call a CLB API operation by using OpenAPI Explorer to mount an elastic network interface (ENI) to the elastic container instance and configure a listener. Procedure:

  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, 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. The following parameters are included:

    • ServerId: the ID of the backend server. Enter the ID of the ENI that is mounted 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.

    • Type: the type of the backend server. In this example, set the value to eni.

    • ServerIp: the internal IP address of the instance. In this example, enter the internal IP address of the elastic container instance.

    • Port: the backend port that receives requests.

  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 the result

  1. Run the following command multiple times to send requests to 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.