接口
接口名称:create_cluster
参数
参数 | 类型 | 是否必须 | 描述 |
cluster_desc | ClusterDescription | 是 | 集群的描述信息 |
idempotent_token | string | 否 | 用于保证请求的幂等性。由用户生成该参数值,要保证在不同请求间唯一,最大不值过64个字符,字符包括了字母、数字以及下划线。 |
ClusterDescription 对象可通过 JSON 串初始化,具体参数请参阅 API 创建集群描述。
返回值
成功返回 CreateResponse 对象,可以通过 response.Id
的方式获取新集群的标识符。失败抛出异常和错误,参考 SDK的使用说明中的异常类型。
例子
下面是一个创建集群的例子,可执行的代码部分是一个简单的例子。注释掉的代码中是其他的参数的配置,您可以根据自己的需要打开相关的功能。关于各个参数具体配置信息请参考 API 创建集群描述。
Python 源码:
import time
import random
import string
import batchcompute
from batchcompute import CN_SHENZHEN as REGION
from batchcompute import Client, ClientError
from batchcompute.resources import (
JobDescription, TaskDescription, DAG,
GroupDescription, ClusterDescription,
Configs, Networks, VPC, Classic, Mounts, Notification, Topic
)
ACCESS_KEY_ID = 'Your Access Key Id'
ACCESS_KEY_SECRET = 'Your Access Key Secret'
IMAGE_ID = 'img-ubuntu'
INSTANCE_TYPE = 'ecs.sn2ne.large'
client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
def create_cluster(idempotent_token=''):
try:
# Cluster description.
cluster_desc = ClusterDescription()
cluster_desc.Name = "test-cluster"
cluster_desc.Description = "demo"
cluster_desc.ImageId = IMAGE_ID
cluster_desc.InstanceType = INSTANCE_TYPE
#cluster_desc.UserData = {'key1': 'value1', 'key2': 'value2'}
#cluster_desc.Bootstrap = '' #your boot program
#cluster_desc.EnvVars = {'key3': 'value3', 'key4':'value4'}
#Group description
group_desc1 = GroupDescription()
group_desc1.DesiredVMCount = 4
group_desc1.InstanceType = 'ecs.sn1ne.large' #user group special instance type
group_desc1.ResourceType = 'OnDemand'
#group_desc1.SpotStrategy = 'SpotAsPriceGo'
#group_desc1.SpotPriceLimit = 0.1
#group_desc2 = GroupDescription()
#group_desc2.DesiredVMCount = 8
#group_desc2.InstanceType = ''
#group_desc2.ResourceType = 'OnDemand'
cluster_desc.add_group('group1', group_desc1)
#cluster_desc.add_group('group2', group_desc2)
#Configs
configs = Configs()
#Configs.Disks
configs.add_system_disk(50, 'cloud_efficiency')
configs.add_data_disk(500, 'cloud_efficiency', '/home/my-data-disk')
#Configs.Networks
networks = Networks()
vpc = VPC()
vpc.CidrBlock = '192.168.0.0/16'
#vpc.VpcId = 'vpc-xxxxx'
networks.VPC = vpc
#classic = Classic()
#classic.AllowSecurityGroup = []
#classic.AllowSecurityGroupEgress = []
#classic.AllowIpAddress = []
#classic.AllowIpAddressEgress = []
#networks.Classic = classic
configs.Networks = networks
#Configs.Mounts
#mounts = Mounts()
#nas_entry = {'Source': 'nas://26a174a62f-sfi98.cn-shenzhen.nas.aliyuncs.com:/',
# 'Destination': '/home/admin/mydir1/',
# 'WriteSupport': True}
#oss_entry = {'Source': 'oss://my-test-bk/',
# 'Destination': '/home/admin/mydir3/',
# 'WriteSupport': True}
#mounts.Entries = [nas_entry, oss_entry]
#mounts.Locale = 'GBK'
#mounts.Lock = False
#mounts.CacheSupport = True
#mounts.CacheBlockSize = 262144
#mounts.CacheTotalSize = 536870912
#mounts.OSS.AccessKeyId = 'OSS Access Key Id'
#mounts.OSS.AccessKeySecret = 'OSS Access Key Secret'
#mounts.OSS.SecurityToken = ''
#configs.Mounts = mounts
cluster_desc.Configs = configs
#Notification
#notification = Notification()
#topic = Topic()
#topic.Name = 'testlow'
#topic.Endpoint = 'http://48351.mns.cn-shenzhen.aliyuncs.com/'
#topic.Events = ["OnClusterDeleted", "OnInstanceCreated", "OnInstanceActive"]
#notification.Topic = topic
#cluster_desc.Notification = notification
print cluster_desc
rsp = client.create_cluster(cluster_desc, idempotent_token)
#rsp = client.create_cluster(cluster_desc)
print rsp
return True
except ClientError, e:
print (e.get_status_code(), e.get_code(), e.get_requestid(), e.get_msg())
return False
if __name__ == '__main__':
#Not Use idempotent token
create_cluster()
#Use idempotent token
#str_list = [random.choice(string.digits + string.ascii_letters) for i in range(32)]
#random_str = ''.join(str_list)
#print random_str
#ret = False
#loop = 0
#while loop < 5 and ret == False:
# ret = create_cluster(random_str)
# loop += 1
执行结果:
{
"Date": "Thu, 08 Nov 2018 11:21:34 GMT",
"Id": "cls-6kie8e833lnov8gg50k00c",
"RequestId": "CE68B00C-4D3C-48EA-8EBD-1F48FFDA0C1D",
"StatusCode": 201
}
注意点
以下的各点都在 API 文档中有相关的详细说明。
1、集群中默认只支持1个组,受配额限制。
2、如果 Group 中有配置 ResourceType,优先使用 Group 中的 ResourceType
3、实例的实例类型与磁盘类型有一定的对应关系,请参考ECS的实例文档。
4、建议您使用 VPC 网络。您可以指定VpcId,如果不指定我们会自动给您创建。注意CidrBlock的格式。
5、Mount 的路径请注意格式。如果挂在NAS,必须使用VPC,并且VPC配置中的VpcId必须是跟NAS在同一个VpcId下。
6、挂载 OSS 文件的访问权限可以通过单独配置 Access Key Id / Access Key Secrect。
7、如果您需要关心集群实时状态,可以通过 Notification 来获取。
8、如果需要保证请求的幂等性,可以通过生成 Idempotent Token 来创建集群。