节点组管理
更新时间:2025-01-03
创建节点组
func CreateInstanceGroup() {
// 用户的Access Key ID和Secret Access Key
AK, SK := "", ""
// 用户指定的endpoint
ENDPOINT := ""
// 初始化一个CCEClient
ccev2Client, err := v2.NewClient(AK, SK, ENDPOINT)
if err != nil {
panic(err)
}
resp, err := ccev2Client.CreateInstanceGroup(&v2.CreateInstanceGroupArgs{
ClusterID: "",
Request: &v2.CreateInstanceGroupRequest{
InstanceGroupSpec: types.InstanceGroupSpec{
InstanceGroupName: "",
ClusterID: "",
InstanceTemplate: types.InstanceTemplate{
InstanceSpec: types.InstanceSpec{
MachineType: types.MachineTypeBCC,
InstanceType: api.InstanceType34,
InstanceName: "",
VPCConfig: types.VPCConfig{
VPCSubnetID: "sbn-xxx",
SecurityGroup: types.SecurityGroup{
EnableCCERequiredSecurityGroup: true,
EnableCCEOptionalSecurityGroup: false,
},
},
InstanceResource: types.InstanceResource{
CPU: 2,
MEM: 8,
RootDiskType: api.StorageTypeHP1,
RootDiskSize: 100,
LocalDiskSize: 0,
CDSList: []types.CDSConfig{},
MachineSpec: "bcc.g5.c2m8",
},
ImageID: "7183d3d0-3e24-464d-9c02-xxxxxxx",
InstanceOS: types.InstanceOS{
ImageType: api.ImageTypeSystem,
},
SSHKeyID: "k-xxxx",
DeployCustomConfig: types.DeployCustomConfig{
KubeletRootDir: "/var/lib/kubelet",
KubeReserved: map[string]string{
"cpu": "50m",
"memory": "100Mi",
},
SystemReserved: map[string]string{
"cpu": "50m",
"memory": "100Mi",
},
ContainerdConfig: types.ContainerdConfig{
DataRoot: "/home/cce/containerd",
},
},
RelationTag: true,
InstanceChargingType: api.PaymentTimingPostPaid,
Tags: types.TagList{
{
TagKey: "tagkey",
TagValue: "tagvalue",
},
},
},
},
Replicas: 1,
},
},
})
if err != nil {
fmt.Println(err.Error())
return
}
s, _ := json.MarshalIndent(resp, "", "\t")
fmt.Println("Response:" + string(s))
}
创建节点组并关联标签
resp, err := ccev2Client.CreateInstanceGroup(&v2.CreateInstanceGroupArgs{
ClusterID: "",
Request: &v2.CreateInstanceGroupRequest{
InstanceGroupSpec: types.InstanceGroupSpec{
InstanceGroupName: "",
ClusterID: "",
InstanceTemplate: types.InstanceTemplate{
InstanceSpec: types.InstanceSpec{
// 节点组中的实例配置
//
RelationTag: true,
Tags: types.TagList{
{
TagKey: "tagkey",
TagValue: "tagvalue",
},
},
},
},
Replicas: 1,
},
},
})
if err != nil {
fmt.Println(err.Error())
return
}
s, _ := json.MarshalIndent(resp, "", "\t")
fmt.Println("Response:" + string(s))
查询节点组
func GetInstanceGroup() {
// 用户的Access Key ID和Secret Access Key
AK, SK := "", ""
// 用户指定的endpoint
ENDPOINT := ""
// 初始化一个CCEClient
ccev2Client, err := v2.NewClient(AK, SK, ENDPOINT)
if err != nil {
panic(err)
}
resp, err := ccev2Client.GetInstanceGroup(&v2.GetInstanceGroupArgs{
ClusterID: "",
InstanceGroupID: "",
})
if err != nil {
fmt.Println(err.Error())
return
}
s, _ := json.MarshalIndent(resp, "", "\t")
fmt.Println("Response:" + string(s))
// 节点组标签
tags, _ := json.MarshalIndent(resp.InstanceGroup.Spec.InstanceTemplate.Tags, "", "\t")
fmt.Println("Response:" + string(tags))
}
添加已有节点至节点组
// TestClient_AttachInstancesToInstanceGroup 添加已有节点至节点组
func TestClient_AttachInstancesToInstanceGroup(t *testing.T) {
var (
ak = ""
sk = ""
endpoint = ""
clusterID = ""
instanceGroupID = ""
existInstanceID = ""
AdminPassword = ""
// rebuild 是否重装操作系统
rebuild = true
// useInstanceGroupConfig 是否使用节点组配置
useInstanceGroupConfig = true
// useLocalDiskForContainer 是否使用本地盘保存数据
useLocalDiskForContainer = false
imageID = ""
)
cceClient, err := NewClient(ak, sk, endpoint)
if err != nil {
fmt.Printf("NewClient error: %s", err.Error())
return
}
attachInstanceToInstanceGroupArgs := func() *AttachInstancesToInstanceGroupArgs {
args := AttachInstancesToInstanceGroupArgs{}
args.ClusterID = clusterID
args.InstanceGroupID = instanceGroupID
args.Request = &AttachInstancesToInstanceGroupRequest{
ExistedInstances: make([]*InstanceSet, 0),
UseInstanceGroupConfig: useInstanceGroupConfig,
}
existInstance := &InstanceSet{
InstanceSpec: types.InstanceSpec{
Existed: true,
ExistedOption: types.ExistedOption{
// bcc 实例 id
ExistedInstanceID: existInstanceID,
Rebuild: &rebuild,
},
// 看具体的类型,bcc,bbc,ebc
MachineType: types.MachineTypeBCC,
ClusterRole: types.ClusterRoleNode,
// 二选一
AdminPassword: AdminPassword,
SSHKeyID: "",
},
}
// 如果需要重装操作系统,需要配置这里
if rebuild {
existInstance.InstanceSpec.InstanceOS = types.InstanceOS{
ImageType: bccapi.ImageTypeSystem,
}
existInstance.InstanceSpec.ImageID = imageID
}
if useLocalDiskForContainer {
// 将容器数据存储在数据盘或本地盘中
existInstance.InstanceSpec.InstanceResource = types.InstanceResource{
CDSList: types.CDSConfigList{
{
DataDevice: "/dev/xxx",
Path: "/home/cce",
},
},
}
}
if !useInstanceGroupConfig {
existInstance.InstanceSpec.VPCConfig = types.VPCConfig{
SecurityGroups: []types.SecurityGroupV2{
{
ID: "",
Name: "",
Type: types.SecurityGroupTypeNormal,
},
},
}
existInstance.InstanceSpec.DeployCustomConfig = types.DeployCustomConfig{
KubeletRootDir: "",
PreUserScript: "",
PostUserScript: "",
}
// 标签配置
existInstance.InstanceSpec.Tags = types.TagList{
{
TagKey: "",
TagValue: "",
},
}
}
args.Request.ExistedInstances = append(args.Request.ExistedInstances, existInstance)
return &args
}()
commonResp, err := cceClient.AttachInstancesToInstanceGroup(attachInstanceToInstanceGroupArgs)
if err != nil {
fmt.Printf("attach instance to instance group failed, errir: %v", err)
return
}
fmt.Printf("Request ID: %s", commonResp.RequestID)
}
节点组移出已有节点
func TestClient_CreateScaleDownInstanceGroupTask(t *testing.T) {
type fields struct {
ak, sk, endpoint string
}
type args struct {
args *CreateScaleDownInstanceGroupTaskArgs
}
tests := []struct {
name string
fields fields
args args
}{
{
name: "移出已有节点",
fields: fields{
ak: "",
sk: "",
endpoint: "",
},
args: args{
args: &CreateScaleDownInstanceGroupTaskArgs{
InstancesToBeRemoved: []string{""},
ClusterID: "",
InstanceGroupID: "",
CleanPolicy: CleanPolicyDelete,
DeleteOption: &types.DeleteOption{
DeleteCDSSnapshot: false,
DeleteResource: false,
DrainNode: false,
MoveOut: true,
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := NewClient(tt.fields.ak, tt.fields.sk, tt.fields.endpoint)
if err != nil {
t.Errorf("failed init client, error = %v", err)
return
}
if resp, err := c.CreateScaleDownInstanceGroupTask(tt.args.args); err != nil {
t.Errorf("CreateScaleDownInstanceGroupTask() error = %v", err)
} else {
t.Logf("request id is: %s", resp.RequestID)
}
})
}
}