创建弹性伸缩策略
功能介绍
创建弹性伸缩策略。
调用方法
请参见如何调用API。
URI
POST /v2/{project_id}/autoscaling-policy/{cluster_id}
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
project_id |
是 |
String |
项目ID。 |
cluster_id |
是 |
String |
集群ID。 |
请求参数
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
node_group_name |
是 |
String |
节点组名称。必填参数。如果resource_pool_name为default,则创建节点组维度的弹性伸缩策略。如果resource_pool_name不为default,则在该节点组下创建对应资源池维度的策略。 |
resource_pool_name |
是 |
String |
资源池名称。必填参数。当集群版本不支持按指定资源池进行弹性伸缩时,需要填写为default资源池。不为default时删除指定资源池维度的弹性伸缩策略。 |
auto_scaling_policy |
否 |
AutoScalingPolicyInfo object |
弹性伸缩规则。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
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 |
弹性伸缩标签列表 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
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(周日) |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
name |
是 |
String |
弹性伸缩规则的名称。 只能由字母、数字、中划线和下划线组成,并且长度为1~64个字符。 在一个节点组范围内,不允许重名。 |
description |
否 |
String |
弹性伸缩规则的说明。 最大长度为1024字符。 |
adjustment_type |
是 |
String |
弹性伸缩规则的调整类型,只允许以下类型: 枚举值:
|
cool_down_minutes |
是 |
Integer |
触发弹性伸缩规则后,该集群处于冷却状态(不再执行弹性伸缩操作)的时长,单位为分钟。 取值范围[0~10080],10080为一周的分钟数。 |
scaling_adjustment |
是 |
Integer |
单次调整集群节点的个数。 取值范围[1~100] |
trigger |
是 |
Trigger object |
描述该规则触发条件。 |
响应参数
无
请求示例
创建一个弹性伸缩策略, 在节点组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 |
创建弹性伸缩策略完成 |
错误码
请参见错误码。