Avoid reserving an unnecessary NodePort in a Service

8/6/2018

I have this NodePort service that exposes two ports:

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: my-service
  ports:
  - name: nginx-public
    port: 443
    targetPort: nginx-public
  - name: metrics
    port: 9200
    targetPort: metrics

The nginx-public port has to be exposed on the node because I'm using it with aws-alb-ingress-controller.

However, the other port for metrics is only used internally (from within the cluster) - it's picked up by prometheus-operator. I need to have it documented in the service spec, but I'd rather not reserve another port on the Node at the same time.

Is there a way to tell Kubernetes to only reserve one node port for this service?

I tried specifying nodePort: null in the port spec like this:

  ports:
  - name: nginx-public
    port: 443
    targetPort: nginx-public
  - name: metrics
    port: 9200
    targetPort: metrics
    nodePort: null

but according to describe service, seems like this has the same effect as omitting nodePort altogether, and a random port is still being reserved for metrics.

-- Kos
kubernetes

1 Answer

8/6/2018

you can define two services one selects nginx-public and the other selects metrics. use two different labels on the pods respectively.

-- P Ekambaram
Source: StackOverflow