Configure Server Load Balancer using Open API

Last Updated: Dec 05, 2017

In this tutorial, the request parameters are included in the request URL, and the URL does not include common parameters. For more information, see API overview.

Note: To increase readability, the parameter values of the request URL in this example are not URL-encoded.

Prerequisites

You have created 2 ECS instances and granted access to their SSH and Web ports.

Procedure

  1. Call CreateLoadBalancer interface to create a Server Load Balancer instance.

    Request:

    https://slb.aliyuncs.com/?Action=CreateLoadBalancer&RegionId=cn-hangzhou-dg-a01

    Response:

    1. {
    2. "RequestId":"3DE96B24-E2AB-4DFA-9910-1AADD60E13A5",
    3. "LoadBalancerId":"LoadBalancerId",
    4. "Address":"SLBIPAddress"
    5. }
  2. Call CreateLoadBalancerHttpListener interface to create a HTTP listener, of which the port is 80, for the Server Load Balancer instance.

    Request:

    https://slb.aliyuncs.com/?Action=CreateLoadBalancerHttpListener&LoadBalancerId=LoadBalancerId&ListenerPort=80&BackendServerPort=80&ListenerStatus=active

  3. Call SetLoadBalancerStatus interface to active the Server Load Balancer instance.

    Request:

    https://slb.aliyuncs.com/?Action=SetLoadBalancerStatus&LoadBalancerId=LoadBalancerId&LoadBalancerStatus=active

  4. Call AddBackendServers interface to add an ECS instance to backend servers.

    Request:

    https://slb.aliyuncs.com/?Action=AddBackendServers&LoadBalancerId=LoadBalancerId&BackendServers=[{"ServerId":"ECS1InstanceID"}]

    Response:

    1. {
    2. "RequestId" : "FA2F2172-63F2-409D-927C-86BD1D536F13",
    3. "LoadBalancerId" : "LoadBalancerId",
    4. "BackendServers" : {
    5. "BackendServer" : [
    6. {
    7. "ServerId" : "ECS1InstanceId",
    8. "Weight" : 100
    9. }
    10. ]
    11. }
    12. }
  5. Call AddBackendServers interface again to add an ECS instance to backend servers.

    Request:

    https://slb.aliyuncs.com/?Action=AddBackendServers&LoadBalancerId=LoadBalancerId&BackendServers=[{"ServerId":"ECS2InstanceID"}]

    Response:

    1. {
    2. "RequestId" : "C61FAD0A-2E87-4D0C-80B0-95AB758FCA70",
    3. "LoadBalancerId" : "LoadBalancerId",
    4. "BackendServers" : {
    5. "BackendServer" : [
    6. {
    7. "ServerId" : "ECS1InstanceId",
    8. "Weight" : 100
    9. },
    10. {
    11. "ServerId" : "ECS2InstanceId",
    12. "Weight" : 100
    13. }
    14. ]
    15. }
    16. }
  6. Call DescribeLoadBalancerAttribute interface to view the configuration of the Server Load Balancer instance.

    Request:

    https://slb.aliyuncs.com/?Action=DescribeLoadBalancerAttribute&LoadBalancerId=LoadBalancerId

    Response:

    1. {
    2. "RequestId" : "4747E9AE-ADFD-412D-B523-C1CBD45A2154",
    3. "LoadBalancerId" : "LoadBalancerId",
    4. "Address" : "SLBIPAddress",
    5. "IsPublicAddress" : "true",
    6. "ListenerPorts" : {
    7. "ListenerPort" : [
    8. 80
    9. ]
    10. },
    11. "BackendServers" : {
    12. "BackendServer" : [
    13. {
    14. "ServerId" : "ECS1InstanceId",
    15. "Weight" : 100
    16. },
    17. {
    18. "ServerId" : "ECS2InstanceId",
    19. "Weight" : 100
    20. }
    21. ]
    22. }
    23. }

    Use your browser to access the IP address of the Server Load Balancer instance to verify whether the service is working.

Thank you! We've received your feedback.