全部产品

创建集群

更新时间:2018-11-18 12:15:22

接口

接口名称:create_cluster

参数

参数 类型 是否必须 描述
cluster_desc ClusterDescription 集群的描述信息
idempotent_token string 用于保证请求的幂等性。由用户生成该参数值,要保证在不同请求间唯一,最大不值过64个字符,字符包括了字母、数字以及下划线。

ClusterDescription 对象可通过 JSON 串初始化,具体参数请参阅 API 创建集群 描述。

返回值

成功返回 CreateResponse 对象,可以通过 response.Id 的方式获取新集群的标识符。失败抛出异常和错误,参考 SDK的使用说明 中的异常类型。

例子

下面是一个创建集群的例子,可执行的代码部分是一个简单的例子。注释掉的代码中是其他的参数的配置,您可以根据自己的需要打开相关的功能。关于各个参数具体配置信息请参考 API 创建集群 描述。

Python 源码:

  1. import time
  2. import random
  3. import string
  4. import batchcompute
  5. from batchcompute import CN_SHENZHEN as REGION
  6. from batchcompute import Client, ClientError
  7. from batchcompute.resources import (
  8. JobDescription, TaskDescription, DAG,
  9. GroupDescription, ClusterDescription,
  10. Configs, Networks, VPC, Classic, Mounts, Notification, Topic
  11. )
  12. ACCESS_KEY_ID = 'Your Access Key Id'
  13. ACCESS_KEY_SECRET = 'Your Access Key Secret'
  14. IMAGE_ID = 'img-ubuntu'
  15. INSTANCE_TYPE = 'ecs.sn2ne.large'
  16. client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
  17. def create_cluster(idempotent_token=''):
  18. try:
  19. # Cluster description.
  20. cluster_desc = ClusterDescription()
  21. cluster_desc.Name = "test-cluster"
  22. cluster_desc.Description = "demo"
  23. cluster_desc.ImageId = IMAGE_ID
  24. cluster_desc.InstanceType = INSTANCE_TYPE
  25. #cluster_desc.UserData = {'key1': 'value1', 'key2': 'value2'}
  26. #cluster_desc.Bootstrap = '' #your boot program
  27. #cluster_desc.EnvVars = {'key3': 'value3', 'key4':'value4'}
  28. #Group description
  29. group_desc1 = GroupDescription()
  30. group_desc1.DesiredVMCount = 4
  31. group_desc1.InstanceType = 'ecs.sn1ne.large' #user group special instance type
  32. group_desc1.ResourceType = 'OnDemand'
  33. #group_desc1.SpotStrategy = 'SpotAsPriceGo'
  34. #group_desc1.SpotPriceLimit = 0.1
  35. #group_desc2 = GroupDescription()
  36. #group_desc2.DesiredVMCount = 8
  37. #group_desc2.InstanceType = ''
  38. #group_desc2.ResourceType = 'OnDemand'
  39. cluster_desc.add_group('group1', group_desc1)
  40. #cluster_desc.add_group('group2', group_desc2)
  41. #Configs
  42. configs = Configs()
  43. #Configs.Disks
  44. configs.add_system_disk(50, 'cloud_efficiency')
  45. configs.add_data_disk(500, 'cloud_efficiency', '/home/my-data-disk')
  46. #Configs.Networks
  47. networks = Networks()
  48. vpc = VPC()
  49. vpc.CidrBlock = '192.168.0.0/16'
  50. #vpc.VpcId = 'vpc-xxxxx'
  51. networks.VPC = vpc
  52. #classic = Classic()
  53. #classic.AllowSecurityGroup = []
  54. #classic.AllowSecurityGroupEgress = []
  55. #classic.AllowIpAddress = []
  56. #classic.AllowIpAddressEgress = []
  57. #networks.Classic = classic
  58. configs.Networks = networks
  59. #Configs.Mounts
  60. #mounts = Mounts()
  61. #nas_entry = {'Source': 'nas://26a174a62f-sfi98.cn-shenzhen.nas.aliyuncs.com:/',
  62. # 'Destination': '/home/admin/mydir1/',
  63. # 'WriteSupport': True}
  64. #oss_entry = {'Source': 'oss://my-test-bk/',
  65. # 'Destination': '/home/admin/mydir3/',
  66. # 'WriteSupport': True}
  67. #mounts.Entries = [nas_entry, oss_entry]
  68. #mounts.Locale = 'GBK'
  69. #mounts.Lock = False
  70. #mounts.CacheSupport = True
  71. #mounts.CacheBlockSize = 262144
  72. #mounts.CacheTotalSize = 536870912
  73. #mounts.OSS.AccessKeyId = 'OSS Access Key Id'
  74. #mounts.OSS.AccessKeySecret = 'OSS Access Key Secret'
  75. #mounts.OSS.SecurityToken = ''
  76. #configs.Mounts = mounts
  77. cluster_desc.Configs = configs
  78. #Notification
  79. #notification = Notification()
  80. #topic = Topic()
  81. #topic.Name = 'testlow'
  82. #topic.Endpoint = 'http://48351.mns.cn-shenzhen.aliyuncs.com/'
  83. #topic.Events = ["OnClusterDeleted", "OnInstanceCreated", "OnInstanceActive"]
  84. #notification.Topic = topic
  85. #cluster_desc.Notification = notification
  86. print cluster_desc
  87. rsp = client.create_cluster(cluster_desc, idempotent_token)
  88. #rsp = client.create_cluster(cluster_desc)
  89. print rsp
  90. return True
  91. except ClientError, e:
  92. print (e.get_status_code(), e.get_code(), e.get_requestid(), e.get_msg())
  93. return False
  94. if __name__ == '__main__':
  95. #Not Use idempotent token
  96. create_cluster()
  97. #Use idempotent token
  98. #str_list = [random.choice(string.digits + string.ascii_letters) for i in range(32)]
  99. #random_str = ''.join(str_list)
  100. #print random_str
  101. #ret = False
  102. #loop = 0
  103. #while loop < 5 and ret == False:
  104. # ret = create_cluster(random_str)
  105. # loop += 1

执行结果:

  1. {
  2. "Date": "Thu, 08 Nov 2018 11:21:34 GMT",
  3. "Id": "cls-6kie8e833lnov8gg50k00c",
  4. "RequestId": "CE68B00C-4D3C-48EA-8EBD-1F48FFDA0C1D",
  5. "StatusCode": 201
  6. }

注意点

以下的各点都在 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 来创建集群。