java k8s-client api操作 kubernetes

本文详细介绍了Kubernetes APIServer的功能,它作为集群管理的REST API接口,负责资源对象的操作,并提供安全机制。同时,文章还展示了如何通过HTTP/HTTPS端口访问API,以及使用Java客户端进行资源查询。提供了示例代码展示获取所有Pod和Service的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

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 引用
         <dependency>
            <groupId>io.kubernetes</groupId>
            <artifactId>client-java</artifactId>
            <version>12.0.1</version>
        </dependency>

获取所有的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/,了解更多。

在这里插入图片描述

参考

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值