5

springboot调用kubernetes-client方法填坑

 2 years ago
source link: https://www.wencst.com/archives/2244
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

springboot调用kubernetes-client方法填坑

作者: wencst 分类: docker,云计算,微服务,架构设计 发布时间: 2022-04-24 15:00 阅读: 31 次

引入步骤:

1.构建springboot工程

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/>
    </parent>

注意:这里使用的版本号:2.6.7,当前2.*的最新版,尝试过使用2.1.5-RELEASE和3.0.0-M2等版本,均不适用

2.添加kubernetes-client工程


		<dependency>
			<groupId>io.kubernetes</groupId>
			<artifactId>client-java</artifactId>
			<version>14.0.0</version>
		</dependency>

注意:当前所使用的14.0.0是最新版工程,因为实际部署的kubernetes版本为1.22.0版本,看了一下版本表对应关系,发现14.0.0最合适,当然最新版有15.0.1,暂未尝试。

3.构建kubernetes config类


    public ApiClient getConnection() {

        try {
            client = Config.defaultClient();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        Configuration.setDefaultApiClient(client);
        return client;
    }

4.构建调用list的方法进行尝试

    public static void main(String[] args) throws IOException, ApiException{
        K8sConfig config = new K8sConfig();
        ApiClient client = config.getConnection();
        CoreV1Api api = new CoreV1Api();
        V1PodList podlist = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
        for (V1Pod item : podlist.getItems()) {
            System.out.println(item.getMetadata().getName());
        }
    }

5.拷贝环境变量

将k8s master中的~/.kube/config拷贝到本地(相应node)的~/.kube文件夹下

6.设置hosts

执行hosts需要查看config文件中的api-server的hosts

本文需要设置的域名为:apiserver.cluster.local

7.再次尝试调用步骤4中的方法,通过。

如果文章对您有用,扫一下支付宝的红包,不胜感激!

欢迎加入QQ群进行技术交流:656897351(各种技术、招聘、兼职、培训欢迎加入)

Leave a Reply Cancel reply

You must be logged in to post a comment.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK