接口
接口名稱: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 來創建集群。