Server Load Balancer (SLB) is a traffic distribution service that distributes inbound network traffic across backend servers based on forwarding rules. SLB improves the service capability and availability of applications. In addition to Elastic Compute Service (ECS) instances and elastic network interfaces (ENIs), SLB also supports Elastic Container Instances (ECIs). SLB is very important in scenarios where ECIs are used as backend servers or used as pods on virtual nodes of a Kubernetes cluster. This topic describes how to add ECIs as backend servers to an SLB instance.

Before you begin

Create ECIs

In this example, two ECIs are created and each ECI runs an NGINX web server. Port 80 is exposed to NGINX web server for both ECIs. We recommend that you enable ECI logging. For more information, see Log collection. The following figure shows the created ECIs.

Create an SLB instance

You can create an SLB instance or use an existing SLB instance. In this example, use an SLB instance that can access the Internet, as shown in the following figure.

Add ECIs as backend servers to the SLB instance

Currently, you can use the ENIs associated with ECIs in the console or call API operations to add the ECIs to the SLB instance. In this example, use the Java SDK to call API operations.

Add ECIs as backend servers
AddBackendServersRequest addBackendServersRequest = new AddBackendServersRequest();
addBackendServersRequest.setLoadBalancerId(SLB_ID);
List<BackendServer> backendServers = new ArrayList<>();
ECI_IDS.forEach(eciId -> {
    SetBackendServersResponse.BackendServer slbBackendServer = new SetBackendServersResponse.BackendServer();
    slbBackendServer.setServerId(eciId);
    slbBackendServer.setWeight("100");
    slbBackendServer.setType("eci");
    backendServers.add(slbBackendServer);
});
addBackendServersRequest.setBackendServers(new Gson().toJson(backendServers));
client.getAcsResponse(addBackendServersRequest);
For more information, see the complete sample code on GitHub.
Notice Only ECIs in the Pending, Running, Restarting, or Updating state can be added as backend servers to an SLB instance. ECIs in the Succeeded or Failed state cannot be added. ECIs in the Scheduling state may fail to be added.
Query the status of backend servers
// Before you call this operation, configure the SLB instance in the SLB console.
DescribeHealthStatusRequest describeHealthStatusRequest = new DescribeHealthStatusRequest();
describeHealthStatusRequest.setLoadBalancerId(SLB_ID);
try {
    client.getAcsResponse(describeHealthStatusRequest);
} catch (ClientException e) {
    e.printStackTrace();
}

For more information, see the complete sample code on GitHub.

Sample response:
{
    "backendServers":[
        {
            "bizProtocol":"tcp",
            "listenerPort":80,
            "port":80,
            "protocol":"tcp",
            "serverHealthStatus":"normal",
            "serverId":"eci-2ze7o9f7dlbi4jwx****",
            "serverIp":"192.168. *.**"
        },
        {
            "bizProtocol":"tcp",
            "listenerPort":80,
            "port":80,
            "protocol":"tcp",
            "serverHealthStatus":"normal",
            "serverId":"eci-2ze7o9f7dlbi4jwx****",
            "serverIp":"192.168. *.**"
        }
    ],
    "requestId":"4DC09389-910F-44D7-A60A-ABD73D9AA3B4"
}

The ECIs are added as backend servers. You can log on to the SLB console to view the added backend servers.

Remove ECIs from backend servers

You do not need to remove an ECI from backend servers of an SLB instance. An ECI will be automatically removed from backend servers when it is deleted. ECIs in the Succeeded or Failed state are also automatically removed from backend servers.
RemoveBackendServersRequest removeBackendServersRequest = new RemoveBackendServersRequest();
removeBackendServersRequest.setLoadBalancerId(SLB_ID);
List<SetBackendServersResponse.BackendServer> backendServers = new ArrayList<>();
ECI_IDS.forEach(eciId->{
    SetBackendServersResponse.BackendServer slbBackendServer = new SetBackendServersResponse.BackendServer();
    slbBackendServer.setServerId(eciId);
    slbBackendServer.setWeight("100");
    slbBackendServer.setType("eci");
    backendServers.add(slbBackendServer);
});
removeBackendServersRequest.setBackendServers(new Gson().toJson(backendServers));
try {
    client.getAcsResponse(removeBackendServersRequest);
} catch (ClientException e) {
    e.printStackTrace();
}

For more information, see the complete sample code on GitHub.

(Required for the first time) Configure the SLB instance in the SLB console

Configure listeners

Log on to the SLB console. You can view the ECIs added through API operations for the target SLB instance. Currently, the SLB console is not associated with the ECI console. Therefore, you cannot go to the ECI console from the SLB console. However, the ID of each ECI is correct. The SLB console and the ECI console will be associated soon.

Specify the scheduling algorithm

The default scheduling algorithm is weighted round-robin (WRR).

Configure default backend servers

By default, NGINX listens to port 80. Set the port number to 80 for each backend server. Set the weight based on business needs. An SLB instance balances loads among its backend servers based on the ratio of their weights, instead of the weight values.

Configure health checks

Verify that ECIs are added to the SLB instance

Run the following curl command on the local computer multiple times to query the public IP address of the SLB instance:

liumihustdeMacBook-Pro:~ liumihust$ curl 47.96. **. *** 80	
View the logs of ECIs. The requests are distributed to two ECIs by the SLB instance.