If you are getting started with Kubernetes, you probably have been using kubectl to perform operations on your cluster. Kubectl is a client library that communicates with the Kubernetes cluster using the Kubernetes API.

In this article we are going to learn how to use the Kubernetes Java Client to communicate with our cluster from a Java program.

Installation

We can find the up-to-date instructions at the official installation documentation.

For Bazel, we need to have the maven repository configured in the workspace and the client-java artifact:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
load('@bazel_tools//tools/build_defs/repo:http.bzl', 'http_archive')

RULES_JVM_EXTERNAL_TAG = '4.1'
RULES_JVM_EXTERNAL_SHA = 'f36441aa876c4f6427bfb2d1f2d723b48e9d930b62662bf723ddfb8fc80f0140'

http_archive(
  name = 'rules_jvm_external',
  strip_prefix = 'rules_jvm_external-%s' % RULES_JVM_EXTERNAL_TAG,
  sha256 = RULES_JVM_EXTERNAL_SHA,
  url = 'https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip' % RULES_JVM_EXTERNAL_TAG,
)

load('@rules_jvm_external//:defs.bzl', 'maven_install')

maven_install(
  artifacts = [
    'io.kubernetes:client-java:15.0.1',
  ],
  repositories = [
    'https://repo1.maven.org/maven2',
  ],
)

We also need to add the Kubernetes client packages as dependencies:

1
2
3
4
5
6
7
8
9
java_binary(
  name = 'demo',
  srcs = glob(['*.java']),
  main_class = 'demo.PodLister',
  deps = [
    '@maven//:io_kubernetes_client_java',
    '@maven//:io_kubernetes_client_java_api'
  ],
)

Example usage

One of the simplest things we can do with Kubernetes client is list all pods in all namespaces:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package demo;

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.Config;

import java.io.IOException;

public class PodLister {
  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, null);

    for (V1Pod item : list.getItems()) {
        System.out.println(item.getMetadata().getName());
    }
  }
}

Note that the example above uses Config.defaultClient(); The default client looks for ~/.kube/config, which contains the information and credentials necessary to connect to a cluster.

If our configuration is in another path, we can instruct the client to use a specific path:

1
2
3
ApiClient client = ClientBuilder
    .kubeconfig(KubeConfig.loadKubeConfig(new FileReader("/path/to/config")))
    .build();

You can see a full running example in: Kubernetes java client demo

Conclusion

The Kubernetes Java Client provides a way to programatically perform operations on a Kubernetes cluster. This can be used to automate tasks or check on the status of the cluster.

Although this is the official client, there is another java client by fabric8.io that is more widely used and has a simpler API.

[ docker  java  programming  ]
Monitoring Kubernetes Resources with Fabric8 Informers
Fabric8 Kubernetes Java Client
Dependency injection (Inversion of Control) in Spring framework
Jackson - Working with JSON in Java
Immutables and Java