Connect to server in another Pod by <pod-name>:<port>

12/13/2019

I have a Pod my-pod-1 and a TCP server listening on a dynamically assigned port 12345 (the server then registers the port to other services). I know I could create a Service for the server, so that clients on other Pods can connect to it, but because the port is dynamically assigned, it is inconvenient. What I am thinking about is that, can other Pods reach out to the server through my-pod-1:12345 directly? What is the problem with this approach?

-- Dagang
kubernetes

1 Answer

12/13/2019

I am not sure if you can do <pod-name>:<port>. You could setup a Route to use an alias for your service, but the dynamically assigned port seems to prevent this... Unless you were to setup some service on a consistent port (8080?) that would return information regarding services -> assigned ports? That could very well be a terrible idea, just off the top of my head.

I do know that you can do <pod-IP>:<port> directly. You can use a central registry (have used Hazelcast for this) to register each service's name, IP address, port, status, and other needed data. Link: https://hazelcast.org/

You can configure the pod-IP, port and other information as environment variables on the pod via the Downward API: https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/

-- Loading...
Source: StackOverflow