BCI实例
更新时间:2023-07-13
创建实例
使用以下代码可以创建一个BCI实例。
Go
1// import "github.com/baidubce/bce-sdk-go/services/bci"
2
3args := &CreateInstanceArgs{
4 // 保证请求幂等性
5 ClientToken: "random-uuid",
6 // BCI实例名称
7 Name: "instanceName",
8 // 可用区名称
9 ZoneName: "zoneC",
10 // 实例所属于的安全组Id
11 SecurityGroupIds: []string{"g-59gf44p4jmwe"},
12 // 实例所属的子网Id
13 SubnetIds: []string{"sbn-g463qx0aqu7q"},
14 // 实例重启策略
15 RestartPolicy: "Always",
16 // 弹性公网IP
17 EipIp: "106.13.234.xx",
18 // 自动创建EIP
19 AutoCreateEip: false,
20 // 弹性公网名称
21 EipName: "zwj-test-eip",
22 // EIP线路类型
23 EipRouteType: "BGP",
24 // 公网带宽,单位为Mbps
25 EipBandwidthInMbps: 1,
26 // 计费方式
27 EipBillingMethod: "ByTraffic",
28 // 实例所需的 GPU 资源型号
29 GPUType: "Nvidia A10 PCIE",
30 // 程序的缓冲时间,用于处理关闭之前的操作
31 TerminationGracePeriodSeconds: 0,
32 // 主机名称
33 HostName: "zwj-go-sdktest",
34 // 用户标签列表
35 Tags: []Tag{
36 {
37 TagKey: "appName",
38 TagValue: "zwj-test",
39 },
40 },
41 // 镜像仓库凭证信息
42 ImageRegistryCredentials: []ImageRegistryCredential{
43 {
44 Server: "docker.io/wywcoder",
45 UserName: "wywcoder",
46 Password: "Qaz123456",
47 },
48 },
49 // 业务容器组
50 Containers: []Container{
51 {
52 Name: "container01",
53 Image: "registry.baidubce.com/bci-zjm-public/ubuntu:18.04",
54 Memory: 0.5,
55 CPU: 0.25,
56 GPU: 0,
57 WorkingDir: "",
58 ImagePullPolicy: "IfNotPresent",
59 Commands: []string{"/bin/sh"},
60 Args: []string{"-c", "sleep 30000 && exit 0"},
61 VolumeMounts: []VolumeMount{
62 {
63 MountPath: "/usr/local/nfs",
64 ReadOnly: false,
65 Name: "nfs",
66 Type: "NFS",
67 },
68 {
69 MountPath: "/usr/local/share",
70 ReadOnly: false,
71 Name: "emptydir",
72 Type: "EmptyDir",
73 },
74 {
75 MountPath: "/config",
76 ReadOnly: false,
77 Name: "configfile",
78 Type: "ConfigFile",
79 },
80 },
81 Ports: []Port{
82 {
83 Port: 8099,
84 Protocol: "TCP",
85 },
86 },
87 EnvironmentVars: []Environment{
88 {
89 Key: "java",
90 Value: "/usr/local/jre",
91 },
92 },
93 LivenessProbe: &Probe{
94 InitialDelaySeconds: 0,
95 TimeoutSeconds: 0,
96 PeriodSeconds: 0,
97 SuccessThreshold: 0,
98 FailureThreshold: 0,
99 TerminationGracePeriodSeconds: 0,
100 Exec: &ExecAction{
101 Command: []string{"echo 0"},
102 },
103 },
104 Stdin: false,
105 StdinOnce: false,
106 Tty: false,
107 SecurityContext: &ContainerSecurityContext{},
108 },
109 },
110 // Init 容器
111 InitContainers: []Container{},
112 // 数据卷信息
113 Volume: &Volume{
114 Nfs: []NfsVolume{
115 {
116 Name: "nfs",
117 Server: "xxx.cfs.gz.baidubce.com",
118 Path: "/",
119 },
120 },
121 EmptyDir: []EmptyDirVolume{
122 {
123 Name: "emptydir",
124 },
125 },
126 ConfigFile: []ConfigFileVolume{
127 {
128 Name: "configfile",
129 ConfigFiles: []ConfigFileDetail{
130 {
131 Path: "podconfig",
132 File: "filenxx",
133 },
134 },
135 },
136 },
137 },
138}
139result, err := client.CreateInstance(args)
140if err != nil {
141 fmt.Printf("CreateInstance error: %+v \n", err)
142 return
143}
144fmt.Printf("CreateInstance success, bci instance id: %+v \n", result.InstanceId)
注意:
- 保证请求幂等性。从您的客户端生成一个参数值,确保不同请求间该参数值唯一。只支持ASCII字符,且不能超过64个字符。
- BCI实例名称,即容器组名称;支持长度为2~253个英文小写字母、数字或者连字符(-),不能以连接字符开始或结尾。如果填写大写字母,后台会自动转为小写。
- 实例重启策略。取值范围:Always:总是重启,Never:从不重启,OnFailure:失败时重启。默认值:Always。
- 自动创建EIP,并绑定到BCI实例上。只有当eipIp为空的情况下,此字段才生效。默认值为:false。
- EIP线路类型,包含标准BGP(BGP)和增强BGP(BGP_S),默认标准BGP。当autoCreateEip为true时,此字段才生效。默认值为:BGP。
- 公网带宽,单位为Mbps。对于预付费以及按使用带宽计费的后付费EIP,标准型BGP限制为1~500之间的整数,增强型BGP限制为100~5000之间的整数(代表带宽上限);对于按使用流量计费的后付费EIP,标准型BGP限制为1~200之间的整数(代表允许的带宽流量峰值)。如果填写浮点数会向下取整。当autoCreateEip为true时,此字段才生效。默认值为100。
- 计费方式,按流量(ByTraffic)、按带宽(ByBandwidth)、按增强95(ByPeak95)(只有共享带宽后付费支持)。当autoCreateEip为true时,此字段才生效。增强型BGP_S不支持按流量计费(ByTraffic),需要按带宽计费(ByBandwidth)。默认值为ByTraffic。
- 实例所需的 GPU 资源型号。目前仅支持:Nvidia A10 PCIE。
查询实例列表
使用以下代码可以查询BCI实例列表。
Go
1// import "github.com/baidubce/bce-sdk-go/services/bci"
2
3args := &ListInstanceArgs{
4 // 查询关键字名称
5 KeywordType: "podId",
6 // 查询关键字值
7 keyword: "p-xxx",
8 // 表示下一个查询开始的marker,marker为空表示没有下一个
9 Marker: "",
10 // 每页包含的最大数量
11 MaxKeys: 5,
12}
13result, err := client.ListInstances(args)
14fmt.Printf("ListInstances result: %+v, err: %+v \n", result, err)
注意:
- 查询关键字名称,取值范围:name、podId。
- 表示下一个查询开始的marker,marker为空表示没有下一个。说明:首次查询时无需设置该参数,后续查询的marker从返回结果中获取。
- 每页包含的最大数量,最大数量通常不超过1000,缺省值为10。maxKeys的取值范围:[1, 1000]之间的正整数。
查询实例详情
使用以下代码可以查询BCI实例详情。
Go
1// import "github.com/baidubce/bce-sdk-go/services/bci"
2
3args := &GetInstanceArgs{
4 // BCI实例ID
5 InstanceId: "p-xxx",
6}
7result, err := client.GetInstance(args)
8fmt.Printf("ListInstances result: %+v, err: %+v \n", result, err)
删除实例
使用以下代码可以删除BCI实例。
Go
1// import "github.com/baidubce/bce-sdk-go/services/bci"
2
3args := &DeleteInstanceArgs{
4 // 待删除的BCI实例ID
5 InstanceId: "p-xxxx",
6 // 释放关联资源
7 RelatedReleaseFlag: true,
8}
9err := client.DeleteInstance(args)
10fmt.Printf("DeleteInstance err: %+v\n", err)
注意:
- 释放关联资源,目前只有EIP资源,默认值为false。
批量删除实例
使用以下代码可以批量删除BCI实例。
Go
1// import "github.com/baidubce/bce-sdk-go/services/bci"
2
3args := &BatchDeleteInstanceArgs{
4 // 待删除的BCI实例ID列表
5 InstanceIds: []string{"p-axxx", "p-bxxx"},
6 // 释放关联资源
7 RelatedReleaseFlag: true,
8}
9err := client.BatchDeleteInstance(args)
10fmt.Printf("BatchDeleteInstance err: %+v\n", err)
注意:
- 释放关联资源,目前只有EIP资源,默认值为false。