Operation in Kubernetes from Java

Typically, when we talk about Kubernetes, we think of it as the operating environment for our applications, and that we will manage it through tools such as Kustomize, Kubectl, Helm, etc., and also through Container management platforms (CaaS) such as be Rancher or Openshift.

However, the need may arise to integrate some Kubernetes functionality with our own application, either so that the application itself manages some aspect of the operating environment, or because we are developing a tool that has to centrally offer Kubernetes functionality to its operators, as is the case of the «Onesait Platform Center». In these scenarios where the need to integrate with other systems arises, we will always resort to APIs, and of course Kubernetes has its own, under the API REST standard.

Still, dealing with such an extensive API, to the point of having to implement REST calls for each operation, can become really tedious for a programmer. The solution is to use the client libraries for the API in the programming language that we work with. In this sense, Kubernetes offers its own official client library in different languages, including Java.

Yet, the problem that we can find when using client libraries is that the manufacturer has not included the bulk of functionalities offered by the product from the console or from the REST API itself, and in the case of Kubernetes, after having evaluated the official library by incorporating it into the Onesait Platform Center project, we can confirm that this is the case: it is insufficient in aspects such as enumerating Workloads, operating with them, extracting metrics, etc., as well as the one with the scant documentation it offers.

Kubernetes-client Fabric8

Among the alternatives to the official library, we can highlight kubernetes-client, a fork of fabric8 that is very active on GitHub:

As you can guess, it offers a complete coverage for both the Kubernetes API and the OpenShift API, offering a client for both:

In the documentation of the library on GitHub, we can find information on how to:

  • Configure the client (including the option to do it from a kubeconfig file):
  • Create and query resources in the cluster (either programmatically or from yaml files, like we would do it with kubectl –f):
  • Listen for events on the Kubernetes cluster using the Watchers log:

Another very interesting functionality is that it offers a «Mock» mode, for development and testing with the library:

As for examples of use, you will find a fairly complete section on GitHub itself:

As well as a «Cheat Sheet» with very useful code snippets:

And a very interesting section in which a correspondence is made between Kubectl commands and their programmatic equivalent with the client library.

Fabric 8 Kubernetes Client at the Onesait Platform Center

The Onesait Platform Center is the tool that we are developing to support projects under a single management console, offering tools to support migration to the Cloud, diagramming, generation of artifacts and containers, Helm templating, deployment and monitoring of applications and solutions on CaaS platforms. It is in this last part that the library comes into play.

A Project/Product registered in the Onesait Platform Center will have one or more associated operating environments (Development, UAT, Preproduction, Production, etc.) registered through a kube config. These operating environments are external to the Onesait Platform Center, the only requirement being that they be based on Kubernetes, ranging from a Rancher managing a cluster in an IaaS environment, to an organization’s Openshift:

Through this configuration, and using the library client, we can already establish a connection to the operating environment and manage the application workloads in its Kubernetes environment, without the user having to specifically enter the CaaS platform for the most trivial tasks:

However, as has been seen in the different functionalities covered by the examples of this library, the possibilities of use go far beyond these daily tasks, which will give us the opportunity to progressively add new functionality to the Onesait Platform Center to centrally cover many projects, different tasks currently carried out by development and operations teams, accessing a plethora of environments with different characteristics (some managed with Rancher, others with Openshift, in different cloud providers).

No obstante, como se ha podido apreciar en las diferentes funcionalidades que cubren los ejemplos de esta librería, las posibilidades de uso van mucho mas allá de estas tareas cotidianas, lo que nos dará pie a ir añadiendo progresivamente nueva funcionalidad al Onesait Platform Center para cubrir de forma centralizada a muchos proyectos, diferentes tareas que realizan actualmente los equipos de desarrollo y operaciones accediendo a una pléyade de entornos con diferentes características (unos administrados con Rancher, otros con Openshift, en diferentes proveedores cloud).

Header image: Pawel Czerwinski at Unsplash

Leave a Reply

Your email address will not be published.