更新时间:2024-01-23 GMT+08:00
分享

创建弹性伸缩策略

功能介绍

创建弹性伸缩策略。

调用方法

请参见如何调用API

URI

POST /v2/{project_id}/autoscaling-policy/{cluster_id}

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID。

cluster_id

String

集群ID。

请求参数

表2 请求Body参数

参数

是否必选

参数类型

描述

node_group_name

String

节点组名称。必填参数。如果resource_pool_name为default,则创建节点组维度的弹性伸缩策略。如果resource_pool_name不为default,则在该节点组下创建对应资源池维度的策略。

resource_pool_name

String

资源池名称。必填参数。当集群版本不支持按指定资源池进行弹性伸缩时,需要填写为default资源池。不为default时删除指定资源池维度的弹性伸缩策略。

auto_scaling_policy

AutoScalingPolicyInfo object

弹性伸缩规则。

表3 AutoScalingPolicyInfo

参数

是否必选

参数类型

描述

auto_scaling_enable

Boolean

当前自动伸缩规则是否开启。

min_capacity

Integer

指定该节点组的最小保留节点数。 取值范围:[0~500]

max_capacity

Integer

指定该节点组的最大节点数。 取值范围:[0~500]

resources_plans

Array of ResourcesPlan objects

资源计划列表。若该参数为空表示不启用资源计划。

当启用弹性伸缩时,资源计划与自动伸缩规则需至少配置其中一种。

rules

Array of Rule objects

自动伸缩的规则列表。当启用弹性伸缩时,资源计划与自动伸缩规则需至少配置其中一种。

tags

Array of Tag objects

弹性伸缩标签列表

表4 ResourcesPlan

参数

是否必选

参数类型

描述

period_type

String

资源计划的周期类型,当前只允许以下类型:

daily

start_time

String

资源计划的起始时间,格式为“hour:minute”,表示时间在0:00-23:59之间。

end_time

String

资源计划的结束时间,格式与“start_time”相同,不早于start_time表示的时间,且与start_time间隔不小于30min。

min_capacity

Integer

资源计划内该节点组的最小保留节点数。 取值范围:[0~500]

max_capacity

Integer

资源计划内该节点组的最大保留节点数。 取值范围:[0~500]

effective_days

Array of strings

资源计划的生效日期,为空时代表每日,另外也可为以下返回值:

MONDAY(周一)、TUESDAY(周二)、WEDNESDAY(周三)、THURSDAY(周四)、FRIDAY(周五)、SATURDAY(周六)、SUNDAY(周日)

表5 Rule

参数

是否必选

参数类型

描述

name

String

弹性伸缩规则的名称。 只能由字母、数字、中划线和下划线组成,并且长度为1~64个字符。 在一个节点组范围内,不允许重名。

description

String

弹性伸缩规则的说明。 最大长度为1024字符。

adjustment_type

String

弹性伸缩规则的调整类型,只允许以下类型: 枚举值:

  • scale_out:扩容

  • scale_in:缩容

cool_down_minutes

Integer

触发弹性伸缩规则后,该集群处于冷却状态(不再执行弹性伸缩操作)的时长,单位为分钟。 取值范围[0~10080],10080为一周的分钟数。

scaling_adjustment

Integer

单次调整集群节点的个数。 取值范围[1~100]

trigger

Trigger object

描述该规则触发条件。

表6 Trigger

参数

是否必选

参数类型

描述

metric_name

String

指标名称。 该触发条件会依据该名称对应指标的值来进行判断。 最大长度为64个字符。

metric_value

String

指标阈值。

触发该条件的指标阈值,只允许输入整数或者带两位小数的数。

comparison_operator

String

指标判断逻辑运算符,包括:

  • LT:小于

  • GT:大于

  • LTOE:小于等于

  • GTOE:大于等于

evaluation_periods

Integer

判断连续满足指标阈值的周期数(一个周期为5分钟)。 取值范围[1~288]

表7 Tag

参数

是否必选

参数类型

描述

key

String

键。

  • 最大长度36个unicode字符,不能为空字符串。

  • 标签的key值不能包含非打印字符ASCII(0-31),“=”,“*”,“<”,“>”,“\”,“,”,“|”,“/”,且首尾字符不能为空格。

  • 同一资源的key值不能重复。

value

String

值。

  • 最大长度43个unicode字符,可以为空字符串。

  • 标签的value值不能包含非打印字符ASCII(0-31),“=”,“*”,“<”,“>”,“\”,“,”,“|”,“/”,且首尾字符不能为空格。

响应参数

请求示例

创建一个弹性伸缩策略, 在节点组node_group_1中配置节点组数量范围为0-5,并配置2个伸缩规则。 如果YARNAppRunning 大于等于 75 ,并持续 1 个五分钟,则添加1 个Task节点。如果 YARNAppRunning 小于等于25 ,并持续 1 个五分钟,则终止 1个Task节点。冷却时间皆为20分钟,并且设置了该策略下弹出的节点会打上aaa=bbb的标签。

/v2/{project_id}/autoscaling-policy/{cluster_id}

{
  "node_group_name" : "node_group_1",
  "auto_scaling_policy" : {
    "auto_scaling_enable" : true,
    "min_capacity" : 0,
    "max_capacity" : 5,
    "rules" : [ {
      "name" : "default-expand-1",
      "adjustment_type" : "scale_out",
      "cool_down_minutes" : 20,
      "scaling_adjustment" : 1,
      "trigger" : {
        "metric_name" : "YARNAppRunning",
        "metric_value" : "75",
        "comparison_operator" : "GT",
        "evaluation_periods" : 1
      }
    }, {
      "name" : "default-shrink-1",
      "adjustment_type" : "scale_in",
      "cool_down_minutes" : 20,
      "scaling_adjustment" : 1,
      "trigger" : {
        "metric_name" : "YARNAppRunning",
        "metric_value" : "25",
        "comparison_operator" : "LT",
        "evaluation_periods" : 1
      }
    } ],
    "resources_plans" : [ {
      "period_type" : "daily",
      "start_time" : "06:00",
      "end_time" : "20:00",
      "min_capacity" : "0",
      "max_capacity" : "2",
      "effective_days" : [ "MONDAY" ]
    } ],
    "tags" : [ {
      "key" : "aaa",
      "value" : "bbb"
    } ]
  },
  "resource_pool_name" : "default"
}

响应示例

SDK代码示例

SDK代码示例如下。

创建一个弹性伸缩策略, 在节点组node_group_1中配置节点组数量范围为0-5,并配置2个伸缩规则。 如果YARNAppRunning 大于等于 75 ,并持续 1 个五分钟,则添加1 个Task节点。如果 YARNAppRunning 小于等于25 ,并持续 1 个五分钟,则终止 1个Task节点。冷却时间皆为20分钟,并且设置了该策略下弹出的节点会打上aaa=bbb的标签。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.mrs.v2.region.MrsRegion;
import com.huaweicloud.sdk.mrs.v2.*;
import com.huaweicloud.sdk.mrs.v2.model.*;

import java.util.List;
import java.util.ArrayList;

public class CreateAutoScalingPolicySolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        MrsClient client = MrsClient.newBuilder()
                .withCredential(auth)
                .withRegion(MrsRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateAutoScalingPolicyRequest request = new CreateAutoScalingPolicyRequest();
        AutoScalingPolicyV2 body = new AutoScalingPolicyV2();
        List<Tag> listAutoScalingPolicyTags = new ArrayList<>();
        listAutoScalingPolicyTags.add(
            new Tag()
                .withKey("aaa")
                .withValue("bbb")
        );
        Trigger triggerRules = new Trigger();
        triggerRules.withMetricName("YARNAppRunning")
            .withMetricValue("25")
            .withComparisonOperator("LT")
            .withEvaluationPeriods(1);
        Trigger triggerRules1 = new Trigger();
        triggerRules1.withMetricName("YARNAppRunning")
            .withMetricValue("75")
            .withComparisonOperator("GT")
            .withEvaluationPeriods(1);
        List<Rule> listAutoScalingPolicyRules = new ArrayList<>();
        listAutoScalingPolicyRules.add(
            new Rule()
                .withName("default-expand-1")
                .withAdjustmentType(Rule.AdjustmentTypeEnum.fromValue("scale_out"))
                .withCoolDownMinutes(20)
                .withScalingAdjustment(1)
                .withTrigger(triggerRules1)
        );
        listAutoScalingPolicyRules.add(
            new Rule()
                .withName("default-shrink-1")
                .withAdjustmentType(Rule.AdjustmentTypeEnum.fromValue("scale_in"))
                .withCoolDownMinutes(20)
                .withScalingAdjustment(1)
                .withTrigger(triggerRules)
        );
        List<ResourcesPlan.EffectiveDaysEnum> listResourcesPlansEffectiveDays = new ArrayList<>();
        listResourcesPlansEffectiveDays.add(ResourcesPlan.EffectiveDaysEnum.fromValue("MONDAY"));
        List<ResourcesPlan> listAutoScalingPolicyResourcesPlans = new ArrayList<>();
        listAutoScalingPolicyResourcesPlans.add(
            new ResourcesPlan()
                .withPeriodType("daily")
                .withStartTime("06:00")
                .withEndTime("20:00")
                .withMinCapacity(0)
                .withMaxCapacity(2)
                .withEffectiveDays(listResourcesPlansEffectiveDays)
        );
        AutoScalingPolicyInfo autoScalingPolicybody = new AutoScalingPolicyInfo();
        autoScalingPolicybody.withAutoScalingEnable(true)
            .withMinCapacity(0)
            .withMaxCapacity(5)
            .withResourcesPlans(listAutoScalingPolicyResourcesPlans)
            .withRules(listAutoScalingPolicyRules)
            .withTags(listAutoScalingPolicyTags);
        body.withAutoScalingPolicy(autoScalingPolicybody);
        body.withResourcePoolName("default");
        body.withNodeGroupName("node_group_1");
        request.withBody(body);
        try {
            CreateAutoScalingPolicyResponse response = client.createAutoScalingPolicy(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

创建一个弹性伸缩策略, 在节点组node_group_1中配置节点组数量范围为0-5,并配置2个伸缩规则。 如果YARNAppRunning 大于等于 75 ,并持续 1 个五分钟,则添加1 个Task节点。如果 YARNAppRunning 小于等于25 ,并持续 1 个五分钟,则终止 1个Task节点。冷却时间皆为20分钟,并且设置了该策略下弹出的节点会打上aaa=bbb的标签。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkmrs.v2.region.mrs_region import MrsRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkmrs.v2 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.getenv("CLOUD_SDK_AK")
    sk = os.getenv("CLOUD_SDK_SK")

    credentials = BasicCredentials(ak, sk) \

    client = MrsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(MrsRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateAutoScalingPolicyRequest()
        listTagsAutoScalingPolicy = [
            Tag(
                key="aaa",
                value="bbb"
            )
        ]
        triggerRules = Trigger(
            metric_name="YARNAppRunning",
            metric_value="25",
            comparison_operator="LT",
            evaluation_periods=1
        )
        triggerRules1 = Trigger(
            metric_name="YARNAppRunning",
            metric_value="75",
            comparison_operator="GT",
            evaluation_periods=1
        )
        listRulesAutoScalingPolicy = [
            Rule(
                name="default-expand-1",
                adjustment_type="scale_out",
                cool_down_minutes=20,
                scaling_adjustment=1,
                trigger=triggerRules1
            ),
            Rule(
                name="default-shrink-1",
                adjustment_type="scale_in",
                cool_down_minutes=20,
                scaling_adjustment=1,
                trigger=triggerRules
            )
        ]
        listEffectiveDaysResourcesPlans = [
            "MONDAY"
        ]
        listResourcesPlansAutoScalingPolicy = [
            ResourcesPlan(
                period_type="daily",
                start_time="06:00",
                end_time="20:00",
                min_capacity=0,
                max_capacity=2,
                effective_days=listEffectiveDaysResourcesPlans
            )
        ]
        autoScalingPolicybody = AutoScalingPolicyInfo(
            auto_scaling_enable=True,
            min_capacity=0,
            max_capacity=5,
            resources_plans=listResourcesPlansAutoScalingPolicy,
            rules=listRulesAutoScalingPolicy,
            tags=listTagsAutoScalingPolicy
        )
        request.body = AutoScalingPolicyV2(
            auto_scaling_policy=autoScalingPolicybody,
            resource_pool_name="default",
            node_group_name="node_group_1"
        )
        response = client.create_auto_scaling_policy(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

创建一个弹性伸缩策略, 在节点组node_group_1中配置节点组数量范围为0-5,并配置2个伸缩规则。 如果YARNAppRunning 大于等于 75 ,并持续 1 个五分钟,则添加1 个Task节点。如果 YARNAppRunning 小于等于25 ,并持续 1 个五分钟,则终止 1个Task节点。冷却时间皆为20分钟,并且设置了该策略下弹出的节点会打上aaa=bbb的标签。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    mrs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mrs/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mrs/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mrs/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := mrs.NewMrsClient(
        mrs.MrsClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateAutoScalingPolicyRequest{}
	var listTagsAutoScalingPolicy = []model.Tag{
        {
            Key: "aaa",
            Value: "bbb",
        },
    }
	comparisonOperatorTrigger:= "LT"
	triggerRules := &model.Trigger{
		MetricName: "YARNAppRunning",
		MetricValue: "25",
		ComparisonOperator: &comparisonOperatorTrigger,
		EvaluationPeriods: int32(1),
	}
	comparisonOperatorTrigger1:= "GT"
	triggerRules1 := &model.Trigger{
		MetricName: "YARNAppRunning",
		MetricValue: "75",
		ComparisonOperator: &comparisonOperatorTrigger1,
		EvaluationPeriods: int32(1),
	}
	var listRulesAutoScalingPolicy = []model.Rule{
        {
            Name: "default-expand-1",
            AdjustmentType: model.GetRuleAdjustmentTypeEnum().SCALE_OUT,
            CoolDownMinutes: int32(20),
            ScalingAdjustment: int32(1),
            Trigger: triggerRules1,
        },
        {
            Name: "default-shrink-1",
            AdjustmentType: model.GetRuleAdjustmentTypeEnum().SCALE_IN,
            CoolDownMinutes: int32(20),
            ScalingAdjustment: int32(1),
            Trigger: triggerRules,
        },
    }
	var listEffectiveDaysResourcesPlans = []model.ResourcesPlanEffectiveDays{
        model.GetResourcesPlanEffectiveDaysEnum().MONDAY,
    }
	var listResourcesPlansAutoScalingPolicy = []model.ResourcesPlan{
        {
            PeriodType: "daily",
            StartTime: "06:00",
            EndTime: "20:00",
            MinCapacity: int32(0),
            MaxCapacity: int32(2),
            EffectiveDays: &listEffectiveDaysResourcesPlans,
        },
    }
	autoScalingPolicybody := &model.AutoScalingPolicyInfo{
		AutoScalingEnable: true,
		MinCapacity: int32(0),
		MaxCapacity: int32(5),
		ResourcesPlans: &listResourcesPlansAutoScalingPolicy,
		Rules: &listRulesAutoScalingPolicy,
		Tags: &listTagsAutoScalingPolicy,
	}
	request.Body = &model.AutoScalingPolicyV2{
		AutoScalingPolicy: autoScalingPolicybody,
		ResourcePoolName: "default",
		NodeGroupName: "node_group_1",
	}
	response, err := client.CreateAutoScalingPolicy(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

202

创建弹性伸缩策略完成

错误码

请参见错误码

分享:

    相关文档

    相关产品