通过java 客户端 操作k8s集群

1.在diea中新建一个maven工程然后倒入一下依赖

   <dependency>
            <groupId>io.kubernetes</groupId>
            <artifactId>client-java</artifactId>
            <version>5.0.0</version>
   </dependency>

2.将k8s集群的config文件复制到项目路径下

 

该文件在初始化kubeadm的时候会移动到 ~/.kube下

将其复制到项目下

3.写一个小demo

package com.rongan.k8s;

import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.apis.ExtensionsV1beta1Api;
import io.kubernetes.client.models.V1Pod;
import io.kubernetes.client.models.V1PodList;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;

import java.io.FileReader;
import java.io.IOException;

public class TestClient {
    public static void main(String[] args) throws IOException, ApiException {

        //直接在项目下可以直接写文件名
        String kubeConfigPath = "config";

        //加载k8s,confg
        ApiClient client =
                ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();

        //将加载confi的client设置为默认的client
        Configuration.setDefaultApiClient(client);

        //创建一个api
        CoreV1Api api = new CoreV1Api();
        //打印所有的pod
        V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
        for (V1Pod item : list.getItems()) {
            System.out.println(item.getMetadata().getName());
        }
    }
}

4.封装自己的util

package com.rongan.k8s;

import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.AppsV1Api;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.auth.ApiKeyAuth;
import io.kubernetes.client.models.*;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;
import io.kubernetes.client.util.Yaml;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * k8s操作util
 */
public class K8sUtil {
    /**
     * 在使用前要先调用该方法,设置k8s集群的配置
     * @param kubeConfigPath
     * @return
     * @throws IOException
     */
    public static void setConfig(String kubeConfigPath) throws IOException {
        ApiClient client =
                ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();
        Configuration.setDefaultApiClient(client);
        ApiClient defaultClient = Configuration.getDefaultApiClient();
        ApiKeyAuth BearerToken = (ApiKeyAuth) defaultClient.getAuthentication("BearerToken");
        BearerToken.setApiKey("YOUR API KEY");

    }

    /**
     * 加载yaml配置文件
     *
     * @param path
     * @throws IOException
     */
    public static Object loadYaml(String path) throws IOException {
        Reader reader = new FileReader(path);
        return Yaml.load(reader);
    }

    /**
     * 创建pod
     *
     * @param nameSpace :名称空间
     * @param body      :pod
     * @return
     * @throws ApiException
     */
    public static V1Pod createPod(String nameSpace, V1Pod body) throws ApiException {

        return new CoreV1Api().createNamespacedPod(nameSpace, body, true, "true", null);

    }

    /**
     * 删除pod
     *
     * @param nameSpace
     * @param podName
     * @throws Exception
     */
    public static void deletePod(String nameSpace, String podName) throws Exception {
        new CoreV1Api().deleteNamespacedPod(podName, nameSpace, "true", null, null, null, null, null);
    }

    /**
     * 创建service
     *
     * @param nameSpace
     * @param sv
     * @throws ApiException
     */
    public static void createService(String nameSpace, V1Service sv) throws ApiException {
        new CoreV1Api().createNamespacedService(nameSpace, sv, true, "true", null);
    }

    /**
     * 删除service
     *
     * @param nameSpace
     * @param serviceName
     * @throws Exception
     */
    public static void deleteService(String nameSpace, String serviceName) throws Exception {
        new CoreV1Api().deleteNamespacedService(serviceName, nameSpace, null, null, null, null, null, null);
    }

    /**
     * 创建deployment
     *
     * @param nameSpace
     * @param body
     * @throws ApiException
     */
    public static void createDeployment(String nameSpace, V1Deployment body) throws ApiException {
        new AppsV1Api().createNamespacedDeployment(nameSpace, body, true, "true", null);
    }

    /**
     * 刪除namespace
     *
     * @param nameSpace
     * @param deployeName
     * @throws ApiException
     */
    public static void deleteDeployment(String nameSpace, String deployeName) throws ApiException {
        new AppsV1Api().deleteNamespacedDeployment(deployeName, nameSpace, "true", null, null, null, null, null);
    }

    public static void main(String[] args) throws Exception {
        setConfig("config");
        Reader reader = new FileReader("datax3.yaml");
        Object load = Yaml.load(reader);
        System.out.println(load.getClass());
        V1Deployment d = (V1Deployment) load;
        d.getMetadata().setName("datax-test");
        deleteDeployment("default", d.getMetadata().getName());

    }
}

5.更多操作可以查看文档

https://github.com/kubernetes-client/java

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页