当前位置: 首页 > 工具软件 > fabric8 > 使用案例 >

通过Fabric8 Java API 连接kubernetes集群

闻人德庸
2023-12-01

一、依赖

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

二、kubernetes-client与kubernetes集群版本对照

K8s 1.19.1K8s 1.18.0K8s 1.17.0K8s 1.16.0K8s 1.15.3K8s 1.14.2K8s 1.12.0K8s 1.11.0K8s 1.10.0K8s 1.9.0K8s 1.7.0K8s 1.6.0K8s 1.4.9
kubernetes-client 4.13.0---
kubernetes-client 4.12.0----
kubernetes-client 4.11.1----
kubernetes-client 4.11.0----
kubernetes-client 4.10.3----
kubernetes-client 4.10.2----
kubernetes-client 4.10.1----
kubernetes-client 4.10.0----
kubernetes-client 4.9.2-----
kubernetes-client 4.9.1-----
kubernetes-client 4.9.0-----
kubernetes-client 4.8.0-----
kubernetes-client 4.7.1-----
kubernetes-client 4.7.0-----
kubernetes-client 4.6.4-------
kubernetes-client 4.6.3-------
kubernetes-client 4.6.2-------
kubernetes-client 4.6.1-------
kubernetes-client 4.6.0-------
kubernetes-client 4.5.2--------
kubernetes-client 4.5.1--------
kubernetes-client 4.5.0--------
kubernetes-client 4.4.2--------
kubernetes-client 4.4.1--------
kubernetes-client 4.4.0--------
kubernetes-client 4.3.1--------
kubernetes-client 4.3.0--------
kubernetes-client 4.2.2---------
kubernetes-client 4.2.1---------
kubernetes-client 4.2.0---------
kubernetes-client 4.1.3---------
kubernetes-client 4.1.2---------
kubernetes-client 4.1.1---------
kubernetes-client 4.1.0----------
kubernetes-client 4.0.0----------
kubernetes-client 3.2.0----------
kubernetes-client 3.1.12----------
kubernetes-client 3.0.11----------
kubernetes-client 3.0.10----------
kubernetes-client 3.0.3------------
kubernetes-client 1.3.92-----------++

三、连接kubernetes集群

1.通过admin.conf连接kubernetes集群

通过kubernetes集群的admin.conf文件连接比较方便、快捷~ ,admin.conf文件在kubernets集群主节点的/etc/kubernetes目录下,然后拷贝到项目的src/main/resources目录下即可。

package com.example.demo;

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.ServiceList;
import io.fabric8.kubernetes.api.model.apps.DeploymentList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import lombok.extern.slf4j.Slf4j;

/**
 * junit5
 */
@Slf4j
public class KubernetsClientTest {
	
	private KubernetesClient client;
	
	@BeforeEach
	public void init() {
		try {
			System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE,
					KubernetsClientTest.class.getClassLoader().getResource("admin.conf").getPath());
			client = new DefaultKubernetesClient();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testListPod() {
		PodList list = client.pods().inNamespace("default").list();
		List<Pod> items = list.getItems();
		items.stream().forEach(pod -> {
			log.info("{}" , pod.getMetadata().getName());
		});
	}
	
}

2.通过token连接kubernetes集群

package com.example.demo;

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.ServiceList;
import io.fabric8.kubernetes.api.model.apps.DeploymentList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import lombok.extern.slf4j.Slf4j;

/**
 * junit5
 */
@Slf4j
public class KubernetsClientTest {
	
	private KubernetesClient client;
    private String token = "sfasdfxxx";
	
	@BeforeEach
	public void init() {
		try {
			Config build = new ConfigBuilder().withMasterUrl("https://xxx:6443").withTrustCerts(true).withOauthToken(token).build();
			client = new DefaultKubernetesClient(build);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testListPod() {
		PodList list = client.pods().inNamespace("default").list();
		List<Pod> items = list.getItems();
		items.stream().forEach(pod -> {
			log.info("{}" , pod.getMetadata().getName());
		});
	}
	
}

四、更多API

https://github.com/fabric8io/kubernetes-client/blob/master/doc/CHEATSHEET.md#initializing-kubernetes-client

 类似资料: