Search code examples
javaservicekubernetesfabric8

Creating a Kubernetes Service in Java


I am using fabric8.io to orchestrate application containers in Kubernetes. I am looking to create a Service that manages a pod with a certain label on a certain port. Is there a specific example of the API that does this. I couldnt find it in the examples

https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/FullExample.java#L75

There dont seem to be javadocs available???


Solution

  • 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.