Managing deployments, namespace and services in Kubernetes using Fabric8 client — Java

Dynamic way to create, delete and scale your applications using fabric8.

Sep 10 · 4 min read
Image for post
Image for post

Hello fellow developers,

In this cloud era, deploying and maintaining applications(micro-services) is such a hassle. What if I told you there’s an easy way out of this problem. Yes, Fabric8 is the answer, we can use this java client to manage our applications just with few commands, or you can automate it based on some criteria as per your desire.

Let’s get started


Adding the fabric8 kubernetes-client dependency to your pom.xml allows us to use all the features provided by fabric8, just check for version compatibility with other dependencies if you face any issues.

Basic Features provided by fabric8

Creating a client in fabric8

KubernetesClient client = new DefaultKubernetesClient();

You can also create a client with specific configurations by using ConfigBuilder to create a config object for the Kubernetes client:

Accessing resources in your cluster using fabric8

NamespaceList myNs = client.namespaces().list();

List of all services:

ServiceList myServices =;

List of all services in a particular namespace:

ServiceList myNsServices ="default").list();

Get details on particular resource namespace/service:

Function to create and delete namespace using fabric8

In the above code the function createNamespace takes two arguments, the namespace name and the labels. So the labels are very useful, for example if istio has to be enabled for this namespace, we can add a label as .addToLabels(“istio-injection”, “enabled”). Coming to the namespace deletion, it’s pretty straightforward, passing the name of namespace to be deleted. Client has to be closed wherever it is created, so closing it in the finally block, the common exception thrown in this case is KubernetesClientException, so just logging it in the catch block, you can perform your own operation.

Function to create and delete service using fabric8

In the above code we are creating service of type NodePort, but you can choose other types, service ports can also be added based on requirement. The Service will be created/deleted in the specified namespace in the given name.This specification will create a Service which targets TCP port specified on any Pod with the app: name selector, and expose it on an abstracted Service port.

Function to create and delete Deployment using fabric8

You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.

In the above code, we create deployment in specified namespace with specified name and runs on port given unless its already taken. Replicas can also be specified which creates multiple instances of the application and does weight distribution. The image here specifies the path to a docker image created with your application. Make sure to give the selector properly as specified in the service so that connection happens correctly and traffic is routed properly. Fabric8 allows us to personalize the configuration of above resources with such ease.

Scaling applications using fabric8

The code scales the deployment in the specified namespace with given replicas count. We can also know the current number of replicas of a particular deployment, so based on the current replicas we can take a call to decide if it has to be scaled up/down to meet the traffic requirements:

These show only the basic things needed to create namespace/service/deployments but fabric8 allows all or most of the configurations provided by kubernetes, so they can be added as per your requirements.

Fabric8 functions makes a structure similar to the YAML file we create for namespace/service/deployments. So using these functions we can create rest apis and manage them from dashboard or some UI without the need to create those massive YAML files and running them in VirtualMachines. Just make sure this java program runs in the same kubernetes cluster.

If anyone finds these useful please drop a clap, feel free to share this or let me know should there be an error / bad practice / implementations. This is Vinesh signing-off. Byeee :)


Medium’s largest Java publication, followed by 7300+ programmers. Follow to join our community.


Written by


Coder 👨‍💻, Gamer🎮 and a dog person🐶. That says a lot about me, I guess😛.


A humble place to learn Java and Programming better.


Written by


Coder 👨‍💻, Gamer🎮 and a dog person🐶. That says a lot about me, I guess😛.


A humble place to learn Java and Programming better.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store