新建CCE集群
引导式阅读
Go
新建CCE集群
作者
C***
上架时间
2023-11-14 03:29:13

0. 版本说明

本示例基于华为云SDK V3.0版本开发。

1. 示例简介

华为云提供了云容器引擎 CCE SDK,您可以直接集成SDK来调用相关API,从而实现对云容器引擎服务的快速操作。

Kubernetes是大规模容器集群管理软件。该示例展示了如何在云容器引擎服务创建一个Kubernetes集群,并等待集群创建成功。

注意:示例中创建的CCE集群为按需计费,如不再使用请及时删除集群。

2. 开发前准备

  • 已注册华为云,并完成实名认证。
  • 已在华为云控制台授权使用CCE服务。
  • 具备开发环境 ,支持go 1.14及以上版本。
  • 已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见访问密钥。
  • 已获取CCE服务对应区域的项目ID,请在华为云控制台“我的凭证 > API凭证”页面上查看项目ID。具体请参见API凭证。
  • 已创建可用的虚拟私有云和子网,请在华为云控制台“网络控制台 >虚拟私有云”页面上查看虚拟私有云ID和子网ID。

3. 安装SDK

云容器引擎 CCE SDK支持go 1.14及以上版本。执行go version检查当前Go的版本信息。

使用go get安装华为云Go SDK,执行如下命令安装华为云Go SDK库以及相关依赖库,具体的SDK版本号请参见SDK开发中心

# 安装华为云Go库 go get github.com/huaweicloud/huaweicloud-sdk-go-v3

4. 代码示例

以下代码展示如何创建一个CCE集群:

package main import ( "encoding/json" "fmt" "time" "os" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" cce "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/region" ) func main() { /* 以下部分请替换成用户实际参数 */ // 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 ak := os.Getenv("HUAWEICLOUD_SDK_AK") // 华为云账号Access Key sk := os.Getenv("HUAWEICLOUD_SDK_SK") // 华为云账号Secret Access Key projectID := "<YOUR PROJECT ID>" // 华为云账号项目ID clusterName := "<YOUR ClUSTER NAME>" // 新建集群的名称, eg: test-cluster vpc := "<YOUR VPC ID>" // 新建集群使用的虚拟私有云ID subnet := "<YOUR SUBNET ID>" // 新建集群使用的子网ID userRegion := "<YOUR REGION>" // 服务所在区域,eg: cn-north-4 /* 主流程 */ fmt.Printf("This is a demo to create cluster.\n") data := "{\"kind\":\"Cluster\",\"apiVersion\":\"v3\",\"spec\":{\"type\":\"VirtualMachine\",\"flavor\":\"cce.s1.small\",\"version\":\"v1.19\",\"description\":\"\",\"ipv6enable\":false,\"containerNetwork\":{\"mode\":\"vpc-router\"},\"authentication\":{\"mode\":\"rbac\"},\"billingMode\":0}}" auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectID). Build() fmt.Printf("Build auth successfully.\n") client := cce.NewCceClient( cce.CceClientBuilder(). WithRegion(region.ValueOf(userRegion)). WithCredential(auth). Build()) fmt.Printf("Build client successfully.\n") request := &model.CreateClusterRequest{} request.Body = &model.Cluster{} json.Unmarshal([]byte(data), request.Body) request.Body.Metadata = &model.ClusterMetadata{ Name: clusterName, } request.Body.Spec.HostNetwork = &model.HostNetwork{ Vpc: vpc, Subnet: subnet, } /* 创建集群 */ response, err := client.CreateCluster(request) if err == nil { fmt.Printf("Start to create cluster...\n") } else { fmt.Printf("Create cluster failed...\n") fmt.Printf("Error: %+v\n", err) return } if response != nil && response.Status != nil && response.Status.JobID != nil { waitClusterReady(client, *response.Status.JobID) } else { fmt.Printf("Get cluster state failed...\n") } } /* 查询任务执行状态,等待集群创建成功 */ func waitClusterReady(c *cce.CceClient, jobID string) { req := &model.ShowJobRequest{ JobId: jobID, } ticker := time.NewTicker(5 * time.Second) defer ticker.Stop() timeout := time.After(30 * time.Minute) for { select { case <-timeout: fmt.Printf("Wait cluster state timed out") return case <-ticker.C: resp, err := c.ShowJob(req) if err != nil { fmt.Printf("Show create cluster job failed...\n") fmt.Printf("Error: %+v\n", err) return } if resp != nil && resp.Status != nil && resp.Status.Phase != nil { fmt.Printf("Wait cluster ready, job is %s...\n", *resp.Status.Phase) if *resp.Status.Phase == "Success" { fmt.Printf("Create cluster successfully!") return } else if *resp.Status.Phase == "Failed" { fmt.Printf("Create cluster failed!") return } } } } }

5. 参考

更多信息请参考:云容器引擎 CCE 文档

6. 修订记录

发布日期 文档版本 修订说明
2021-12-3 1.0 文档首次发布