节点组管理
更新时间:2025-01-03
创建节点组
Plain Text
1func CreateInstanceGroup() {
2 // 用户的Access Key ID和Secret Access Key
3 AK, SK := "", ""
4
5 // 用户指定的endpoint
6 ENDPOINT := ""
7
8 // 初始化一个CCEClient
9 ccev2Client, err := v2.NewClient(AK, SK, ENDPOINT)
10 if err != nil {
11 panic(err)
12 }
13 resp, err := ccev2Client.CreateInstanceGroup(&v2.CreateInstanceGroupArgs{
14 ClusterID: "",
15 Request: &v2.CreateInstanceGroupRequest{
16 InstanceGroupSpec: types.InstanceGroupSpec{
17 InstanceGroupName: "",
18 ClusterID: "",
19
20 InstanceTemplate: types.InstanceTemplate{
21 InstanceSpec: types.InstanceSpec{
22 MachineType: types.MachineTypeBCC,
23 InstanceType: api.InstanceType34,
24 InstanceName: "",
25 VPCConfig: types.VPCConfig{
26 VPCSubnetID: "sbn-xxx",
27 SecurityGroup: types.SecurityGroup{
28 EnableCCERequiredSecurityGroup: true,
29 EnableCCEOptionalSecurityGroup: false,
30 },
31 },
32 InstanceResource: types.InstanceResource{
33 CPU: 2,
34 MEM: 8,
35 RootDiskType: api.StorageTypeHP1,
36 RootDiskSize: 100,
37 LocalDiskSize: 0,
38 CDSList: []types.CDSConfig{},
39 MachineSpec: "bcc.g5.c2m8",
40 },
41 ImageID: "7183d3d0-3e24-464d-9c02-xxxxxxx",
42 InstanceOS: types.InstanceOS{
43 ImageType: api.ImageTypeSystem,
44 },
45 SSHKeyID: "k-xxxx",
46 DeployCustomConfig: types.DeployCustomConfig{
47 KubeletRootDir: "/var/lib/kubelet",
48 KubeReserved: map[string]string{
49 "cpu": "50m",
50 "memory": "100Mi",
51 },
52 SystemReserved: map[string]string{
53 "cpu": "50m",
54 "memory": "100Mi",
55 },
56 ContainerdConfig: types.ContainerdConfig{
57 DataRoot: "/home/cce/containerd",
58 },
59 },
60 RelationTag: true,
61 InstanceChargingType: api.PaymentTimingPostPaid,
62 Tags: types.TagList{
63 {
64 TagKey: "tagkey",
65 TagValue: "tagvalue",
66 },
67 },
68 },
69 },
70 Replicas: 1,
71 },
72 },
73 })
74 if err != nil {
75 fmt.Println(err.Error())
76 return
77 }
78 s, _ := json.MarshalIndent(resp, "", "\t")
79 fmt.Println("Response:" + string(s))
80}
创建节点组并关联标签
Plain Text
1 resp, err := ccev2Client.CreateInstanceGroup(&v2.CreateInstanceGroupArgs{
2 ClusterID: "",
3 Request: &v2.CreateInstanceGroupRequest{
4 InstanceGroupSpec: types.InstanceGroupSpec{
5 InstanceGroupName: "",
6 ClusterID: "",
7
8 InstanceTemplate: types.InstanceTemplate{
9 InstanceSpec: types.InstanceSpec{
10 // 节点组中的实例配置
11
12 //
13 RelationTag: true,
14 Tags: types.TagList{
15 {
16 TagKey: "tagkey",
17 TagValue: "tagvalue",
18 },
19 },
20 },
21 },
22 Replicas: 1,
23 },
24 },
25 })
26 if err != nil {
27 fmt.Println(err.Error())
28 return
29 }
30 s, _ := json.MarshalIndent(resp, "", "\t")
31 fmt.Println("Response:" + string(s))
查询节点组
Plain Text
1func GetInstanceGroup() {
2 // 用户的Access Key ID和Secret Access Key
3 AK, SK := "", ""
4
5 // 用户指定的endpoint
6 ENDPOINT := ""
7
8 // 初始化一个CCEClient
9 ccev2Client, err := v2.NewClient(AK, SK, ENDPOINT)
10 if err != nil {
11 panic(err)
12 }
13
14 resp, err := ccev2Client.GetInstanceGroup(&v2.GetInstanceGroupArgs{
15 ClusterID: "",
16 InstanceGroupID: "",
17 })
18 if err != nil {
19 fmt.Println(err.Error())
20 return
21 }
22
23 s, _ := json.MarshalIndent(resp, "", "\t")
24 fmt.Println("Response:" + string(s))
25
26 // 节点组标签
27 tags, _ := json.MarshalIndent(resp.InstanceGroup.Spec.InstanceTemplate.Tags, "", "\t")
28 fmt.Println("Response:" + string(tags))
29}
添加已有节点至节点组
Plain Text
1// TestClient_AttachInstancesToInstanceGroup 添加已有节点至节点组
2func TestClient_AttachInstancesToInstanceGroup(t *testing.T) {
3 var (
4 ak = ""
5 sk = ""
6 endpoint = ""
7 clusterID = ""
8 instanceGroupID = ""
9 existInstanceID = ""
10 AdminPassword = ""
11 // rebuild 是否重装操作系统
12 rebuild = true
13 // useInstanceGroupConfig 是否使用节点组配置
14 useInstanceGroupConfig = true
15 // useLocalDiskForContainer 是否使用本地盘保存数据
16 useLocalDiskForContainer = false
17 imageID = ""
18 )
19
20 cceClient, err := NewClient(ak, sk, endpoint)
21 if err != nil {
22 fmt.Printf("NewClient error: %s", err.Error())
23 return
24 }
25
26 attachInstanceToInstanceGroupArgs := func() *AttachInstancesToInstanceGroupArgs {
27 args := AttachInstancesToInstanceGroupArgs{}
28 args.ClusterID = clusterID
29 args.InstanceGroupID = instanceGroupID
30 args.Request = &AttachInstancesToInstanceGroupRequest{
31 ExistedInstances: make([]*InstanceSet, 0),
32 UseInstanceGroupConfig: useInstanceGroupConfig,
33 }
34 existInstance := &InstanceSet{
35 InstanceSpec: types.InstanceSpec{
36 Existed: true,
37 ExistedOption: types.ExistedOption{
38 // bcc 实例 id
39 ExistedInstanceID: existInstanceID,
40 Rebuild: &rebuild,
41 },
42 // 看具体的类型,bcc,bbc,ebc
43 MachineType: types.MachineTypeBCC,
44 ClusterRole: types.ClusterRoleNode,
45
46 // 二选一
47 AdminPassword: AdminPassword,
48 SSHKeyID: "",
49 },
50 }
51
52 // 如果需要重装操作系统,需要配置这里
53 if rebuild {
54 existInstance.InstanceSpec.InstanceOS = types.InstanceOS{
55 ImageType: bccapi.ImageTypeSystem,
56 }
57 existInstance.InstanceSpec.ImageID = imageID
58 }
59
60 if useLocalDiskForContainer {
61 // 将容器数据存储在数据盘或本地盘中
62 existInstance.InstanceSpec.InstanceResource = types.InstanceResource{
63 CDSList: types.CDSConfigList{
64 {
65 DataDevice: "/dev/xxx",
66 Path: "/home/cce",
67 },
68 },
69 }
70 }
71 if !useInstanceGroupConfig {
72 existInstance.InstanceSpec.VPCConfig = types.VPCConfig{
73 SecurityGroups: []types.SecurityGroupV2{
74 {
75 ID: "",
76 Name: "",
77 Type: types.SecurityGroupTypeNormal,
78 },
79 },
80 }
81 existInstance.InstanceSpec.DeployCustomConfig = types.DeployCustomConfig{
82 KubeletRootDir: "",
83 PreUserScript: "",
84 PostUserScript: "",
85 }
86 // 标签配置
87 existInstance.InstanceSpec.Tags = types.TagList{
88 {
89 TagKey: "",
90 TagValue: "",
91 },
92 }
93 }
94 args.Request.ExistedInstances = append(args.Request.ExistedInstances, existInstance)
95 return &args
96 }()
97 commonResp, err := cceClient.AttachInstancesToInstanceGroup(attachInstanceToInstanceGroupArgs)
98 if err != nil {
99 fmt.Printf("attach instance to instance group failed, errir: %v", err)
100 return
101 }
102 fmt.Printf("Request ID: %s", commonResp.RequestID)
103}
节点组移出已有节点
Plain Text
1func TestClient_CreateScaleDownInstanceGroupTask(t *testing.T) {
2 type fields struct {
3 ak, sk, endpoint string
4 }
5 type args struct {
6 args *CreateScaleDownInstanceGroupTaskArgs
7 }
8 tests := []struct {
9 name string
10 fields fields
11 args args
12 }{
13 {
14 name: "移出已有节点",
15 fields: fields{
16 ak: "",
17 sk: "",
18 endpoint: "",
19 },
20 args: args{
21 args: &CreateScaleDownInstanceGroupTaskArgs{
22 InstancesToBeRemoved: []string{""},
23 ClusterID: "",
24 InstanceGroupID: "",
25 CleanPolicy: CleanPolicyDelete,
26 DeleteOption: &types.DeleteOption{
27 DeleteCDSSnapshot: false,
28 DeleteResource: false,
29 DrainNode: false,
30 MoveOut: true,
31 },
32 },
33 },
34 },
35 }
36 for _, tt := range tests {
37 t.Run(tt.name, func(t *testing.T) {
38 c, err := NewClient(tt.fields.ak, tt.fields.sk, tt.fields.endpoint)
39 if err != nil {
40 t.Errorf("failed init client, error = %v", err)
41 return
42 }
43 if resp, err := c.CreateScaleDownInstanceGroupTask(tt.args.args); err != nil {
44 t.Errorf("CreateScaleDownInstanceGroupTask() error = %v", err)
45
46 } else {
47 t.Logf("request id is: %s", resp.RequestID)
48 }
49
50 })
51 }
52}