文档首页> > 开发指南> Python> AS Python SDK示例

AS Python SDK示例

更新时间: 2018-11-26 14:47

创建伸缩组

伸缩组是具有相同应用场景配置的实例集合。伸缩组内定义了实例最大数量和最小数量等信息。

可以参考以下代码创建弹性伸缩组,其中子网(Network)、安全组(Security Group)、VPC为必选参数,在创建伸缩组之前必须先创建VPC,以及同一个VPC下的子网(Network)和安全组(Security Group)。

def test_creat_group(self):
_group = {
        "name": "as_NameTest_modify",
        "scaling_configuration_id": "33b55531-78f8-43c9-8cf5-ffec40bd0c6f",
        "desire_instance_number": 10,
        "min_instance_number": 2,
        "max_instance_number": 10,
        "cool_down_time": 200,
        "lb_listener_id": "114863c227a64255bd25157e0beb783c",
        "available_zones": ["eu-de-02"],
        "health_periodic_audit_method": "NOVA_AUDIT",
        "health_periodic_audit_time": "15",
        "instance_terminate_policy": "OLD_CONFIG_NEW_INSTANCE",
        "vpc_id": "2f2b426c-2072-47a7-babc-c35080fa79d4",
        "networks": [{                                  
          "id": "f80308f4-2608-4ae8-9489-c87720383ae5"
        }],
        "notifications": ["EMAIL"],
        "delete_publicip": "true",
        "security_groups": [{
          "id": "57f0a6cd-c427-4e40-a9a2-301ca90893fd" 
       }]
      }
      group = self.conn.auto_scaling.create_group(**_group)
      group = self.conn.auto_scaling.get_group(group)
      logging.info(group.id)

创建伸缩配置

伸缩配置定义了用于创建弹性伸缩组中实例的配置。AutoScaling为某个伸缩组自动添加实例时,会根据配置创建实例。

可以参考以下代码创建弹性伸缩组配置,当使用已存在的云服务器的规格为模板创建弹性伸缩配置时传入instance_id字段,此时flavorRef、imageRef、disk字段不生效。当不传入instance_id字段时flavorRef、imageRef、disk字段为必选。

def test_creat_config(self):
    instance_config = {
      "flavor_id": "normal1",
      "image_id": "ba391176-5e4c-4c06-8466-349f6b5fc91b",
      "disk": [{
          "size": 40,
          "volume_type": "SATA",
          "disk_type": "SYS"
       }],
      "metadata": {
          "key1": "value1",
          "tag": "app"
      },
      "key_name": "KeyPair-0406-as",
      "user_data": "wewfef46565",
      "public_ip": {
          "eip": {
              "ip_type": "5_bgp",
              "bandwidth": {
                  "size": 10,
                  "share_type": "PER",
                  "charging_mode": "traffic"
              }
          }
      }
      }
      config_name = "auto-scaling-config-name"
      config = self.conn.auto_scaling.create_config(config_name, **instance_config)
      config = self.conn.auto_scaling.get_config(config)
      logging.info(config.id)

创建伸缩策略

支持三种伸缩策略类型:定时,周期,告警 ,其中周期策略还分为Daily、Weekly和Monthly三种类型,如果选择告警策略,则选择或者创建的告警策略只能关联一个弹性伸缩组。

以下代码为创建每天的定时策略:

def test_creat_policy_Daily(self):
    as_group_id = "196ddd9c-e1f2-4088-b150-b67ae5ebf746"
    as_policy_name = "as-policy-name"
    _policy = {
          "name": as_policy_name,
          "scaling_policy_action": {
              "operation": "ADD",
              "instance_number": 1
          },
          "cool_down_time": 900,
          "scheduled_policy": {
              "launch_time": "16:00",
              "recurrence_type": "Daily",
              "recurrence_value": None,
              "start_time": "2017-07-14T03:34Z",
              "end_time": "2017-07-27T03:34Z"
          },
          "type": "RECURRENCE",
          "scaling_group_id": as_group_id
    }
    policy = self.conn.auto_scaling.create_policy(**_policy)
    policy = self.conn.auto_scaling.get_policy(policy)
    logging.info(policy)

创建生命周期挂钩

为伸缩组添加生命周期挂钩,目的是当伸缩组进行伸缩活动时,实例将被生命周期挂钩挂起并置于等待状态(正在加入伸缩组或正在移出伸缩组),实例将保持此状态直至超时时间结束或者用户手动回调。

代码参考:

 #test_create lifecycle_hook
 def test_create_life_cycle_hook(self):
    groupID = "58cbfcab-ebc6-4263-8b71-7d414810488d"
    groupID1 = "936634ad-1a4b-4929-b574-2bc2cbacb608"
    attrs = {
        "lifecycle_hook_name": "test-hook_c",
        "lifecycle_hook_type": "INSTANCE_TERMINATING",
        "default_result": "ABANDON",
        "default_timeout": "",
        "notification_topic_urn": "urn:smn:cn-suzhou2-1:ebac0c927c104c4587687ce375d0b656:as_test",
        "notification_metadata": "xxxxxxxx"
    }
    hook = self.conn.auto_scaling.create_lifecycle_hook(groupID,**attrs)

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

在文档使用中是否遇到以下问题







请至少选择或填写一项反馈信息

字符长度不能超过100

反馈内容不能为空!

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

跳转到云社区
点我,12·12惊喜等着你哦~