集群管理
更新时间:2024-06-27
创建集群
func CreateCluster() {
// 用户的Access Key ID和Secret Access Key
AK, SK := "", ""
// 用户指定的endpoint
ENDPOINT := ""
// 初始化一个CCEClient
ccev2Client, err := v2.NewClient(AK, SK, ENDPOINT)
if err != nil {
panic(err)
}
args := &v2.CreateClusterArgs{
CreateClusterRequest: &v2.CreateClusterRequest{
ClusterSpec: &types.ClusterSpec{
ClusterName: "your-cluster-name",
K8SVersion: "1.21.14",
RuntimeType: types.RuntimeTypeDocker,
VPCID: "vpc-id",
MasterConfig: types.MasterConfig{
MasterType: types.MasterTypeManaged,
ClusterHA: 1,
ExposedPublic: false,
ClusterBLBVPCSubnetID: "cluster-blb-vpc-subnet-id",
ManagedClusterMasterOption: types.ManagedClusterMasterOption{
MasterVPCSubnetZone: types.AvailableZoneA,
},
},
ContainerNetworkConfig: types.ContainerNetworkConfig{
Mode: types.ContainerNetworkModeKubenet,
LBServiceVPCSubnetID: "lb-service-vpc-subnet-id",
ClusterPodCIDR: "172.28.0.0/16",
ClusterIPServiceCIDR: "172.31.0.0/16",
},
Tags: types.TagList{
{
TagKey: "tagkey",
TagValue: "tagvalue",
},
},
},
NodeSpecs: []*v2.InstanceSet{
{
Count: 1,
InstanceSpec: types.InstanceSpec{
InstanceName: "",
ClusterRole: types.ClusterRoleNode,
Existed: false,
MachineType: types.MachineTypeBCC,
InstanceType: api.InstanceTypeN3,
VPCConfig: types.VPCConfig{
VPCID: "",
VPCSubnetID: "vpc-subnet-id",
SecurityGroupID: "security-group-id",
AvailableZone: types.AvailableZoneA,
},
InstanceResource: types.InstanceResource{
CPU: 4,
MEM: 8,
RootDiskSize: 40,
LocalDiskSize: 0,
CDSList: []types.CDSConfig{},
},
ImageID: "image-id",
InstanceOS: types.InstanceOS{
ImageType: api.ImageTypeSystem,
},
NeedEIP: false,
AdminPassword: "admin-password",
SSHKeyID: "ssh-key-id",
InstanceChargingType: api.PaymentTimingPostPaid,
RuntimeType: types.RuntimeTypeDocker,
RelationTag: true,
},
},
},
},
}
resp, err := ccev2Client.CreateCluster(args)
if err != nil {
fmt.Println(err.Error())
return
}
s, _ := json.MarshalIndent(resp, "", "\t")
fmt.Println("Response:" + string(s))
}
创建集群并关联标签
args := &v2.CreateClusterArgs{
CreateClusterRequest: &v2.CreateClusterRequest{
ClusterSpec: &types.ClusterSpec{
// 创建集群的其他配置
// 集群关联的标签
Tags: types.TagList{
{
TagKey: "tagkey",
TagValue: "tagvalue",
},
},
},
NodeSpecs: []*v2.InstanceSet{
{
// 创建节点实例的其他配置
// 节点同时关联到上述标签
InstanceSpec: types.InstanceSpec{
RelationTag: true,
},
},
},
},
}
查询集群
func GetCluster() {
// 用户的Access Key ID和Secret Access Key
AK, SK := "", ""
// 用户指定的endpoint
ENDPOINT := ""
// 初始化一个CCEClient
ccev2Client, err := v2.NewClient(AK, SK, ENDPOINT)
if err != nil {
panic(err)
}
clusterID := ""
resp, err := ccev2Client.GetCluster(clusterID)
if err != nil {
fmt.Println(err.Error())
return
}
s, _ := json.MarshalIndent(resp, "", "\t")
fmt.Println("Response:" + string(s))
}
查询集群标签
resp, err := ccev2Client.GetCluster(clusterID)
if err != nil {
fmt.Println(err.Error())
return
}
tags, _ := json.MarshalIndent(resp.Cluster.Spec.Tags, "", "\t")
fmt.Println("Response:" + string(tags))
查询集群列表
func ListClusters() {
// 用户的Access Key ID和Secret Access Key
AK, SK := "", ""
// 用户指定的endpoint
ENDPOINT := ""
// 初始化一个CCEClient
ccev2Client, err := ccev2.NewClient(AK, SK, ENDPOINT)
if err != nil {
panic(err)
}
args := &ccev2.ListClustersArgs{
KeywordType: "clusterName",
Keyword: "",
OrderBy: "clusterID",
Order: ccev2.OrderASC,
PageSize: 10,
PageNum: 1,
}
resp, err := ccev2Client.ListClusters(args)
if err != nil {
fmt.Println(err.Error())
return
}
s, _ := json.MarshalIndent(resp, "", "\t")
fmt.Println("Response:" + string(s))
}