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());
        });

    }
}
相关推荐
webmote1 小时前
做一个FabricJS.cc的中文文档网站——面向markdown编程
canvas·fabric·使用手册·中文·fabricjs
Python私教2 小时前
ubuntu搭建k8s环境详细教程
linux·ubuntu·kubernetes
O&REO4 小时前
单机部署kubernetes环境下Overleaf-基于MicroK8s的Overleaf应用部署指南
云原生·容器·kubernetes
politeboy4 小时前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
运维小文5 小时前
K8S资源限制之LimitRange
云原生·容器·kubernetes·k8s资源限制
登云时刻5 小时前
Kubernetes集群外连接redis集群和使用redis-shake工具迁移数据(二)
redis·容器·kubernetes
wuxingge13 小时前
k8s1.30.0高可用集群部署
云原生·容器·kubernetes
志凌海纳SmartX14 小时前
趋势洞察|AI 能否带动裸金属 K8s 强势崛起?
云原生·容器·kubernetes
锅总14 小时前
nacos与k8s service健康检查详解
云原生·容器·kubernetes
BUG弄潮儿15 小时前
k8s 集群安装
云原生·容器·kubernetes