本文旨在介绍kubernetes client java库的常用API
kubernetes client java api通信原理
如图所示,kubernetes中master节点跟worker节点的通信是通过API Sever进行的,而我们使用的java api与API Server的通信也是如此。java api通过与master上的6443端口上的restful接口进行通信,完成命令的分发,节点的控制,服务的管理等操作。
可以用 kubectl api-versions 命令查看目前支持的api
hj@huangji-ubuntu2004-openeuler-k8s-master:~$ kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
extensions/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
metrics.k8s.io/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
POM依赖添加
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>18.0.0</version>
</dependency>
API查询
可以通过上面这个官方地址去查看,后缀名带有API字样的是我们可能要用到的
Api使用
ApiClient初始化&认证
@Data
@Component
public class ApiCommon {
private CoreV1Api v1ApiInstance;
private AutoscalingV2beta1Api autoscalingV2Api;
private AppsV1Api appsV1Api;
private Metrics metrics;
private ApiClient client;
/**
* 该方法用于初始化API单例
*
* @param kubeConfigPath 传入本地可访问tokenConfig文件路径
* @throws IOException 抛出IO异常
* @author huangji
*/
@Value("${custom.kube-config-path}")
public void initApi(String kubeConfigPath) throws IOException {
// loading the out-of-cluster config, a kubeconfig from file-system
client =
ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();
// set the global default api-client to the in-cluster one from above
Configuration.setDefaultApiClient(client);
v1ApiInstance = new CoreV1Api(client);
autoscalingV2Api = new AutoscalingV2beta1Api(client);
appsV1Api = new AppsV1Api(client);
metrics = new Metrics(client);
}
}
node的增删改查接口
使用 CoreV1Api apiInstance = new CoreV1Api(); 操作
Method | HTTP request | Description |
---|---|---|
createNode | POST /api/v1/nodes | 创建节点 |
deleteCollectionNode | DELETE /api/v1/nodes | 删除多个节点 |
deleteNode | DELETE/api/v1/nodes/{name} | 删除节点 |
listNode | GET /api/v1/nodes | 节点列表 |
patchNode | PATCH/api/v1/nodes/{name} | 更新节点 |
readNode | GET /api/v1/nodes/{name} | 查询指定节点 |
replaceNode | PUT /api/v1/nodes/{name} | 替换指定节点内容 |
replaceNodeStatus | PUT/api/v1/nodes/{name}/status | 修改节点状态 |
调试代码如下,使用此代码可以打印所有node的信息
public V1PodList getPodNameList() throws ApiException, IOException {
// file path to your KubeConfig
String kubeConfigPath = "C:/Users/12058/Desktop/kubeconfig";
// loading the out-of-cluster config, a kubeconfig from file-system
ApiClient client =
ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();
// set the global default api-client to the in-cluster one from above
Configuration.setDefaultApiClient(client);
// the CoreV1Api loads default api-client from global configuration.
CoreV1Api api = new CoreV1Api();
// invokes the CoreV1Api client
V1PodList list =
api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
return list;
}
deploy的增删改查接口
这个接口在2019年由ExtensionsV1beta1Api换成了AppsV1Api, 这里踩了一个坑
使用AppsV1Api apiInstance = new AppsV1Api(client); 操作
Method | HTTP request | Description |
---|---|---|
createNamespacedDeployment | POST | 创建应用 |
deleteCollectionNamespacedDeployment | DELETE | 删除多个应用 |
deleteNamespacedDeployment | DELETE | 删除应用 |
listNamespacedDeployment | GET | 应用列表 |
patchNamespacedDeployment | PATCH | 更新应用 |
readNamespacedDeployment | GET | 查询指定应用 |
replaceNamespacedDeployment | PUT | 替换指定 |
调试代码如下,使用此代码可以打印所有namespace为default的信息
public V1DeploymentList listAllDeployment() throws ApiException, IOException {
String kubeConfigPath = "C:/Users/12058/Desktop/kubeconfig";
// loading the out-of-cluster config, a kubeconfig from file-system
ApiClient client =
ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();
// // set the global default api-client to the in-cluster one from above
// Configuration.setDefaultApiClient(client);
AppsV1Api apiInstance = new AppsV1Api(client);
V1DeploymentList result = apiInstance.listNamespacedDeployment("default", null, null, null, null, null, null, null, null, null, null);
return result;
}
metrics的查询接口
metrics接口的使用依赖于metrics service,而相关服务的搭建可以参考我的文章《在kubernetes上部署metrics service》
Method | HTTP request | Description |
---|---|---|
getNodeMetrics | GET | 获取节点的指标信息 |
getPodMetrics | GET | 获取Pod的指标信息 |
service的增删查改接口
Method | HTTP request | Description |
---|---|---|
createNamespacedService | POST | 创建service |
deleteNamespacedService | DELETE | 删除service |
listNamespacedService | GET | 查找service |
replaceNamespacedService | PUT | 修改service |
pod的增删查改接口
Method | HTTP request | Description |
---|---|---|
createNamespacedPod | POST | 创建Pod |
deleteNamespacedPod | DELETE | 删除Pod |
listNamespacedPod | GET | 查询Pod |
replaceNamespacedPod | PUT | 修改Pod |
autoScaler的增删查改接口
Method | HTTP request | Description |
---|---|---|
createNamespacedHorizontalPodAutoscaler | POST | 创建autoScaler |
deleteNamespacedHorizontalPodAutoscaler | DELETE | 删除autoScaler |
listNamespacedHorizontalPodAutoscaler | GET | 查询autoScaler |
replaceNamespacedHorizontalPodAutoscaler | PUT | 修改autoScaler |