简体   繁体   English

如何使用Java连接到现有的Kubernetes服务器并列出所有Pod?

[英]How to connect to an existing kubernetes server and list all pods using java?

I'm trying to connect to an existing kubernetes that's running on AWS and run arbitrary commands on it using Java. 我正在尝试连接到在AWS上运行的现有kubernetes,并使用Java在其上运行任意命令。 Specifically, we are using fabric8 (although I am open to another api if you can provide a sufficient answer using one). 具体来说,我们使用的是fabric8(尽管我可以使用其他api,但如果您可以使用一个API提供足够的答案)。 The reason I need to do this in Java is because we plan to eventually incorporate this into our existing junit live tests. 我之所以需要在Java中执行此操作,是因为我们计划最终将其合并到我们现有的junit在线测试中。

For now I just need an example of how to connect to the sever and get all of the pod names as an array of Strings. 现在,我只需要一个示例,说明如何连接到服务器并以字符串数组的形式获取所有Pod名称。 Can somebody show me a simple, concise example of how to do this. 有人可以给我展示一个简单,简洁的示例。

ie I want the equivalent of this bash script using a java api (again preferably using fabric8, but I'll accept another api if you know one) 即我想要使用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

Here is the official Java client for Kubernetes. 这是Kubernetes的官方Java客户端。

https://github.com/kubernetes-client/java https://github.com/kubernetes-client/java

It gives you a clean interface and write code in java to execute against kubernetes. 它为您提供了一个干净的接口,并用Java编写了代码以针对kubernetes执行。

As listed in the documentation page to list all pods, 如文档页面中所列,以列出所有Pod,

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

Hope it helps. 希望能帮助到你。

Here is the fabric8 kubernetes client for kubernetes: 这是kubernetes的fabric8 kubernetes客户端:

https://github.com/fabric8io/kubernetes-client/ https://github.com/fabric8io/kubernetes-client/

It comes with a fluent DSL to work with kubernetes/Openshift resources. 它带有流畅的DSL,可与kubernetes / Openshift资源一起使用。 It has got pagination support too. 它也有分页支持。 If you want to list resources in certain namespace then you can use inNamespace("your namespace") parameter in dsl. 如果要列出某些命名空间中的资源,则可以在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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM