
# 使用Kubernetes API
#### Kubernetes API说明
Kubernetes API是通过HTTP提供的基于资源 (RESTful) 的编程接口。它支持通过标准HTTP请求方法（POST、PUT、PATCH、DELETE、GET）进行查询、创建、更新和删除各类集群资源。
CCE支持通过多种方式使用原生[Kubernetes API](https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/)：
- [通过集群API Server调用Kubernetes API]：（推荐）直接连接集群API Server，适合大规模调用。
- [通过API网关调用Kubernetes API]：适合小规模调用场景，大规模调用时可能会触发API网关流控。
 
 #### 通过集群API Server调用Kubernetes API
通过Kubernetes集群的API Server可以调用Kubernetes原生API。
1. 获取集群证书及API Server。 
   - 方式一：通过[获取集群证书](https://support.huaweicloud.com/api-cce-autopilot/CreateAutopilotKubernetesClusterCert.html)API获取，将返回的信息保存至kubeconfig.json文件中，并提取证书、私钥和API Server信息，命令如下。
     ```
     # 获取证书并保存为client.crt
     cat ./kubeconfig.json |grep client-certificate-data | awk -F '"' '{print $4}' | base64 -d > ./client.crt
     # 获取私钥并保存为client.key
     cat ./kubeconfig.json |grep client-key-data | awk -F '"' '{print $4}' | base64 -d > ./client.key
     # 获取API Server
     cat ./kubeconfig.json |grep server | awk -F '"' '{print $4}'
     ```
     
   
   - 方式二：通过CCE控制台的"概览"页面查询API Server地址（内网地址或公网地址），并下载证书（client.crt和client.key文件）。 ![](https://support.huaweicloud.com/api-cce-autopilot/zh-cn_image_0000001962927482.png "点击放大")
     
   
   
   
   
2. 使用集群证书调用Kubernetes原生API。 
   例如使用curl命令调用接口查看Pod信息，如下所示，其中*192.168.0.198:5443*为集群API Server地址。
   ```
   curl --cacert ./ca.crt --cert ./client.crt --key ./client.key  https://192.168.0.198:5443/api/v1/namespaces/default/pods/
   ```
   更多集群接口请参见[Kubernetes API](https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/)。
   
   
 
 #### 通过API网关调用Kubernetes API
Kubernetes原生API，可以通过API网关调用，其URL格式为：**https://{clusterid}.Endpoint/uri** 。其中**{clusterid}** 为集群ID，**uri**为资源路径，也即API访问的路径。
表1URL中的参数说明 
| 参数          | 描述                                                                                                                                                     |
|:---|:---|
| {clusterid} | 集群ID，创建集群后，调用[获取指定项目下的集群](https://support.huaweicloud.com/api-cce-autopilot/ListAutopilotClusters.html)接口获取。                                           |
| Endpoint    | Web服务入口点的URL，可以从[终端节点（Endpoint）](https://support.huaweicloud.com/api-cce-autopilot/cce_02_0001.html#cce_02_0001__section280872820353)中获取。              |
| uri         | 资源路径，也即API访问路径。从具体接口的URI模块获取，请参见[Kubernetes API](https://support.huaweicloud.com/api-cce-autopilot/cce_02_0098.html#cce_02_0098__section996118115215)。 |
   
1. 获取集群所在区域的Token，获取方式请参见[获取Token](https://support.huaweicloud.com/api-cce-autopilot/cce_02_0004.html)。
2. 获取集群ID。
   
   - 方式一：通过[获取集群信息API](https://support.huaweicloud.com/api-cce-autopilot/ListAutopilotClusters.html)查询集群uid。
   
   - 方式二：通过CCE控制台的"概览"页面查询。
   
   
   
   
3. 根据URL格式**https://{clusterid}.Endpoint/uri** ，确定请求的URL。
   
   - **{clusterid}** ：通过[2]获取。
   
   - **Endpoint** ：通过[地区和终端节点](https://developer.huaweicloud.com/endpoint)获取。
     例如CCE服务在"华东-上海一"区域的Endpoint为"cce.cn-east-3.myhuaweicloud.com"
     
   
   - **uri** ：根据需要调用的接口设置，例如需要创建一个Deployment，则请求方法为POST，接口uri为**/apis/apps/v1/namespaces/{namespace}/deployments** ，其中{namespace}为集群命名空间名称，本示例为default。
     更多接口请参见[Kubernetes API](https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/)。
     
   
   
   将上述参数根据URL格式**https://{clusterid}.Endpoint/uri**进行拼接。
   则调用接口查看所有Pod信息的URL示例如下：
   ```
   https://07da5*****.cce.cn-east-3.myhuaweicloud.com/apis/apps/v1/namespaces/default/deployments
   ```
   
   
4. 使用接口指定的请求方法，并设置请求Header参数。如果接口要求添加Body参数，可参考[Kubernetes API](https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/)添加接口对应的结构体。
   
   例如使用curl命令调用创建Deployment接口，请求方法为POST，并添加对应的Body体。
   本示例中使用nginx.json文件，创建一个名为nginx的Deployment负载，该工作负载使用nginx:latest镜像并包含两个Pod，每个Pod占用100mCPU、200Mi内存。
   ```
   curl --location --request POST 'https://07da5*****.cce.cn-east-3.myhuaweicloud.com/apis/apps/v1/namespaces/default/deployments' \
   --header 'Content-Type: application/json' \
   --header 'X-Auth-Token: MIIWvw******' \
   --data @nginx.json
   ```
   请求中包含的Header参数如下：
   表2请求Header参数 
   | 参数           | 是否必选 | 参数类型   | 描述                                                                                                                                             |
   |:---|:---|:---|:---|
   | Content-Type | 是    | String | 消息体的类型（格式）。固定为application/json。                                                                                                                |
   | X-Auth-Token | 是    | String | 调用接口的认证方式分为Token和AK/SK两种，如果您使用的Token方式，此参数为必填，请填写Token的值，获取方式请参见[获取token](https://support.huaweicloud.com/api-cce-autopilot/cce_02_0004.html)。 |
      
   nginx.json文件内容如下：
   ```
   {
       "apiVersion": "apps/v1",
       "kind": "Deployment",
       "metadata": {
           "name": "nginx"
       },
       "spec": {
           "replicas": 2,
           "selector": {
               "matchLabels": {
                   "app": "nginx"
               }
           },
           "template": {
               "metadata": {
                   "labels": {
                       "app": "nginx"
                   }
               },
               "spec": {
                   "containers": [
                       {
                           "image": "nginx:latest",
                           "name": "container-0",
                           "resources": {
                               "limits": {
                                   "cpu": "100m",
                                   "memory": "200Mi"
                               },
                               "requests": {
                                   "cpu": "100m",
                                   "memory": "200Mi"
                               }
                           }
                       }
                   ],
                   "imagePullSecrets": [
                       {
                           "name": "default-secret"
                       }
                   ]
               }
           }
       }
   }
   ```
   
   
 
#### 相关文档
- [使用Kubernetes API访问集群](https://kubernetes.io/zh-cn/docs/tasks/administer-cluster/access-cluster-api/)
- [Kubernetes官方SDK](https://kubernetes.io/zh-cn/docs/reference/using-api/client-libraries/)（包括Go、Python、Java等语言）
  
  | 语言         | 客户端库                                                                                       | 样例程序                                                                                     |
  |:---|:---|:---|
  | C          | [github.com/kubernetes-client/c](https://github.com/kubernetes-client/c/)                  | [浏览](https://github.com/kubernetes-client/c/tree/master/examples)                        |
  | dotnet     | [github.com/kubernetes-client/csharp](https://github.com/kubernetes-client/csharp)         | [浏览](https://github.com/kubernetes-client/csharp/tree/master/examples/simple)            |
  | Go         | [github.com/kubernetes/client-go/](https://github.com/kubernetes/client-go/)               | [浏览](https://github.com/kubernetes/client-go/tree/master/examples)                       |
  | Haskell    | [github.com/kubernetes-client/haskell](https://github.com/kubernetes-client/haskell)       | [浏览](https://github.com/kubernetes-client/haskell/tree/master/kubernetes-client/example) |
  | Java       | [github.com/kubernetes-client/java](https://github.com/kubernetes-client/java/)            | [浏览](https://github.com/kubernetes-client/java/tree/master/examples)                     |
  | JavaScript | [github.com/kubernetes-client/javascript](https://github.com/kubernetes-client/javascript) | [浏览](https://github.com/kubernetes-client/javascript/tree/master/examples)               |
  | Perl       | [github.com/kubernetes-client/perl/](https://github.com/kubernetes-client/perl/)           | [浏览](https://github.com/kubernetes-client/perl/tree/master/examples)                     |
  | Python     | [github.com/kubernetes-client/python/](https://github.com/kubernetes-client/python/)       | [浏览](https://github.com/kubernetes-client/python/tree/master/examples)                   |
  | Ruby       | [github.com/kubernetes-client/ruby/](https://github.com/kubernetes-client/ruby/)           | [浏览](https://github.com/kubernetes-client/ruby/tree/master/examples)                     |
     
  
 
