<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>4.13.0</version>
</dependency>
K8s 1.19.1 | K8s 1.18.0 | K8s 1.17.0 | K8s 1.16.0 | K8s 1.15.3 | K8s 1.14.2 | K8s 1.12.0 | K8s 1.11.0 | K8s 1.10.0 | K8s 1.9.0 | K8s 1.7.0 | K8s 1.6.0 | K8s 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集群的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());
});
}
}
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());
});
}
}