kubernetes client java介绍

298 阅读3分钟

本文旨在介绍kubernetes client java库的常用API

kubernetes client java api通信原理

如图所示,kubernetes中master节点跟worker节点的通信是通过API Sever进行的,而我们使用的java api与API Server的通信也是如此。java api通过与master上的6443端口上的restful接口进行通信,完成命令的分发,节点的控制,服务的管理等操作。

image.png

 

可以用 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查询

github.com/kubernetes-…

可以通过上面这个官方地址去查看,后缀名带有API字样的是我们可能要用到的

image.png

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(); 操作

MethodHTTP requestDescription
createNodePOST /api/v1/nodes创建节点
deleteCollectionNodeDELETE /api/v1/nodes删除多个节点
deleteNodeDELETE/api/v1/nodes/{name}删除节点
listNodeGET /api/v1/nodes节点列表
patchNodePATCH/api/v1/nodes/{name}更新节点
readNodeGET /api/v1/nodes/{name}查询指定节点
replaceNodePUT /api/v1/nodes/{name}替换指定节点内容
replaceNodeStatusPUT/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); 操作

MethodHTTP requestDescription
createNamespacedDeploymentPOST创建应用
deleteCollectionNamespacedDeploymentDELETE删除多个应用
deleteNamespacedDeploymentDELETE删除应用
listNamespacedDeploymentGET应用列表
patchNamespacedDeploymentPATCH更新应用
readNamespacedDeploymentGET查询指定应用
replaceNamespacedDeploymentPUT替换指定

调试代码如下,使用此代码可以打印所有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

MethodHTTP requestDescription
getNodeMetricsGET获取节点的指标信息
getPodMetricsGET获取Pod的指标信息

service的增删查改接口

MethodHTTP requestDescription
createNamespacedServicePOST创建service
deleteNamespacedServiceDELETE删除service
listNamespacedServiceGET查找service
replaceNamespacedServicePUT修改service

pod的增删查改接口

MethodHTTP requestDescription
createNamespacedPodPOST创建Pod
deleteNamespacedPodDELETE删除Pod
listNamespacedPodGET查询Pod
replaceNamespacedPodPUT修改Pod

autoScaler的增删查改接口

MethodHTTP requestDescription
createNamespacedHorizontalPodAutoscalerPOST创建autoScaler
deleteNamespacedHorizontalPodAutoscalerDELETE删除autoScaler
listNamespacedHorizontalPodAutoscalerGET查询autoScaler
replaceNamespacedHorizontalPodAutoscalerPUT修改autoScaler