A logo showing the text blog.marcnuri.com
Español
Home»Java»Kubernetes Client for Java: Fabric8 introduction

Recent Posts

  • Fabric8 Kubernetes Client 6.5.0 is now available!
  • Eclipse JKube 1.11 is now available!
  • Fabric8 Kubernetes Client 6.4.1 is now available!
  • I bought an iPad
  • Three years at Red Hat

Categories

  • Front-end
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • December 2015
  • November 2015
  • November 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Kubernetes Client for Java: Fabric8 introduction

2021-12-01 in Java tagged Client / Cloud / Fabric8 / Java / Kubernetes / OpenShift by Marc Nuri | Last updated: 2022-09-01
Versión en Español

The logo of Fabric8 Kubernetes Client
The logo of Fabric8 Kubernetes Client

Introduction

The Kubernetes Java world is a very rich ecosystem with an endless amount of resources. For instance, when you need to interact with the Kubernetes API server, you have a few options to choose from.

In an earlier post I introduced you to YAKC, a client I implemented as a side project. Today I'm going to talk about the other client I officially maintain, the Fabric8 Kubernetes Client.

The Fabric8 Kubernetes Client is the oldest Java Kubernetes Client of the bunch. And even though there is now an official CNCF Java Client for Kubernetes, Fabric8 remains one of the most popular options.

The Fabric8 client differentiates itself from the rest because it provides a fluent DSL. Both, to perform operations, and to instantiate and modify resource types.

What are the main components of the Fabric8 Kubernetes Client?

Cluster specific implementations

Depending on the cluster type you are targeting, there are different options you can choose from. The basic choice is the kubernetes-client artifact, which should be compatible with any Kubernetes implementation (including OpenShift). However, if you want to take full advantage of OpenShift and its unique resources, you should use the more specific artifact openshift-client.

ClusterComponent/Artifact
Kuberneteskubernetes-client
OpenShiftopenshift-client

Available Extensions

In addition to the base client, there is a growing number of extensions available as part of the Fabric8 Kubernetes Client distribution.

ExtensionComponent/Artifact
Apache Camel Kcamel-k-client
cert-managercertmanager-client
Chaos Meshchaosmesh-client
Istioistio-client
Knativeknative-client
Open Cluster Managementopen-cluster-management-client
Service Catalogservicecatalog-client
Tektontekton-client
Vertical Pod Autoscalerverticalpodautoscaler-client
Volcanovolcano-client
Volume Snapshotvolumesnapshot-client

How do I set up Fabric8 Kubernetes Client in my project?

The easiest way to get started is by including your required component dependency in your project.

If you are using Maven, this can be as easy as:

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

Or if your project is based on Gradle:

dependencies {
  implementation 'io.fabric8:kubernetes-client:6.5.0'
}

Operation overview

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.

Retrieve 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()));
}

Retrieve 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();

Create a resource

The hereunder example creates a ConfigMap in the specified namespace:

kc.configMaps().inNamespace("my-namespace").create(new ConfigMapBuilder()
  .withNewMetadata().withName("my-configmap").endMetadata()
  .addToData("data-field", "data-value")
  .build()
);

Edit 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());

Delete 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 are just a few examples that showcase some of the many features that you'll get if you choose to use the Fabric8 Kubernetes Client. You can find many more examples in the official Cheatsheet.

Conclusion

In this post, I gave you a very light introduction to the Fabric8 Kubernetes Client and its components. I also included some code snippets describing how to perform basic operations in your cluster.

I will publish further posts with more detailed instructions for more involved operations. There are also complementary components in the suite, such as the Mock Server, that I'd like to showcase in a dedicated post.

In the meantime, you can learn more about the client by checking the following resources:

  • Official GitHub Repository
  • Official Examples Project
  • Fabric8 Kubernetes Client Cheatsheet
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation

← Build Kubernetes controllers with Fabric8 Kubernetes Client, Quarkus, and JKubeQuarkus: How to change the application port? →
© 2007 - 2023 Marc Nuri