Managing deployments, namespace and services in Kubernetes using Fabric8 client — Java
Dynamic way to create, delete and scale your applications using fabric8.
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
Fabric8 is an end to end development platform spanning ideation to production for the creation of cloud native applications and micro-services. This client provides access to the full Kubernetes & OpenShift REST APIs via a fluent DSL.
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
The easiest way to create a client is:
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
List of all namespaces:
NamespaceList myNs = client.namespaces().list();
List of all services:
ServiceList myServices = client.services().list();
List of all services in a particular namespace:
ServiceList myNsServices = client.services().inNamespace("default").list();
Get details on particular resource namespace/service:
Function to create and delete namespace using fabric8
Namespaces are intended for use in environments with many users spread across multiple teams, or projects. For clusters with a few to tens of users, you should not need to create or think about namespaces at all.
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 Kubernetes, a Service is an abstraction which defines a logical set of Pods and a policy by which to access them (sometimes this pattern is called a micro-service). The set of Pods targeted by a Service is usually determined by a selector.
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 :)