1

I am creating a service in the following way

io.fabric8.kubernetes.api.model.Service myService = new ServiceBuilder()
        .withNewMetadata()
        .withName(svcName).addToLabels(svcLabelKey,svcLabelValue)
        .endMetadata()
        .withNewSpec().addToSelector(specSelectorKey, specSelectorValue).withType(svcType)
        .addNewPort()
        .withProtocol(protocol)
        .withPort(port)
        .endPort()
        .endSpec()
        .build();
        client.services().inNamespace(namespace).create(myService);
        return myService;

How do I get the nodePort it returns from this json?

  {
 "metadata": {
        "name": "my-svc",
        "namespace": "myns",
        "selfLink": "",
        "uid": "6cb0f222-7e57-11e5-96f2-005056976c6f",
        "resourceVersion": "1016121",
        "creationTimestamp": "2015-10-29T16:09:25Z",
        "labels": {
          "name": ""
        }
      },
      "spec": {
        "ports": [
          {
            "protocol": "TCP",
            "port": 80,
            "targetPort": 80,
            "nodePort": 20430
          }
        ],
        "selector": {
          "name": "p"
        },
        "clusterIP": "10.254.181.34",
        "type": "LoadBalancer",
        "sessionAffinity": "None"
      },
      "status": {
        "loadBalancer": {}
      }
    },

Are there examples to fetch teh nodeport after a service is created? I can get the service port but not the node port that is added later after creation

4

1 回答 1

3

是内部定义的nodePort属性io.fabric8.kubernetes.api.model.ServicePort

您可以获取服务实例,例如:

Service srv = client.services().inNamespace(namespace).withName("myid").get();

然后您可以访问该属性,例如:

Integer nodePort = srv.getSpec().getPorts().get(0).getNodePort();

上面的代码将获取nodePort为服务定义的第一个端口。(见get(0))。如果您有更多端口,则必须迭代并选择所需的端口。

我希望这有帮助。

于 2015-11-02T13:44:20.077 回答