简体   繁体   中英

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. Specifically, we are using fabric8 (although I am open to another api if you can provide a sufficient answer using one). The reason I need to do this in Java is because we plan to eventually incorporate this into our existing junit live tests.

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. 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)

#!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.

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

It gives you a clean interface and write code in java to execute against kubernetes.

As listed in the documentation page to list all 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());
        }
    }
}

Hope it helps.

Here is the fabric8 kubernetes client for kubernetes:

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

It comes with a fluent DSL to work with kubernetes/Openshift resources. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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