by Anish
Posted on Tuesday July 30th 2019
In this blog we will learn how to access your kubernetes cluster from your Java Client. For this you need the following Information
Locate where your cluster-API is running
root# kubectl cluster-info
Kubernetes master is running at https://192.0.2.80:6443
coredns is running at https://192.0.2.80:6443/api/v1/namespaces/kube-system/services/coredns:dns/proxy
kubernetes-dashboard is running at https://192.0.2.80:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy
Locate the kubeconfig file, This can be found at the below location
root# cat $HOME/.kube/config
Save this file(config) on your Java project, we will lookup this file(config) using our sample java client program as shown below. This program uses Corev1 API and list all the pod in all the namespace.
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.fromConfig("config");
client.setVerifyingSsl(false);
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());
}
}
}
To compile this program add the required dependency for kubernetes java api client in the Pom.xml
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>5.0.0</version>
<scope>compile</scope>
</dependency>
Now we have every thing ready to connect and run the program, I have used eclipse to compile and run this Application
Next Reading : How to Monitoring All Executed Commands in Linux
Thanku for reading !!! Give a Share for Support
Asking for donation sound bad to me, so i'm raising fund from by offering all my Nine book for just $9