通过java代码控制k8s

通过java代码控制k8s

参考文档 k8sjava源码: https://github.com/wgplovell/kubernetes-java

第一步需要获得config文件来访问对应的master主机

1.config文件的位置在master主机中 ~/.kube/config,将复制到本地

2.创建操作类(此处将config文件放在了resources文件中)

 private void setDefaultApiClient() throws Exception {
        // 存放K8S的config文件的全路径
		//  String kubeConfigPath = K8sDescover.class.getClassLoader().getResource("config").getPath();
        // 以config作为入参创建的client对象,可以访问到K8S的API Server
        ApiClient client = ClientBuilder
                .kubeconfig(KubeConfig.loadKubeConfig(new InputStreamReader(K8sDescover.class.getClassLoader().getResourceAsStream("config"))))
                .build();

        // 会打印和API Server之间请求响应的详细内容,生产环境慎用
        client.setDebugging(true);

        // 创建操作类
        Configuration.setDefaultApiClient(client);
    }

3.进行查询

//查询所有的namespace
public V1NamespaceList getnamespace() throws Exception {

        return new CoreV1Api().listNamespace(null,false,null, null, null, null, null, null, false);
    }
//查询所有的service
public V1ServiceList findservice() throws Exception {

        return new CoreV1Api().listNamespacedService(NAMESPACE, null,false,null,null,null,null, null,0 , false);
    }

此处为查询里边的所有信息,里边的信息较多,我就不一一列举了,如果项要返回自己想要的部分值,下边的代码查询的是pod所对应的name,apiversion,namespace,和container的数量

public HashMap<String, PodList> getpods(String namespace) throws ApiException {
        HashMap<String, PodList> map = new HashMap<>();

        V1PodList v1PodList = new CoreV1Api().listNamespacedPod(namespace, null, false, null, null, null, null, null, null, null, false);
        for (V1Pod item : v1PodList.getItems()) {
            PodList podList = new PodList();
            String name = item.getMetadata().getName();
//            String namespace = item.getMetadata().getNamespace();
            String apiVersion = item.getApiVersion();
            int containerSize = item.getSpec().getContainers().size();
            for (V1Container container : item.getSpec().getContainers()) {
                String containerName = container.getName();
                String image = container.getImage();
                podList.setContainerName(containerName);
                podList.setImage(image);
            }
            podList.setName(name);
            podList.setNamespace(namespace);
            podList.setApiVersion(apiVersion);
            podList.setContainerSize(containerSize);
            map.put(name,podList);
        }
//            System.out.println(map);

        return map;
    }

项要获取其他内容可以在源码里边查找

在这里插入图片描述
podList是所有的pod列表,找里边的item
在这里插入图片描述
这个是pod 所有的属性,description描述pod的作用,
apiversion:描述对应的版本号
metadata:里边的数据大部分都是自动生成的,所有的组件都一样
spec:里边是要修改的内容,label,container等等

4.要想实现其他增删改,只需要修改方法名即可

public V1Service createservice() throws Exception {
        V1Service v1Service = new V1ServiceBuilder()
                // meta设置
                .withNewMetadata()
                    .withName("nginx")
                .endMetadata()

                // spec设置
                .withNewSpec()
                    .withType("NodePort")
                    .addToPorts(new V1ServicePort().port(80).nodePort(30103))
                    .addToSelector("name", "nginx")
                .endSpec()
                .build();

        return new CoreV1Api().createNamespacedService(NAMESPACE, v1Service, null, null, null);
    }
public V1Status deletenamespace() throws Exception {
       V1DeleteOptions v1DeleteOptions = new V1DeleteOptionsBuilder()
               .withGracePeriodSeconds(0L)
               .build();

       return new CoreV1Api().deleteNamespace("aaa",null,null, 0, false,null , v1DeleteOptions);
   }
public V1Namespace replacenamespace() throws Exception {

       V1Namespace v1Namespace = new V1NamespaceBuilder()
               .withNewMetadata()

               .withName(NAMESPACE)
               .addToLabels("label1", "ccc")
               .addToLabels("label2", "ddd")
               .endMetadata()
               .build();

       return new CoreV1Api().replaceNamespace(NAMESPACE,v1Namespace, null, null, null);
   }

只要修改对应的return 方法即可,项要不同版本的可到源码中插卡不同版本的api
在这里插入图片描述

5.部分返回参数含义

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值