如何连接到现有的 kubernetes 服务器并使用 java 列出所有 pods?
How to connect to an existing kubernetes server and list all pods using java?
我正在尝试连接到现有的 kubernetes,它在 AWS 上 运行ning 并且使用 Java 在其上使用 运行 任意命令。具体来说,我们正在使用 fabric8(尽管我愿意接受另一个 api,如果您可以使用一个提供足够的答案)。我需要在 Java 中执行此操作的原因是因为我们计划最终将其合并到我们现有的 junit 实时测试中。
现在我只需要一个示例,说明如何连接到服务器并将所有 pod 名称获取为字符串数组。谁能给我一个简单明了的例子来说明如何做到这一点。
即我想要使用 java api 的这个 bash 脚本的等价物(同样最好使用 fabric8,但我会接受另一个 api 如果你知道的话)
#!bin/bash
kops export kubecfg --name $CLUSTER --state=s3://$STATESTORE
kubectl get pod -o=custom-colums=NAME:.metadata.name -n=$NAMESPACE
这是 Kubernetes 的官方 Java 客户端。
https://github.com/kubernetes-client/java
它为您提供了一个干净的界面并在 java 中编写代码以针对 kubernetes 执行。
如文档页面中所列,列出所有 pods、
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1Pod;
import io.kubernetes.client.models.V1PodList;
import io.kubernetes.client.util.Config;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException, ApiException{
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
for (V1Pod item : list.getItems()) {
System.out.println(item.getMetadata().getName());
}
}
}
希望对您有所帮助。
这里是 kubernetes 的 fabric8 kubernetes 客户端:
https://github.com/fabric8io/kubernetes-client/
它带有流畅的 DSL 来处理 kubernetes/Openshift 资源。它也有分页支持。如果你想列出某个命名空间中的资源,那么你可以在 dsl.
中使用 inNamespace("your namespace")
参数
String master = "https://192.168.42.20:8443/";
Config config = new ConfigBuilder().withMasterUrl(master).build();
try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
// Simple Listing:
PodList simplePodList = client.pods().inAnyNamespace().list();
// List with limit and continue options:
PodList podList = client.pods().inAnyNamespace().list(5, null);
podList.getItems().forEach((obj) -> { System.out.println(obj.getMetadata().getName()); });
podList = client.pods().inAnyNamespace().list(5, podList.getMetadata().getContinue());
podList.getItems().forEach((obj) -> { System.out.println(obj.getMetadata().getName()); });
} catch (KubernetesClientException e) {
logger.error(e.getMessage(), e);
}
我正在尝试连接到现有的 kubernetes,它在 AWS 上 运行ning 并且使用 Java 在其上使用 运行 任意命令。具体来说,我们正在使用 fabric8(尽管我愿意接受另一个 api,如果您可以使用一个提供足够的答案)。我需要在 Java 中执行此操作的原因是因为我们计划最终将其合并到我们现有的 junit 实时测试中。
现在我只需要一个示例,说明如何连接到服务器并将所有 pod 名称获取为字符串数组。谁能给我一个简单明了的例子来说明如何做到这一点。
即我想要使用 java api 的这个 bash 脚本的等价物(同样最好使用 fabric8,但我会接受另一个 api 如果你知道的话)
#!bin/bash
kops export kubecfg --name $CLUSTER --state=s3://$STATESTORE
kubectl get pod -o=custom-colums=NAME:.metadata.name -n=$NAMESPACE
这是 Kubernetes 的官方 Java 客户端。
https://github.com/kubernetes-client/java
它为您提供了一个干净的界面并在 java 中编写代码以针对 kubernetes 执行。
如文档页面中所列,列出所有 pods、
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1Pod;
import io.kubernetes.client.models.V1PodList;
import io.kubernetes.client.util.Config;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException, ApiException{
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
for (V1Pod item : list.getItems()) {
System.out.println(item.getMetadata().getName());
}
}
}
希望对您有所帮助。
这里是 kubernetes 的 fabric8 kubernetes 客户端:
https://github.com/fabric8io/kubernetes-client/
它带有流畅的 DSL 来处理 kubernetes/Openshift 资源。它也有分页支持。如果你想列出某个命名空间中的资源,那么你可以在 dsl.
中使用inNamespace("your namespace")
参数
String master = "https://192.168.42.20:8443/";
Config config = new ConfigBuilder().withMasterUrl(master).build();
try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
// Simple Listing:
PodList simplePodList = client.pods().inAnyNamespace().list();
// List with limit and continue options:
PodList podList = client.pods().inAnyNamespace().list(5, null);
podList.getItems().forEach((obj) -> { System.out.println(obj.getMetadata().getName()); });
podList = client.pods().inAnyNamespace().list(5, podList.getMetadata().getContinue());
podList.getItems().forEach((obj) -> { System.out.println(obj.getMetadata().getName()); });
} catch (KubernetesClientException e) {
logger.error(e.getMessage(), e);
}