kubernetes: loadbalancer service with a nodeport

1/21/2021

I have a LoadBalancer service which exposes 3300 port outside the cluster. I want to open a new port for internal communication so that other pods can talk to this service, but this port should not be exposed outside the cluster.
Basically, the communication looks like this:

                                   my-svc:3301
    pod/my-app  <--- svc/my-svc  <------------- pod/my-new-app
                      ^
                      | ext-ip:3300
                      |
                   outside_world
apiVersion: v1
kind: Service
metadata:
  name: my-svc
spec:
  type: LoadBalancer
  ports:
  - name: my-port
    protocol: "TCP"
    port: 3300
    targetPort: 3300
  selector:
    app: my-app

Is there a way to include the new port inside this service itself? Do I have to create another service of type NodePort?

-- subtleseeker
kubernetes

2 Answers

1/21/2021

Creating a new service of type ClusterIP(default service type) with the port needed for internal communication as mentioned by the other answer.

apiVersion: v1
kind: Service
metadata:
  name: my-svc-internal
spec:
  type: ClusterIP
  ports:
  - name: my-port
    protocol: "TCP"
    port: 3301
    targetPort: 3301
  selector:
    app: my-app
-- Malathi
Source: StackOverflow

1/21/2021

If the "internal" and "external" communication paths use different ports, you need a separate (ClusterIP) Service. There's no way to say you want a Service to create a LoadBalancer (or a NodePort) but only for certain of the service ports.

If both can use the same port, then you can just point other pods at http://my-svc:3300. Even if it is a LoadBalancer (or NodePort) Service, it can be called using its name: and port: in the same way as a ClusterIP Service.

-- David Maze
Source: StackOverflow