How in kubernetes retrieve the value of the forwarded port?

6/25/2019

I am trying to create a service and then communicate with it externally.

V1Service service = new V1Service();
V1ServiceSpec spec = new V1ServiceSpec();
spec.addPortsItem(new V1ServicePort().port(25452));
service.setSpec(spec);
...

V1Service kubernetesService = api.createNamespacedService("ns", service, null);

When I type kubectl get services --all-namespaces command, I see that the port 25452 was forwarded to another automatically allocated port:

NAMESPACE TYPE        IP               EXTRENAL IP   PORT(S)
ns        NodePort    10.100.237.201   <none>        25452:32668/TCP

How using Java code can I retrieve the value of the new forwarded port (32668)?

-- mercury0114
java
kubernetes
service

1 Answer

6/25/2019

kubernetesService.getSpec().getPorts().get(0).getNodePort()

-- mercury0114
Source: StackOverflow