kce.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # -*- encoding:utf-8 -*-
  2. from kscore.session import get_session
  3. import json
  4. if __name__ == "__main__":
  5. s = get_session()
  6. # 创建集群
  7. client = s.create_client("kce", "cn-beijing-6", use_ssl=True)
  8. client2 = s.create_client("kcev2", "cn-beijing-6", use_ssl=True)
  9. '''
  10. #创建集群(老版2019-08-06不推荐)
  11. param = {
  12. "ClusterName": "xxxxx",
  13. "ClusterManageMode": "DedicatedCluster",
  14. "VpcId": "15552848-026b-4ad6-a3e3-xxxxx",
  15. "PodCidr": "10.32.0.0/14",
  16. "ServiceCidr": "10.254.0.0/16",
  17. "NetworkType": "Flannel",
  18. "K8sVersion": "v1.13.4",
  19. "ReserveSubnetId": "9729f4c0-93ee-4e2a-9b2a-xxxxx",
  20. "PublicApiServer": "{\"LineId\":\"5fc2595f-1bfd-481b-bf64-2d08f116d800\",\"BandWidth\": \"10\",\"ChargeType\": \"PostPaidByDay\"}",
  21. "InstanceSet.0.NodeRole": "Master_Etcd",
  22. "InstanceSet.0.NodePara.0": "{\"MaxCount\":3,\"MinCount\":3,\"ImageId\":\"a0699172-76c6-4885-a4e9-0799a9cb811d\",\"SubnetId\":\"4a077fa8-a239-47bf-a18f-xxxxx\",\"InstancePassword\":\"Root23123\",\"SecurityGroupId\":\"0dcd8356-9e7e-43ae-897b-xxxxx\",\"DataDiskGb\":0,\"ChargeType\":\"Daily\",\"InstanceType\":\"I3.2A\",\"PurchaseTime\":0,\"InstanceName\":\"kce-py\",\"InstanceNameSuffix\":1}",
  23. "InstanceSet.1.NodeRole": "Worker",
  24. "InstanceSet.1.NodePara.0": "{\"MaxCount\":1,\"MinCount\":1,\"ImageId\":\"a0699172-76c6-4885-a4e9-0799a9cb811d\",\"SubnetId\":\"4a077fa8-a239-47bf-a18f-xxxxx\",\"InstancePassword\":\"Root23123\",\"SecurityGroupId\":\"0dcd8356-9e7e-43ae-897b-xxxxx\",\"DataDiskGb\":0,\"ChargeType\":\"Daily\",\"InstanceType\":\"I3.2A\",\"PurchaseTime\":0,\"InstanceName\":\"kce-py\",\"InstanceNameSuffix\":1}",
  25. }
  26. m = client.create_cluster(**param)
  27. print json.dumps(m, sort_keys=True, indent=4)
  28. '''
  29. #创建集群(新版2020-12-31推荐)
  30. param = {
  31. "ClusterName": "xxxxx",
  32. "ClusterManageMode": "DedicatedCluster",
  33. "VpcId": "15552848-026b-4ad6-a3e3-xxxxx",
  34. "PodCidr": "10.32.0.0/14",
  35. "ServiceCidr": "10.254.0.0/16",
  36. "NetworkType": "Flannel",
  37. "K8sVersion": "v1.19.3",
  38. "ReserveSubnetId": "9729f4c0-93ee-4e2a-9b2a-xxxxx",
  39. "PublicApiServer": "{\"LineId\":\"5fc2595f-1bfd-481b-bf64-2d08f116d800\",\"BandWidth\": \"10\",\"ChargeType\": \"PostPaidByDay\"}",
  40. "InstanceForNode.1.NodeRole": "Master_Etcd",
  41. "InstanceForNode.1.NodeConfig.1.Para": "{\"MaxCount\":3,\"MinCount\":3,\"ImageId\":\"a0699172-76c6-4885-a4e9-0799a9cb811d\",\"SubnetId\":\"4a077fa8-a239-47bf-a18f-xxxxx\",\"InstancePassword\":\"Root23123\",\"SecurityGroupId\":\"0dcd8356-9e7e-43ae-897b-xxxxx\",\"DataDiskGb\":0,\"ChargeType\":\"Daily\",\"InstanceType\":\"I3.2A\",\"PurchaseTime\":0,\"InstanceName\":\"kce-py\",\"InstanceNameSuffix\":1}",
  42. "InstanceForNode.1.NodeConfig.1.AdvancedSetting.DockerPath": "/data1/docker",
  43. "InstanceForNode.1.NodeConfig.1.AdvancedSetting.DataDisk.FileSystem": "ext3",
  44. "InstanceForNode.1.NodeConfig.1.AdvancedSetting.DataDisk.MountTarget": "/data1",
  45. "InstanceForNode.2.NodeRole": "Worker",
  46. "InstanceForNode.2.NodeConfig.1.Para": "{\"MaxCount\":1,\"MinCount\":1,\"ImageId\":\"a0699172-76c6-4885-a4e9-0799a9cb811d\",\"SubnetId\":\"4a077fa8-a239-47bf-a18f-xxxxx\",\"InstancePassword\":\"Root23123\",\"SecurityGroupId\":\"0dcd8356-9e7e-43ae-897b-xxxxx\",\"DataDiskGb\":0,\"ChargeType\":\"Daily\",\"InstanceType\":\"I3.2A\",\"PurchaseTime\":0,\"InstanceName\":\"kce-py\",\"InstanceNameSuffix\":1}",
  47. "InstanceForNode.2.NodeConfig.1.AdvancedSetting.DockerPath": "/data1/docker",
  48. "InstanceForNode.2.NodeConfig.1.AdvancedSetting.Label.1.Key": "label",
  49. "InstanceForNode.2.NodeConfig.1.AdvancedSetting.Label.1.Value": "worker",
  50. "InstanceForNode.2.NodeConfig.1.AdvancedSetting.DataDisk.FileSystem": "ext4",
  51. "InstanceForNode.2.NodeConfig.1.AdvancedSetting.DataDisk.MountTarget": "/data1",
  52. }
  53. m = client2.create_cluster(**param)
  54. print json.dumps(m, sort_keys=True, indent=4)
  55. '''
  56. # 查询集群列表
  57. m = client.describe_cluster()
  58. print json.dumps(m, sort_keys=True, indent=4)
  59. # 查询集群节点列表
  60. m = client.describe_cluster_instance(ClusterId="a77b437f-07c9-4ae7-ac8d-xxxxx")
  61. print json.dumps(m, sort_keys=True, indent=4)
  62. '''
  63. '''
  64. # 新增节点
  65. param = {
  66. "ClusterId": "a77b437f-07c9-4ae7-ac8d-xxxxx",
  67. "InstanceSet.0.NodeRole": "Worker",
  68. "InstanceSet.0.NodePara.0": "{\"MaxCount\":1,\"MinCount\":1,\"ImageId\":\"a0699172-76c6-4885-a4e9-0799a9cb811d\",\"SubnetId\":\"4a077fa8-a239-47bf-a18f-xxxxx\",\"InstancePassword\":\"Root23123\",\"SecurityGroupId\":\"0dcd8356-9e7e-43ae-897b-xxxxx\",\"DataDiskGb\":0,\"ChargeType\":\"Daily\",\"InstanceType\":\"I3.2A\",\"PurchaseTime\":0,\"InstanceName\":\"kce-py\",\"InstanceNameSuffix\":1}",
  69. }
  70. m = client.add_cluster_instances(**param)
  71. print json.dumps(m, sort_keys=True, indent=4)
  72. '''
  73. '''
  74. # 删除集群中的节点
  75. param = {
  76. "ClusterId": "a77b437f-07c9-4ae7-ac8d-xxxxx",
  77. "InstanceId.1": "0253d503-485e-4adc-8859-xxxxx",
  78. }
  79. m = client.delete_cluster_instances(**param)
  80. print json.dumps(m, sort_keys=True, indent=4)
  81. # 强制移除节点,该接口必须在delete_cluster_instances后执行
  82. param = {
  83. "ClusterId": "a77b437f-07c9-4ae7-ac8d-xxxxx",
  84. "InstanceId.1": "0253d503-485e-4adc-8859-xxxxx",
  85. "InstanceId.2": "0253d503-485e-4adc-8860-xxxxx",
  86. }
  87. m = client.force_remove_cluster_instance(**param)
  88. print json.dumps(m, sort_keys=True, indent=4)
  89. # 修改集群基本信息
  90. m = client.modify_cluster_info(ClusterId="6f7f35d4-5c91-42f2-bab8-xxxxx", ClusterName="xxxxx",
  91. ClusterDesc="xxxxx")
  92. print json.dumps(m, sort_keys=True, indent=4)
  93. '''
  94. # 下载集群配置文件
  95. '''
  96. m = client.download_cluster_config(ClusterId="6f7f35d4-5c91-42f2-bab8-xxxxx")
  97. print json.dumps(m, sort_keys=True, indent=4)
  98. ''' \
  99. \
  100. '''
  101. # 删除集群
  102. m = client.delete_cluster(ClusterId="715d3352-e401-4994-bd2b-xxxxx")
  103. print json.dumps(m, sort_keys=True, indent=4)
  104. '''
  105. # 获取容器服务支持的节点操作系统
  106. '''
  107. param = {}
  108. m = client.describe_instance_image(**param)
  109. print json.dumps(m, sort_keys=True, indent=4)
  110. ''' \
  111. \
  112. '''
  113. # 获取支持移入物理机集群的epc列表
  114. m = client.describe_epc_for_cluster(ClusterId="a77b437f-07c9-4ae7-ac8d-xxxxx")
  115. print json.dumps(m, sort_keys=True, indent=4)
  116. ''' \
  117. \
  118. '''
  119. # 移入物理机服务器至物理机集群
  120. param = {
  121. "ClusterId": "a77b437f-07c9-4ae7-ac8d-xxxxx",
  122. "InstanceId.1": "0253d503-485e-4adc-8859-xxxxx",
  123. "InstanceId.2": "0253d503-485e-4adc-8860-xxxxx",
  124. "AdvancedSetting.ExtraArg.Kubelet.1.CustomArg": "--feature-gates=EphemeralContainers=true",
  125. "AdvancedSetting.ExtraArg.Kubelet.2.CustomArg": "--read-only-port=0",
  126. "AdvancedSetting.ExtraArg.Kubelet.3.CustomArg": "--cluster-dns=127.0.0.1"
  127. }
  128. m = client.add_cluster_epc_instances(**param)
  129. print json.dumps(m, sort_keys=True, indent=4)
  130. '''
  131. #查询已经存在的云服务器
  132. '''
  133. param = {
  134. "ClusterId": "84d89f76-xxxx-47a2-b37e-xxxxx",
  135. "InstanceId.1": "8d1cae6a-xxxx-47f6-8fe6-xxxxx"
  136. }
  137. m = client.describe_existed_instances(**param)
  138. print json.dumps(m, sort_keys=True, indent=4)
  139. #添加已有的服务器
  140. param = {
  141. "ClusterId": "84d89f76-xxxx-47a2-b37e-xxxxx",
  142. "ExistedInstanceKecSet.1.NodeRole": "worker",
  143. "ExistedInstanceKecSet.1.KecPara.1": "{\"InstanceId\":\"8d1cae6a-23c3-47f6-8fe6-xxxxx\",\"ImageId\":\"81cc01c3-4d64-40fa-89af-xxxxx\",\"InstancePassword\":\"xxxxx\"}"
  144. }
  145. m = client.add_existed_instances(**param)
  146. print json.dumps(m, sort_keys=True, indent=4)
  147. '''
  148. #获取裸金属服务器支持的节点操作系统
  149. param = {
  150. # "ImageId": "84d89f76-xxxx-47a2-b37e-xxxxx",
  151. }
  152. m = client.describe_epc_image(**param)
  153. print json.dumps(m, sort_keys=True, indent=4)
  154. '''
  155. #创建节点池
  156. param = {
  157. "NodePoolName": "xuan-create",
  158. "ClusterId": "d9a0adf0-a8f3-xxx-xxxxx",
  159. "EnableAutoScale": True,
  160. "MinSize": 0,
  161. "DesiredCapacity": 0,
  162. "MaxSize": 2,
  163. "Label.0.Key": "labelk",
  164. "Label.0.Value": "labelv",
  165. "Taint.0.Key": "taintk",
  166. "Taint.0.Value": "taintv",
  167. "Taint.0.Effect": "NoSchedule",
  168. "NodeTemplate.ImageId": "13107fc5-0dd8-xxx-xxxxx",
  169. # "NodeTemplate.KeyId.1":"c409d431-xxx-xxx-9834-xxxxx",
  170. "NodeTemplate.DataDiskGb": 0,
  171. "NodeTemplate.SubnetId.1": "2c83f2d6-8160-xxx-xxxxx",
  172. "NodeTemplate.InstanceType": "E1.4B",
  173. "NodeTemplate.SecurityGroupId": "8728f772-6186-xxx-xxxxx",
  174. "NodeTemplate.ChargeType": "HourlyInstantSettlement",
  175. "NodeTemplate.SystemDisk.DiskType": "Local_SSD",
  176. "NodeTemplate.SubnetStrategy": "balanced-distribution",
  177. "NodeTemplate.AdvancedSetting.DockerPath": "/data/docker",
  178. "NodeTemplate.AdvancedSetting.Schedulable": True,
  179. "NodeTemplate.AdvancedSetting.UserScript": "ZWNobyAicG9zIiA+cG9z",
  180. "NodeTemplate.AdvancedSetting.PreUserScript": "ZWNobyAicHJlIiA+cHJl",
  181. "NodeTemplate.AdvancedSetting.ContainerLogMaxSize": 100,
  182. "NodeTemplate.AdvancedSetting.ContainerLogMaxFiles": 10,
  183. "NodeTemplate.SystemDisk.DiskSize": 20,
  184. "NodeTemplate.ProjectId": 0,
  185. "NodeTemplate.Password": "Xuanxxx",
  186. "NodeTemplate.DataDisk.1.Type": "EHDD",
  187. "NodeTemplate.DataDisk.1.Size": 30
  188. }
  189. m = client.create_node_pool(**param)
  190. print json.dumps(m, sort_keys=True, indent=4)
  191. #查询节点池
  192. param = {
  193. "ClusterId": "d9a0adf0-a8f3-xxx-xxxxx",
  194. "NodePoolId.1": "64xxxxxxxx"
  195. }
  196. m = client.describe_node_pool(**param)
  197. print json.dumps(m, sort_keys=True, indent=4)
  198. #修改节点池
  199. param = {
  200. "NodePoolId": "64xxxxxxxxx",
  201. "ClusterId": "d9a0adf0-a8f3-xxx-xxxxx",
  202. "NodePoolName": "xuan-update",
  203. "EnableAutoScale": False,
  204. "MinSize": 1,
  205. "DesiredCapacity": 0,
  206. "MaxSize": 3,
  207. "Label.0.Key": "labelkupdate",
  208. "Label.0.Value": "labelvupdate",
  209. "Taint.0.Key": "taintkupdate",
  210. "Taint.0.Value": "taintvupdate",
  211. "Taint.0.Effect": "NoSchedule",
  212. "UpdateExistingNodes": True
  213. }
  214. m = client.modify_node_pool(**param)
  215. print json.dumps(m, sort_keys=True, indent=4)
  216. #删除节点池
  217. param = {
  218. "ClusterId": "d9a0adf0-a8f3-xxx-xxxxx",
  219. "NodePoolId.1": "64xxxxxxxxx"
  220. }
  221. m = client.delete_node_pool(**param)
  222. print json.dumps(m, sort_keys=True, indent=4)
  223. # 修改节点池模板
  224. param = {
  225. "ClusterId": "d9a0adf0-a8f3-xxx-xxxxx",
  226. "NodePoolId": "64xxxxxxxxx",
  227. "NodeTemplate.ImageId": "13107fc5-0dd8-xxx-xxxxx",
  228. # "NodeTemplate.KeyId.1":"c409d431-xxx-xxx-9834-xxxxx",
  229. "NodeTemplate.DataDiskGb": 0,
  230. "NodeTemplate.SubnetId.1": "2c83f2d6-8160-xxx-xxxxx",
  231. "NodeTemplate.InstanceType": "E1.2B",
  232. "NodeTemplate.SecurityGroupId": "8728f772-6186-xxx-xxxxx",
  233. "NodeTemplate.ChargeType": "HourlyInstantSettlement",
  234. "NodeTemplate.SystemDisk.DiskType": "Local_SSD",
  235. "NodeTemplate.SubnetStrategy": "balanced-distribution",
  236. "NodeTemplate.AdvancedSetting.DockerPath": "/data/docker/xuan",
  237. "NodeTemplate.AdvancedSetting.Schedulable": True,
  238. "NodeTemplate.AdvancedSetting.UserScript": "ZWNobyAicG9zIiA+cG9z",
  239. "NodeTemplate.AdvancedSetting.PreUserScript": "ZWNobyAicHJlIiA+cHJl",
  240. "NodeTemplate.AdvancedSetting.ContainerLogMaxSize": 110,
  241. "NodeTemplate.AdvancedSetting.ContainerLogMaxFiles": 11,
  242. "NodeTemplate.SystemDisk.DiskSize": 20,
  243. "NodeTemplate.ProjectId": 0,
  244. "NodeTemplate.Password": "Xuanxxx",
  245. "NodeTemplate.DataDisk.1.Type": "EHDD",
  246. "NodeTemplate.DataDisk.1.Size": 30
  247. }
  248. m = client.modify_node_template(**param)
  249. print json.dumps(m, sort_keys=True, indent=4)
  250. #修改节点池扩容策略
  251. param = {
  252. "ClusterId": "d9a0adf0-a8f3-xxx-xxxxx",
  253. "ScaleUpPolicy": "most-pods"
  254. }
  255. m = client.modify_node_pool_scale_up_policy(**param)
  256. print json.dumps(m, sort_keys=True, indent=4)
  257. #修改节点池缩容策略
  258. param = {
  259. "ClusterId": "d9a0adf0-a8f3-xxx-xxxxx",
  260. "MaxEmptyBulkDelete": 1,
  261. "ScaleDownDelayAfterAdd": 1,
  262. "ScaleDownEnabled": True,
  263. "ScaleDownUnneededTime": 1,
  264. "ScaleDownUtilizationThreshold": 60
  265. }
  266. m = client.modify_node_pool_scale_down_policy(**param)
  267. print json.dumps(m, sort_keys=True, indent=4)
  268. #将集群内节点移入节点池
  269. param = {
  270. "ClusterId": "d9a0adf0-a8f3-xxx-xxxxx",
  271. "NodePoolId": "64xxxxxxxxx",
  272. "InstanceIds.1": "7654649f-bbc4-xxx-xxxxx"
  273. }
  274. m = client.add_cluster_instance_to_node_pool(**param)
  275. print json.dumps(m, sort_keys=True, indent=4)
  276. #节点池节点设置缩容保护
  277. param = {
  278. "ClusterId": "d9a0adf0-a8f3-xxx-xxxxx",
  279. "NodePoolId": "64xxxxxxxxx",
  280. "InstanceIds.1": "7654649f-bbc4-xxx-xxxxx",
  281. "ProtectedFromScaleDown": True
  282. }
  283. m = client.protected_from_scale_down(**param)
  284. print json.dumps(m, sort_keys=True, indent=4)
  285. #移除节点池节点
  286. param = {
  287. "ClusterId": "d9a0adf0-a8f3-xxx-xxxxx",
  288. "NodePoolId": "64xxxxxxxxx",
  289. "InstanceIds.1": "40b1fee5-5d0d-xxx-xxxxx",
  290. "InstanceDeleteMode": "Terminate"
  291. }
  292. m = client.delete_cluster_instances_from_node_pool(**param)
  293. print json.dumps(m, sort_keys=True, indent=4)
  294. '''