文档首页/ 弹性伸缩 AS/ API参考/ 应用示例/ 示例4:创建伸缩策略
更新时间:2022-10-08 GMT+08:00

示例4:创建伸缩策略

场景描述

本章节指导用户通过API创建伸缩策略。API的调用方法请参见如何调用API

伸缩策略是规定触发伸缩活动的条件及伸缩活动执行的动作的一种策略。当策略满足触发条件时,伸缩组会发生一次伸缩活动。

涉及接口

本示例场景涉及如下接口调用:

操作步骤

  1. 确定调用API鉴权时使用的token。

    1. 查询token,详情请参见获取用户Token
    2. 获取响应Header参数中X-Subject-Token对应的值。

  1. 确定要创建伸缩策略的伸缩组。

    1. 查询伸缩组列表。
      • 接口相关信息

        URI格式:GET /autoscaling-api/v1/{project_id}/scaling_group

        详情请参见查询弹性伸缩组列表

      • 请求示例

        GET: https://{Endpoint}/autoscaling-api/v1/0605767c2e80d5762fd0c0146a10aaf2/scaling_group

        {endpoint}信息请从地区和终端节点获取。

      • 响应示例
        {
          "limit": 20,
          "scaling_groups": [
              {
                  "networks": [
                      {
                          "id": "a8327883-6b07-4497-9c61-68d03ee193a",
                          "ipv6_enable": true,
                          "ipv6_bandwidth": 
                          {
                              "id": "076ee2ff-f23e-4338-b8ac-1bc7278532d5"
                          }
                      }
                  ],
                  "available_zones": [
                      "XXXa",
                      "XXXb"
                  ],
                  "detail": null,
                  "scaling_group_name": "as-group-test",
                  "scaling_group_id": "77a7a397-7d2f-4e79-9da9-6a35e2709150",
                  "scaling_group_status": "INSERVICE",
                  "scaling_configuration_id": "1d281494-6085-4579-b817-c1f813be835f",
                  "scaling_configuration_name": "healthCheck",
                  "current_instance_number": 0,
                  "desire_instance_number": 1,
                  "min_instance_number": 0,
                  "max_instance_number": 500,
                  "cool_down_time": 300,
                  "lb_listener_id": "f06c0112570743b51c0e8fbe1f235bab",
                  "security_groups": [
                      {
                          "id": "8a4b1d5b-0054-419f-84b1-5c8a59ebc829"
                      }
                  ],
                  "create_time": "2015-07-23T02:46:29Z",
                  "vpc_id": "863ccae2-ee85-4d27-bc5b-3ba2a198a9e2",
                  "health_periodic_audit_method": "ELB_AUDIT",
                  "health_periodic_audit_time": 5,
                  "health_periodic_audit_grace_period": 600,
                  "instance_terminate_policy": "OLD_CONFIG_OLD_INSTANCE",
                  "is_scaling": false,
                  "delete_publicip": false,
                  "enterprise_project_id": "c92b1a5d-6f20-43f2-b1b7-7ce35e58e413",
                  "multi_az_priority_policy": "PICK_FIRST"
              }
        ],
        "total_number": 1,
        "start_number": 0
        }
    2. 选择要创建伸缩策略的伸缩组,并记录伸缩组ID。

  1. 为指定的伸缩组创建伸缩策略。

    • 接口相关信息

      URI格式:POST /autoscaling-api/v1/{project_id}/scaling_policy

      详情请参见创建弹性伸缩策略

    • 请求示例

      示例为创建一个名称为as-policy-7a75的周期策略,该策略的生效时间为2015-12-14T03:34Z到2015-12-27T03:34Z,每天16:00在ID为5bc3aa02-b83e-454c-aba1-4d2095c68f8b的伸缩组中增加一个实例。

      POST: https://{Endpoint}/autoscaling-api/v1/0605767c2e80d5762fd0c0146a10aaf2/scaling_policy

      {endpoint}信息请从地区和终端节点获取。

      Body:

      {
          "scaling_policy_name": "as-policy-7a75",
          "scaling_policy_action": {
              "operation": "ADD",
              "instance_number": 1
          },
          "cool_down_time": 900,
          "scheduled_policy": {
              "launch_time": "16:00",
              "recurrence_type": "Daily",
              "start_time": "2015-12-14T03:34Z",
              "end_time": "2015-12-27T03:34Z"
          },
          "scaling_policy_type": "RECURRENCE",
          "scaling_group_id": "5bc3aa02-b83e-454c-aba1-4d2095c68f8b"
      }

    • 响应示例
      {
          "scaling_policy_id": "0h327883-324n-4dzd-9c61-68d03ee191dd"
      }

  2. 确认伸缩策略创建成功。

    • 接口相关信息

      URI格式:GET /autoscaling-api/v1/{project_id}/scaling_policy/{scaling_policy_id}

      详情请参见查询弹性伸缩策略详情

    • 请求示例

      本示例展示了查询ID为0h327883-324n-4dzd-9c61-68d03ee191dd的伸缩策略的详情信息。

      GET: https://{Endpoint}/autoscaling-api/v1/0605767c2e80d5762fd0c0146a10aaf2/scaling_policy/fd7d63ce-8f5c-443e-b9a0-bef9386b23b3

      {endpoint}信息请从地区和终端节点获取。

    • 响应示例
      {
          "scaling_policy": {
              "scaling_policy_id": "fd7d63ce-8f5c-443e-b9a0-bef9386b23b3",
              "scaling_group_id": "e5d27f5c-dd76-4a61-b4bc-a67c5686719a",
              "scaling_policy_name": "定时1",
              "scaling_policy_type": "SCHEDULED",
              "scheduled_policy": {
                  "launch_time": "2015-07-24T01:21Z"
              },
              "cool_down_time": 300,
              "scaling_policy_action": {
                  "operation": "REMOVE",
                  "instance_number": 1
              },
              "policy_status": "INSERVICE",
              "create_time": "2015-07-24T01:09:30Z"
          }
      }