Cloud Native 8 min read

Master Kubernetes with Fabric8 Java Client: Quick Guide & Advanced Tips

This article introduces the Fabric8 KubernetesClient for Java, explains why it outperforms the official client, shows how to add the Maven dependency, and provides step‑by‑step code examples for listing, creating, deleting, and watching Pods, as well as advanced operations on ConfigMaps, Deployments, and custom resources, illustrating real‑world use cases such as log collection, self‑healing, and dynamic scaling.

FunTester
FunTester
FunTester
Master Kubernetes with Fabric8 Java Client: Quick Guide & Advanced Tips

Why Choose Fabric8 KubernetesClient

Fluent API : Chainable method calls provide a natural, readable coding style.

CRD Support : Full support for custom resource definitions, ideal for Operator development.

Auto KubeConfig Parsing : Works out‑of‑the‑box for local development (reads ~/.kube/config) and in‑cluster execution (uses ServiceAccount).

Spring Boot / Quarkus Integration : Seamless integration with cloud‑native Java frameworks.

Getting Started

1. Add Maven Dependency

<dependency>
  <groupId>io.fabric8</groupId>
  <artifactId>kubernetes-client</artifactId>
  <version>6.10.1</version>
</dependency>

Fabric8 automatically resolves the Kubernetes configuration. When running locally it reads ~/.kube/config; inside a cluster it uses the ServiceAccount without extra settings.

2. Basic Operations

List Pods in a Namespace

try (KubernetesClient client = new DefaultKubernetesClient()) {
    List<Pod> pods = client.pods()
        .inNamespace("default")
        .list()
        .getItems();
    for (Pod pod : pods) {
        if (pod.getMetadata().getName().contains("FunTester")) {
            System.out.println("Pod Name: " + pod.getMetadata().getName());
        }
    }
}

This code is equivalent to kubectl get pods -n default | grep FunTester.

Create a Pod

Pod pod = new PodBuilder()
    .withNewMetadata().withName("FunTester-pod").endMetadata()
    .withNewSpec()
    .addNewContainer()
    .withName("nginx")
    .withImage("nginx:latest")
    .endContainer()
    .endSpec()
    .build();
client.pods().inNamespace("default").create(pod);

The above generates the following YAML:

apiVersion: v1
kind: Pod
metadata:
  name: FunTester-pod
spec:
  containers:
  - name: nginx
    image: nginx:latest

Delete a Pod

client.pods().inNamespace("default").withName("FunTester-pod").delete();

Equivalent to kubectl delete pod FunTester-pod -n default.

Watch Pod Changes

client.pods().inNamespace("default").watch(new Watcher<Pod>() {
    @Override
    public void eventReceived(Action action, Pod pod) {
        if (pod.getMetadata().getName().contains("FunTester")) {
            System.out.println("Event: " + action + " Pod: " + pod.getMetadata().getName());
        }
    }
    @Override
    public void onClose(WatcherException cause) {
        System.out.println("Watcher closed");
    }
});

Typical use cases include monitoring pod creation, deletion, or restart to trigger auto‑scaling or fault recovery, and feeding events into log or metric pipelines.

Advanced Features

Read a ConfigMap

ConfigMap cm = client.configMaps()
    .inNamespace("default")
    .withName("FunTester-config")
    .get();
System.out.println("ConfigMap Data: " + cm.getData());

Equivalent to kubectl get cm FunTester-config -o yaml.

Operate Deployments

Get replica count

Deployment deployment = client.apps().deployments()
    .inNamespace("default")
    .withName("FunTester-deployment")
    .get();
System.out.println("Replicas: " + deployment.getSpec().getReplicas());

Scale deployment dynamically

client.apps().deployments()
    .inNamespace("default")
    .withName("FunTester-deployment")
    .edit(d -> new DeploymentBuilder(d)
        .editSpec().withReplicas(5).endSpec()
        .build());

Corresponds to

kubectl scale deployment FunTester-deployment --replicas=5 -n default

.

Work with Custom Resources (CRD)

MixedOperation<MyCustomResource, MyCustomResourceList, Resource<MyCustomResource>> customResources =
    client.resources(MyCustomResource.class, MyCustomResourceList.class);
MyCustomResource resource = customResources.inNamespace("default")
    .withName("FunTester-crd")
    .get();
System.out.println("Custom Resource Data: " + resource);

This pattern simplifies Operator development by providing a Java‑native way to manage CRDs.

Real‑World Scenarios

Log Collection

LogWatch logWatch = client.pods()
    .inNamespace("default")
    .withName("FunTester-pod")
    .watchLog(System.out);

Streams pod logs in real time, ideal for log‑aggregation systems.

Self‑Healing

client.pods().inNamespace("default").watch(new Watcher<Pod>() {
    @Override
    public void eventReceived(Action action, Pod pod) {
        if (action == Action.DELETED && pod.getMetadata().getName().contains("FunTester")) {
            System.out.println("Pod " + pod.getMetadata().getName() + " deleted, restarting...");
            client.pods().inNamespace("default").create(pod);
        }
    }
    @Override
    public void onClose(WatcherException cause) {
        System.out.println("Watcher closed");
    }
});

Automatically recreates crashed pods to maintain high availability.

Dynamic Scaling Based on Metrics

client.apps().deployments()
    .inNamespace("default")
    .withName("FunTester-deployment")
    .scale(10);

Combines monitoring data with programmatic scaling for intelligent elasticity.

Conclusion

Fabric8 KubernetesClient enables Java developers to manage Kubernetes resources programmatically with a concise, fluent API. Compared with the official kubernetes-client-java library, it offers richer feature support (CRDs, automatic KubeConfig handling, Spring Boot/Quarkus integration) and is well‑suited for automation, monitoring, log collection, and elastic scaling scenarios.

Javacloud-nativeKubernetesDevOpsFabric8KubernetesClient
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.