栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java k8s-client api操作 kubernetes

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java k8s-client api操作 kubernetes

API Server简介

k8s API Server提供了k8s各类资源对象(pod,RC,Service等)的增删改查及watch等HTTP Rest接口,是整个系统的数据总线和数据中心。

kubernetes API Server的功能:

  1. 提供了集群管理的REST API接口(包括认证授权、数据校验以及集群状态变更);
  2. 提供其他模块之间的数据交互和通信的枢纽(其他模块通过API Server查询或修改数据,只有API Server才直接操作etcd);
  3. 是资源配额控制的入口;
  4. 拥有完备的集群安全机制.
如何访问kubernetes API

k8s通过kube-apiserver这个进程提供服务,该进程运行在单个k8s-master节点上。默认有两个端口。

本地端口
  1. 该端口用于接收HTTP请求;
    该端口默认值为8080,可以通过API Server的启动参数“–insecure-port”的值来修改默认值;
  2. 默认的IP地址为“localhost”,可以通过启动参数“–insecure-bind-address”的值来修改该IP地址;
  3. 非认证或授权的HTTP请求通过该端口访问API Server。
vim /etc/kubernetes/manifests/kube-apiserver.yaml
- --enable-swagger-ui=true
- --insecure-bind-address=0.0.0.0
- --insecure-port=8008

访问 http://192.168.56.112:8008/

安全端口

该端口默认值为6443,可通过启动参数“–secure-port”的值来修改默认值;
默认IP地址为非本地(Non-Localhost)网络端口,通过启动参数“–bind-address”设置该值;

  1. 该端口用于接收HTTPS请求;
  2. 用于基于Tocken文件或客户端证书及HTTP base的认证;
  3. 用于基于策略的授权;
  4. 默认不启动HTTPS安全访问控制。
  • 进入 master节点,导出k8s证书config文件
cd /root/.kube
sz config   #下载该文件

然后我们将文件改名为config.yml,复制到java项目resource目录中供使用

  • java 引用
         
            io.kubernetes
            client-java
            12.0.1
        
获取所有的pod
public V1PodList getAllPodList() {
        // new a CoreV1Api
        CoreV1Api api = new CoreV1Api(apiClient);

        // invokes the CoreV1Api client
        try {
            V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
            return list;
        } catch (ApiException e) {
            log.error("获取podlist异常:" + e.getResponseBody(), e);
        }
        return null;
    }
@Test
    public void getAllPodListTest() throws IOException {
        String kubeConfigPath = "config.yml";
        K8sClient k8sClient = new K8sClient(kubeConfigPath);
        V1PodList podList = k8sClient.getAllPodList();
        for (V1Pod item : podList.getItems()) {
            System.out.println(item.getmetadata().getNamespace() + ":" + item.getmetadata().getName());
        }
    }


相当于kubectl get pods -A

相当于 http://192.168.56.112:8008/api/v1/pods

获取所有的service
public V1ServiceList getAllServiceList() {
        // new a CoreV1Api
        CoreV1Api api = new CoreV1Api(apiClient);

        // invokes the CoreV1Api client
        try {
            V1ServiceList v1ServiceList = api.listServiceForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
            return v1ServiceList;
        } catch (ApiException e) {
            log.error("获取podlist异常:" + e.getResponseBody(), e);
        }
        return null;
    }
@Test
    public void getAllServiceListTest() throws IOException {
        String kubeConfigPath = "config.yml";
        K8sClient k8sClient = new K8sClient(kubeConfigPath);
        V1ServiceList allServiceList = k8sClient.getAllServiceList();
        for (V1Service item : allServiceList.getItems()) {
            System.out.println(item.getmetadata().getNamespace() + ":" + item.getmetadata().getName());
        }
    }


相当于 kubectl get svc -A


想当于 http://192.168.56.112:8008/api/v1/services

更多的通过http://192.168.56.112:8008/api/v1/,了解更多。

参考
  • K8s核心原理(一)之API Server
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/270452.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号