Getting started with the fabric8 Kubernetes Java client
Fabric8 has been available as a Java client for Kubernetes since 2015, and today is one of the most popular client libraries for Kubernetes. (The most popular is client-go, which is the client library for the Go programming language on Kubernetes.) In recent years, fabric8 has evolved from a Java client for the Kubernetes REST API to a full-fledged alternative to the
kubectl command-line tool for Java-based development.
Fabric8 is much more than a simple Java Kubernetes REST client. Its features include a rich domain-specific language (DSL), a model for advanced code handling and manipulation, extension hooks, a mock server for testing, and many client-side utilities. In addition to hooks for building new extensions, the fabric8 Kubernetes Java client has extensions for Knative, Tekton, Kubernetes Service Catalog, Red Hat OpenShift Service Catalog, and Kubernetes Assertions.
Many popular projects use fabric8, including Quarkus, Apache Camel, Apache Spark, and many more. See which projects work with this Kubernetes and OpenShift Java client here.
Using fabric8 with Kubernetes
Using Fabric8 is straightforward, especially because it offers an API for accessing Kubernetes resources. To get started with the Java client, you just add it as a dependency in your Maven
Alternatively, you could use
Next, we’ll look at a couple of common examples.
Everything you need to grow your career.
With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.SIGN UP
Example 1: Listing pods in a namespace
Here’s an example of listing all of the client pods in a namespace:
Example 2: Server authentication
When you use
DefaultKubernetesClient, it will try to read the
~/.kube/config file in your home directory and load information required for authenticating with the Kubernetes API server. You can override this configuration with the system property
If you are using
DefaultKubernetesClient from inside a
Pod, it will load
~/.kube/config from the
ServiceAccount volume mounted inside the
Pod. For a more complex configuration, you can simply pass a
Config object inside
DefaultKubernetesClient, like this:
Example 3: Creating a Simple Deployment:
Suppose you want to build up a quick
Deployment object and apply it onto Kubernetes Cluster. You can easily leverage on rich builder classes provided by Fabric8 to construct your Kubernetes resources on the fly. Here is an example of building up a simple Nginx
Example 4: Loading your Kubernetes resource YAMLs into Java Objects:
With Fabric8 Kubernetes Client, you can easily load your resource manifests into Java objects provided by it’s Kubernetes Model. Suppose you have a
ServiceYAML like this one:
Now in order to load this YAML object into a Kubernetes
Service object. You need to do something like this:
Example 5: Doing CRUD operations for Kubernetes resource using Client:
You can easily create, replace, edit, or delete your Kubernetes resources using Fabric8 Kubernetes Client API. We provide a rich DSL to achieve these operations. Here is an example of basic CRUD operations of a
Learn more about fabric8
Fabric8’s development team consists of mostly Java developers, so a Java developer’s perspective heavily influences this client. In this article, I’ve demonstrated just a few of fabric8’s features for using Kubernetes APIs in a Java environment. For more examples, see the Kubernetes Java client examples repository. And for a deep dive into using fabric8, visit the Fabric8 Kubernetes Java Client Cheat Sheet.