Creating a Kubernetes Service in Java -


i using fabric8.io orchestrate application containers in kubernetes. looking create service manages pod label on port. there specific example of api this. couldnt find in examples

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

there dont seem javadocs available???

fabric8's kubernetes client using generated model , dsl has exact same structure as json , yaml configuration.

so in order create service instance 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 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 hold reference of service object , want create it, can inline 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 more compact json equivalent, because default value can committed , stuff selector can optionally inlined in single line.

this not applies service, every-single kubernetes/openshift resource.

if have json or yaml in place can load them providing input stream client:

service service = client.services().load(inputstream).get(); 

there more options here, directly creating service:

service newservice = client.services().load(inputstream).create(); 

it's remember structure same regardless of lang, format. pretty can inlined, tab completion in ide can really helpful.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -