Configure Server Load Balancer using API

Last Updated: Sep 14, 2017

In this tutorial, an ECS instance deployed with a static web page using Nginx is used as an example, and a security rule for allowing access through SSH and web ports is added for the ECS instance.

Task 1 Clone an ECS instance

  1. Create a snapshot for the system disk.

    1. Query the system disk ID of the instance.

      Request:

      https://ecs.aliyuncs.com/?Action=DescribeInstanceDisks&InstanceId=id5ab1760-3498-4d95-9687-a91545ef90b3

      Response:

      1. {
      2. "RequestId" : "9F2188AC-AFAC-4F43-B452-C88463B9F069",
      3. "Disks" : {
      4. "Disk" : [
      5. {
      6. "DiskId" : "1008-27930",
      7. "Size" : 20,
      8. "Type" : "system"}]
      9. }
      10. }
    2. Create a snapshot for the system disk.

      Request:

      https://ecs.aliyuncs.com/?Action=CreateSnapshot&InstanceId=id5ab1760-3498-4d95-9687-a91545ef90b3&DiskId=1008-27930&SnapshotName=mytesthost1-init

      Response:

      1. {
      2. "RequestId" : "5CA4F9E6-81D2-42E1-A317-4C25284C6939",
      3. "SnapshotId" : "1008-27930-1097358"
      4. }
    3. Query the snapshot creation process. When the progress is 100, it indicates that the snapshot has been created.

      Request:

      https://ecs.aliyuncs.com/?Action=DescribeSnapshotAttribute&RegionId=cn-hangzhou-dg-a01&SnapshotId=1008-27930-1097358

      Response:

      1. {
      2. "RequestId" : "8307863A-1415-40EF-9520-8974871E651C",
      3. "SnapshotId" : "1008-27930-1097358",
      4. "SnapshotName" : "mytesthost1-snp-init",
      5. "Progress" : "100",
      6. "CreationTime" : "2013-05-19T03:19Z"
      7. }
  2. Create a custom image with the newly created snapshot.

    Request:

    https://ecs.aliyuncs.com/?Action=CreateImage&RegionId=cn-hangzhou-dg-a01&SnapshotId=1008-27930-1097358&Description=for creating test instances

    Response:

    1. {
    2. "RequestId" : "38C930E9-5CE9-4E24-A392-8538FC20D503",
    3. "ImageId" : "m8a1f80fe-ed9d-4156-a7a8-432f66305c36"
    4. }
  3. Clone the ECS instance.

    With the custom image, an ECS instance with the same configuration can be cloned and the second ECS instance will be created with this ImageID: ImageId=m8a1f80fe- ed9d-4156-a7a8-432f66305c36.

    In this example, the ECS instance configuration is as follows.

    1. {
    2. "RequestId" : "850ED7ED-A4D5-40A1-A7EF-C33B74B1296B",
    3. "InstanceId" : "i6b47cd72-843f-4558-b911-2776acae06fb",
    4. "ImageId" : "m8a1f80fe-ed9d-4156-a7a8-432f66305c36",
    5. "RegionId" : "cn-hangzhou-dg-a01",
    6. "ZoneId" : "cn-hangzhou-gy002-a",
    7. "InstanceType" : "ecs.t1.small",
    8. "HostName" : "mytesthost2",
    9. "Status" : "Stopped",
    10. "SecurityGroupIds" : {
    11. "SecurityGroupId" : [
    12. "g1f91e6e8-3c4b-4923-98dd-78aacbd09d17"
    13. ]
    14. },
    15. "PublicIpAddress" : {
    16. "IpAddress" : [
    17. "10.10.10.173"
    18. ]
    19. },
    20. "InnerIpAddress" : {
    21. "IpAddress" : [
    22. "10.32.148.152"
    23. ]
    24. },
    25. "InternetMaxBandwidthIn" : 2,
    26. "InternetMaxBandwidthOut" : 2,
    27. "SerialNumber" : "1fec6c01-7186-2c3e-fa10-a672b8c300ec"
    28. }
  4. To distinguish this new ECS instance, change the sample sentence in the Body of the file /usr/share/nginx/www/default/index.html. For example: Welcome to nginx on mytesthost2!.

Task 2 Create a Server Load Balancer instance

  1. 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" : "13ebb82ceaa-cn-hangzhou-dg-a01",
    4. "Address" : "10.10.10.77"
    5. }

    A Server Load Balancer with the ID 13ebb82ceaa-cn-hangzhou-dg-a01 is created. You can use the same method to create a Layer-4 instance as follows.

    https://slb.aliyuncs.com/?Action=CreateLoadBalancerHttpListener&LoadBalancerId=13ebb82ceaa-cn-hangzhou-dg-a01&ListenerPort=80&BackendServerPort=80&ListenerStatus=active

  2. Activate the Server Load Balancer instance.

    Request:

    https://slb.aliyuncs.com/?Action=SetLoadBalancerStatus&LoadBalancerId=13ebb82ceaa-cn-hangzhou-dg-a01&LoadBalancerStatus=active

Task 3 Add backend servers

  1. Add a backend server through the AddBackendServers interface.

    Request:

    https://slb.aliyuncs.com/?Action=AddBackendServers&LoadBalancerId=13ebb82ceaa-cn-hangzhou-dg-a01&BackendServers=[{"ServerId":"id5ab1760-3498-4d95-9687-a91545ef90b3"}]

    Response:

    1. {
    2. "RequestId" : "FA2F2172-63F2-409D-927C-86BD1D536F13",
    3. "LoadBalancerId" : "13ebb82ceaa-cn-hangzhou-dg-a01",
    4. "BackendServers" : {
    5. "BackendServer" : [
    6. {
    7. "ServerId" : "id5ab1760-3498-4d95-9687-a91545ef90b3",
    8. "Weight" : 100
    9. }
    10. ]
    11. }
    12. }
  2. Add another backend server.

    Request:

    https://slb.aliyuncs.com/?Action=AddBackendServers&LoadBalancerId=13ebb82ceaa-cn-hangzhou-dg-a01&BackendServers=[{"ServerId":"i6b47cd72-843f-4558-b911-2776acae06fb"}]

    Response:

    1. {
    2. "RequestId" : "C61FAD0A-2E87-4D0C-80B0-95AB758FCA70",
    3. "LoadBalancerId" : "13ebb82ceaa-cn-hangzhou-dg-a01",
    4. "BackendServers" : {
    5. "BackendServer" : [
    6. {
    7. "ServerId" : "id5ab1760-3498-4d95-9687-a91545ef90b3",
    8. "Weight" : 100
    9. },
    10. {
    11. "ServerId" : "i6b47cd72-843f-4558-b911-2776acae06fb",
    12. "Weight" : 100
    13. }
    14. ]
    15. }
    16. }
  3. View the configuration details of the Server Load Balancer instance.

    Request:

    https://slb.aliyuncs.com/?Action=DescribeLoadBalancerAttribute&LoadBalancerId=13ebb82ceaa-cn-hangzhou-dg-a01

    Response:

    1. {
    2. "RequestId" : "4747E9AE-ADFD-412D-B523-C1CBD45A2154",
    3. "LoadBalancerId" : "13ebb82ceaa-cn-hangzhou-dg-a01",
    4. "Address" : "10.10.10.77",
    5. "IsPublicAddress" : "true",
    6. "ListenerPorts" : {
    7. "ListenerPort" : [
    8. 80
    9. ]
    10. },
    11. "BackendServers" : {
    12. "BackendServer" : [
    13. {
    14. "ServerId" : "id5ab1760-3498-4d95-9687-a91545ef90b3",
    15. "Weight" : 100
    16. },
    17. {
    18. "ServerId" : "i6b47cd72-843f-4558-b911-2776acae06fb",
    19. "Weight" : 100
    20. }
    21. ]
    22. }
    23. }
Thank you! We've received your feedback.