更新时间:2025-08-27 GMT+08:00
分享

获取集群证书

功能介绍

该API用于获取指定集群的证书信息。

接口约束

该接口适用于1.13及以上集群版本。

调用方法

请参见如何调用API

URI

POST /autopilot/v3/projects/{project_id}/clusters/{cluster_id}/clustercert

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

参数解释:

项目ID,获取方式请参见如何获取接口URI中参数

约束限制:

不涉及

取值范围:

账号的项目ID

默认取值:

不涉及

cluster_id

String

参数解释:

集群ID,获取方式请参见如何获取接口URI中参数

约束限制:

不涉及

取值范围:

集群ID

默认取值:

不涉及

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

Content-Type

String

参数解释:

消息体的类型(格式)

约束限制:

GET方法不做校验

取值范围:

  • application/json

  • application/json;charset=utf-8

  • application/x-pem-file

  • multipart/form-data(注:存在FormData参数时使用)

默认取值:

不涉及

X-Auth-Token

String

参数解释:

调用接口的认证方式分为Token和AK/SK两种,如果您使用的Token方式,此参数为必填,请填写Token的值,获取方式请参见获取token

约束限制:

不涉及

取值范围:

不涉及

默认取值:

不涉及

表3 请求Body参数

参数

是否必选

参数类型

描述

duration

Integer

参数解释:

集群证书有效时间

约束限制:

不涉及

取值范围:

-1或[1,1827]

说明:
  • 最小值为1天,最大值为5年,因此取值范围为1-1827(以天为单位,实际上限取决于5年内闰年的数量,例如5年内存在一个闰年则上限为1826天);

  • 若填-1则为最大值5年。

默认取值:

不涉及

响应参数

状态码:200

表4 响应Header参数

参数

参数类型

描述

Port-ID

String

集群控制节点端口ID

表5 响应Body参数

参数

参数类型

描述

kind

String

参数解释

API类型

约束限制

该值不可修改

取值范围

不涉及

默认取值

Config

apiVersion

String

参数解释

API版本

约束限制

该值不可修改

取值范围

不涉及

默认取值

v1

preferences

Object

参数解释

当前未使用该字段

约束限制

不涉及

取值范围

不涉及

默认取值

默认为空

clusters

Array of Clusters objects

参数解释

集群列表。

约束限制

不涉及

users

Array of Users objects

参数解释

存放了指定用户的一些证书信息和ClientKey信息。

约束限制

不涉及

contexts

Array of Contexts objects

参数解释

上下文列表。

约束限制

不涉及

current-context

String

参数解释

当前上下文

约束限制

不涉及

取值范围

  • external:公网访问

  • internal:私网访问

默认取值

  • 若存在publicIp(虚拟机弹性IP)时为 external。

  • 若不存在publicIp为 internal。

表6 Clusters

参数

参数类型

描述

name

String

参数解释

集群名字。

约束限制

不涉及

取值范围

  • internalCluster:私网访问的集群

  • externalCluster:公网访问的集群

默认取值

  • 若不存在publicIp(虚拟机弹性IP),则集群列表的集群数量为1,该字段值为“internalCluster”。

  • 若存在publicIp,则集群列表的集群数量大于1,所有扩展的cluster的name的值为“externalCluster”。

cluster

ClusterCert object

参数解释

集群信息。

约束限制

不涉及

表7 ClusterCert

参数

参数类型

描述

server

String

参数解释

服务器地址

约束限制

不涉及

取值范围

不涉及

默认取值

不涉及

certificate-authority-data

String

参数解释

证书授权数据

约束限制

不涉及

取值范围

不涉及

默认取值

不涉及

insecure-skip-tls-verify

Boolean

参数解释

不校验服务端证书

约束限制

不涉及

取值范围

  • true

  • false

默认取值

在 cluster 类型为 externalCluster 时,该值为 true。

表8 Users

参数

参数类型

描述

name

String

参数解释

名称

约束限制

不涉及

取值范围

不涉及

默认取值

user

user

User object

参数解释

存放了指定用户的一些证书信息和ClientKey信息。

约束限制

不涉及

表9 User

参数

参数类型

描述

client-certificate-data

String

参数解释

客户端证书。

约束限制

不涉及

取值范围

不涉及

默认取值

不涉及

client-key-data

String

参数解释

包含来自TLS客户端密钥文件的PEM编码数据。

约束限制

不涉及

取值范围

不涉及

默认取值

不涉及

表10 Contexts

参数

参数类型

描述

name

String

参数解释

上下文的名称。

约束限制

不涉及

取值范围

  • internal:私网访问

  • external:公网访问

默认取值

  • 若不存在publicIp(虚拟机弹性IP),则集群列表的集群数量为1,该字段值为“internal”。

  • 若存在publicIp,则集群列表的集群数量大于1,所有扩展的context的name的值为“external”。

context

Context object

参数解释

上下文信息。

约束限制

不涉及

表11 Context

参数

参数类型

描述

cluster

String

参数解释

上下文cluster信息

约束限制

不涉及

取值范围

不涉及

默认取值

不涉及

user

String

参数解释

上下文user信息

约束限制

不涉及

取值范围

不涉及

默认取值

不涉及

请求示例

申请30天有效的集群访问证书

{
  "duration" : 30
}

响应示例

状态码:200

表示成功获取指定集群的证书。证书文件格式参见kubernetes v1.Config结构

{
  "kind" : "Config",
  "apiVersion" : "v1",
  "preferences" : { },
  "clusters" : [ {
    "name" : "internalCluster",
    "cluster" : {
      "server" : "https://192.168.1.7:5443",
      "certificate-authority-data" : "Q2VydGlmaWNhdGU6******FTkQgQ0VSVElGSUNBVEUtLS0tLQo="
    }
  } ],
  "users" : [ {
    "name" : "user",
    "user" : {
      "client-certificate-data" : "LS0tLS1CRUdJTiBDR******QVRFLS0tLS0K",
      "client-key-data" : "LS0tLS1CRUdJTi******BLRVktLS0tLQo="
    }
  } ],
  "contexts" : [ {
    "name" : "internal",
    "context" : {
      "cluster" : "internalCluster",
      "user" : "user"
    }
  } ],
  "current-context" : "internal"
}

SDK代码示例

SDK代码示例如下。

申请30天有效的集群访问证书

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.cce.v3.region.CceRegion;
import com.huaweicloud.sdk.cce.v3.*;
import com.huaweicloud.sdk.cce.v3.model.*;


public class CreateAutopilotKubernetesClusterCertSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String projectId = "{project_id}";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        CceClient client = CceClient.newBuilder()
                .withCredential(auth)
                .withRegion(CceRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateAutopilotKubernetesClusterCertRequest request = new CreateAutopilotKubernetesClusterCertRequest();
        request.withClusterId("{cluster_id}");
        CertDuration body = new CertDuration();
        body.withDuration(30);
        request.withBody(body);
        try {
            CreateAutopilotKubernetesClusterCertResponse response = client.createAutopilotKubernetesClusterCert(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

申请30天有效的集群访问证书

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcce.v3.region.cce_region import CceRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcce.v3 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    projectId = "{project_id}"

    credentials = BasicCredentials(ak, sk, projectId)

    client = CceClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(CceRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateAutopilotKubernetesClusterCertRequest()
        request.cluster_id = "{cluster_id}"
        request.body = CertDuration(
            duration=30
        )
        response = client.create_autopilot_kubernetes_cluster_cert(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

申请30天有效的集群访问证书

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main

import (
	"fmt"
	"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() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")
    projectId := "{project_id}"

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        Build()

    client := cce.NewCceClient(
        cce.CceClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateAutopilotKubernetesClusterCertRequest{}
	request.ClusterId = "{cluster_id}"
	request.Body = &model.CertDuration{
		Duration: int32(30),
	}
	response, err := client.CreateAutopilotKubernetesClusterCert(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

表示成功获取指定集群的证书。证书文件格式参见kubernetes v1.Config结构

错误码

请参见错误码

相关文档