Skip to main content
Knowledge Base

Best way to connect external client directly to pod on a range of ports?

Answer

A customer asked: > This is a general query regarding Kubernetes networking. What is the best approach to connect external client directly to the pod on a range of ports. Currently I have hostNetworking enabled and client connect through the node public address. Since the setup is in going to be in datacenter and avaialable machines are bigger, what is the best approach to efficiently connect to the pod/pods(with multiple public IPs if possible) over a range of ports.

This is generally handled by a `Service` or `Ingress`, depending on the type of application and how you are connecting to it. The easiest would be to use a `NodePort` type `Service` (which is better than using `hostNetworking` with the container because the container doesn’t have to have privileged access). This is best described in the blog post https://dzone.com/articles/kubernetes-exposing-services. The disadvantage of `NodePort` is that there is only a finite number of ports you can expose, which can easily run out depending on how many pods you want to expose. The next general approach is to use Ingress. The idea of Ingress is to run a load balancer application in your cluster which manages the routing, and expose that load balancer using the Service resource. If neither of those work, then typically you rely on a custom networking system for the Pods by implementing or deploying a CNI that allows you to allocate connectable IP addresses to the Pods. For example, in EKS, this is handled by using the VPC CNI which allocates IP addresses from the VPC to the Pods, which allow any service in the VPC to directly connect to the Pod’s IP. You will have to find what CNI options exist which will allow you to have a similar benefit in your data center.