k8s:使用Fabric 来访问api-server

一、添加依赖

        <dependency>
            <groupId>io.fabric8</groupId>
            <artifactId>kubernetes-client</artifactId>
            <version>6.3.1</version>
        </dependency>

二、通过kubectl 去k8s集群获取token

kubectl -n kube-system describe $(kubectl -n kube-system get secret -n kube-system -o name | grep namespace) | grep token

三、通过token访问k8s,使用Fabric框架

package cn.edu.tju;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;

import java.util.List;

public class Demo01 {
    private static String API_SERVER = "https://xx.xx.xx.xx:6443";
    //去服务器通过kubectl 获取token
    // kubectl -n kube-system describe $(kubectl -n kube-system get secret -n kube-system -o name | grep namespace) | grep token
    private static String token = "eyJhbGciOiJSUzI1NiIsImtpZCI6IlZuSzNPTk5fMFp1bDlHbk1sLXZuZVJuVjJkRUNmOFFwZ0hJRVRMVFF5RUUifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJuYW1lc3BhY2UtY29udHJvbGxlci10b2tlbi10ZmM5bCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJuYW1lc3BhY2UtY29udHJvbGxlciIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjIzNmEyYWFjLWEyZDktNDJkOC05ODkxLTU1YjY0YTIxYWQ0ZSIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTpuYW1lc3BhY2UtY29udHJvbGxlciJ9.IwEnORYqxM_qnVnLM2pdxlbsStHXHmj96t5DF4EstXkG4FJh0q6lc68fpLd1tvi2lJuhPPFHxEoKk3PXXza9x1mobeBRs-TYW3sPxkMNBiBDmjRvhC6Ny_dWU7LWWaNIxMxb0BeaAfgXA1e1Fi5CLxivAeQDNxRJgkFz-FaXraEM5_1CaRJzBasDNmyZZBa5LhW-_PR4O7ENG_JxDPmaAzEEY7RrPNMh27oHpfRnXIv-7b9B3Dk0NzHly1SMAQbngdU_YoyRAc6mW9-lPujl4beHgthYpvemcxdmpOmaywuhVAUYxrekomfZDM_9McArlZgi9I9GCJNTqwLUW2eJHQ";

    public static void main(String[] args) {
        KubernetesClient client = null;
        try {
            Config build = new ConfigBuilder().withMasterUrl(API_SERVER)
                    .withTrustCerts(true)
                    .withOauthToken(token).build();
            client = new DefaultKubernetesClient(build);
        } catch (Exception e) {
            e.printStackTrace();
        }

        PodList podList = client.pods().inNamespace("default").list();
        List<Pod> itemList = podList.getItems();
        itemList.stream().forEach(pod -> {
            System.out.println(pod.getMetadata().getName());
        });

    }
}
相关推荐
景天科技苑34 分钟前
【云原生开发】K8S多集群资源管理平台架构设计
云原生·容器·kubernetes·k8s·云原生开发·k8s管理系统
wclass-zhengge1 小时前
K8S篇(基本介绍)
云原生·容器·kubernetes
颜淡慕潇1 小时前
【K8S问题系列 |1 】Kubernetes 中 NodePort 类型的 Service 无法访问【已解决】
后端·云原生·容器·kubernetes·问题解决
川石课堂软件测试3 小时前
性能测试|docker容器下搭建JMeter+Grafana+Influxdb监控可视化平台
运维·javascript·深度学习·jmeter·docker·容器·grafana
昌sit!9 小时前
K8S node节点没有相应的pod镜像运行故障处理办法
云原生·容器·kubernetes
A ?Charis12 小时前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
wclass-zhengge13 小时前
Docker篇(Docker Compose)
运维·docker·容器
北漂IT民工_程序员_ZG13 小时前
k8s集群安装(minikube)
云原生·容器·kubernetes
梦魇梦狸º16 小时前
腾讯轻量云服务器docker拉取不到镜像的问题:拉取超时
docker·容器·github
南猿北者19 小时前
docker镜像仓库常用命令
运维·docker·容器