Kubernetes Client for Java: Fabric8 introduction
Introduction
The Kubernetes ecosystem for Java developers is flourishing, offering a variety of tools and resources to interact with your cluster. If you need to interact with the Kubernetes API server, there are several client libraries available.
As a RedHatter, I'm the responsible for the maintenance of the Fabric8 Kubernetes Client, a powerful and widely adopted Java library for working with Kubernetes.
The Fabric8 Kubernetes Client is one of the oldest and most established Java clients for Kubernetes. Despite the emergence of the official CNCF Java Client, Fabric8 remains highly popular due to its robust feature set, extensibility, and developer-friendly APIs.
Key features of the Fabric8 Kubernetes Client
The Fabric8 Kubernetes Client offers a wide range of features that make it an excellent choice for Java developers working with Kubernetes and sets it apart from its competitors.
- Rich DSL: The client offers a Domain Specific Language (DSL) that simplifies interactions with the Kubernetes API.
- Extensibility: It supports various extensions, including those for Knative, Tekton, and Istio.
- Flexible HTTP Client: Since version 6.0, users can configure the underlying HTTP client, allowing for better integration with existing applications.
- Custom Resource Support: The client provides excellent support for working with Custom Resource Definitions (CRDs).
- Native Compatibility: It's designed to work seamlessly in native mode, making it an excellent choice for projects using frameworks like Quarkus.
Built-in support for Kubernetes and OpenShift
The Fabric8 Kubernetes Client offers tailored support for both Kubernetes and OpenShift.
Cluster | Component/Artifact |
---|---|
Kubernetes | kubernetes-client |
OpenShift | openshift-client |
While the kubernetes-client
artifact is compatible with any Kubernetes distribution,
the openshift-client
provides additional capabilities specific to OpenShift.
Extensions for Enhanced Functionality
The Fabric8 Kubernetes Client also supports a growing ecosystem of extensions, enabling seamless integration with tools like Cert Manager, Istio, Tekton, and many more:
Getting Started with Fabric8 Kubernetes Client
Let's start by setting up the Fabric8 Kubernetes Client in your project.
How do I set up Fabric8 Kubernetes Client in my project?
Adding Fabric8 to your project is straightforward.
Use the following dependency for Maven:
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>7.0.1</version>
</dependency>
Or if your project is based on Gradle:
dependencies {
implementation 'io.fabric8:kubernetes-client:7.0.1'
}
Common Operations with Fabric8 Kubernetes Client
The Fabric8 Java Client should allow you to perform at least the same operations you can execute with Kubectl or the Kubernetes Go client.
In the following sections, you will find a summary of the most common tasks (usually CRUD related) that you can achieve while using the Fabric8 client.
Retrieving a list of resources
The next example returns a list of Pods
for the specified namespace:
try (KubernetesClient kc = new KubernetesClientBuilder().build()) {
kc.pods().inNamespace("my-namespace").list().getItems()
.forEach(pod ->
System.out.printf("Pod %s%n", pod.getMetadata().getName()));
}
Retrieving a resource with a given name
The following example retrieves a Pod
with the provided name from the specified namespace:
Pod pod = kc.pods().inNamespace("my-namespace").withName("my-pod").get();
Creating a resource
The hereunder example creates a ConfigMap
in the specified namespace:
kc.configMaps().inNamespace("my-namespace").resource(
new ConfigMapBuilder()
.withNewMetadata().withName("my-configmap").endMetadata()
.addToData("data-field", "data-value")
.build()
).create();
Editing a resource with a given name
The following example adds an annotation to a Pod
with the provided name from the specified namespace:
kc.pods().inNamespace("my-namespace").withName("my-pod")
.edit(p -> new PodBuilder(p)
.editMetadata().addToAnnotations("edited", "true").endMetadata().build());
Deleting a named resource
The example below deletes a Pod
with the provided name in the specified namespace and waits 10 seconds for the operation to complete:
kc.pods().inNamespace("my-namespace").withName("my-pod").delete();
kc.pods().inNamespace("my-namespace").withName("my-pod")
.waitUntilCondition(Objects::isNull, 10, TimeUnit.SECONDS);
These examples demonstrate the simplicity and power of Fabric8's fluent DSL. For more examples, explore the official cheatsheet.
Conclusion
The Fabric8 Kubernetes Client is a comprehensive tool for Java developers working with Kubernetes. Its rich features, extensions, and developer-friendly API make it an excellent choice for managing Kubernetes resources programmatically.
Stay tuned for more articles where I’ll dive deeper into advanced operations, integration with tools like the Mock Server, and real-world use cases.
In the meantime, you can explore the following resources: