Creating a Kubernetes Service in Java Creating a Kubernetes Service in Java kubernetes kubernetes

Creating a Kubernetes Service in Java


Fabric8's Kubernetes Client is using a generated model and DSL that has the exact same structure as as the JSON and YAML configuration.

So in order to create a Service instance that looks like:

 {   "kind": "Service",   "apiVersion": "v1",   "metadata": {       "name": "myservice"   },   "spec": {       "ports": [           {              "protocol": "TCP",              "port": 80,              "targetPort": 8080,          }      ],      "selector": {          "key": "value1",      },¬      "portalIP": "172.30.234.134",      "type": "ClusterIP",  }

}

You can use the following code:

Service service = new ServiceBuilder()          .withNewMetadata()              .withName("myservice")          .endMetadata()          .withNewSpec()            .addNewPort()              .withProtocol("TCP")              .withPort(80)              .withNewTargetPort(8080)            .endPort()            .addToSelector("key1", "value1")            .withPortalIP("172.30.234.134")            .withType("ClusterIP")          .endSpec()          .build();

If don't need to hold a reference of the service object and you just want to create it, you can inline it like:

client.services().createNew()          .withNewMetadata()              .withName("myservice")          .endMetadata()          .withNewSpec()            .addNewPort()              .withProtocol("TCP")              .withPort(80)              .withNewTargetPort(8080)            .endPort()            .addToSelector("key1", "value1")            .withPortalIP("172.30.234.134")            .withType("ClusterIP")          .endSpec()          .done();

It's even more compact that the JSON equivalent, because default value can be committed and also some stuff like selector can be optionally inlined in a single line.

This is something that not only applies to Service, but to every-single Kubernetes/Openshift resource.

If you have the JSON or YAML in place you can load them easily by providing an input stream to the client:

Service service = client.services().load(inputStream).get();

There are more options here, like directly creating the service:

Service newService = client.services().load(inputStream).create();

It's always help to remember that structure is always the same regardless of lang, format. Pretty much anything can be inlined, so tab completion in your IDE can be really really helpful.


I'm using the same library, and you're right, the docs are a bit lacking in the creation department. I had to use Eclipse to kind of explore the API by hand, because I couldn't find any explanation of this either. Here's how it works.

KubernetesClient client = new KubernetesClient(masterURL);Service serv = (Service) KubernetesHelper.loadJson(serviceJson);Service result = client.services().create(serv);

So basically, you use the KubernetesClient to create all objects. It will have methods (services, replicaiton controllers, etc) for each object, and any actions associated with them. The second line loads your service definition file, or String, into a Service object. Then you use the client to create the object in Kubernetes.

The KubernetesHelper object can load yaml or json. You can also use the ServiceBuilder object to build it programatically.

EDIT: And after looking back through the answers tagged kubernetes, I found this in a question asking basically the same thing: example to deploy docker image on kubernetes from java


If u do not know resource type(pod, service, deployment etc) that you are creating then you can use

client.load(inputStream).createOrReplaceAnd();

It will create or update created resource.

NOTE:If you are defining multiple resource to be created in single yaml file and with latest kubernetes client, you will be able to create only first resource.

Using kubernetes dashboard or kubectl, you should be able to create multiple resources listed in same yaml file.