<p>I have a question related to Kubernetes networking. </p>

<p>I have a microservice (say <em>numcruncherpod</em>) running in a pod which is serving requests via port <code>9000</code>, and I have created a corresponding Service of type <code>NodePort</code> (<em>numcrunchersvc</em>) and node port which this service is exposed is <code>30900</code>. </p>

<p>My cluster has 3 nodes with following IPs: </p>

<ol>
<li>192.168.201.70, </li>
<li>192.168.201.71</li>
<li>192.168.201.72</li>
</ol>

<p>I will be routing the traffic to my cluster via reverse proxy (nginx). As I understand in nginx I need to specify IPs of all these cluster nodes to route the traffic to the cluster, is my understanding correct ? </p>

<p>My worry is since nginx won't have knowledge of cluster it might not be a good judge to decide the cluster node to which the traffic should be sent to. So is there a better way to route the traffic to my kubernetes cluster ? </p>

<p><em>PS: I am not running the cluster on any cloud platform.</em></p>

<p>This answer is a little late, and a little long, so I ask for forgiveness before I begin. :)</p>

<p>For people not running kubernetes clusters on Cloud Providers there are 4 distinct options for exposing services running inside the cluster to the world outside.</p>

<ol>
<li><p>Service of <code>type: NodePort</code>. This is the simplest and default. Kubernetes assigns a random port to your service. Every node in the cluster listens for traffic to this particular port and then forwards that traffic to any one of the pods backing that service. This is usually handled by kube-proxy, which leverages iptables and load balances using a round-robin strategy. Typically since the UX for this setup is not pretty, people often add an external "proxy" server, such as HAProxy, Nginx or httpd to listen to traffic on a single IP and forward it to one of these backends. This is the setup you, OP, described.</p></li>
<li><p>A step up from this would be using a Service of <code>type: ExternalIP</code>. This is identical to the <code>NodePort</code> service, except it also gets kubernetes to add an additional rule on all kubernetes nodes that says "All traffic that arrives for destination IP ==  must <em>also</em> be forwarded to the pods". This basically allows you to specify any arbitrary IP as the "external IP" for the service. As long as traffic destined for that IP reaches one of the nodes in the cluster, it will be routed to the correct pod. Getting that traffic to any of the nodes however, is your responsibility as the cluster administrator. The advantage here is that you no longer have to run an haproxy/nginx setup, if you specify the IP of one of the physical interfaces of one of your nodes (for example one of your master nodes). Additionally you cut down the number of hops by one.</p></li>
<li><p>Service of <code>type: LoadBalancer</code>. This service type brings baremetal clusters at parity with cloud providers. A fully functioning loadbalancer provider is able to  select IP from a pre-defined pool, automatically assign it to your service and advertise it to the network, assuming it is configured correctly.  This is the most "seamless" experience you'll have when it comes to kubernetes networking on baremetal. Most of LoadBalancer provider implementations use BGP to talk and advertise to an upstream L3 router. Metallb and kube-router are the two FOSS projects that fit this niche.</p></li>
<li><p>Kubernetes Ingress. If your requirement is limited to L7 applications, such as REST APIs, HTTP microservices etc. You can setup a single Ingress provider (nginx is one such provider) and then configure ingress resources for all your microservices, instead of service resources. You deploy your ingress provider and make sure it has an externally available and routable IP (you can pin it to a master node, and use the physical interface IP for that node for example). The advantage of using ingress over services is that ingress objects understand HTTP mircoservices natively and you can do smarter health checking, routing and management.</p></li>
</ol>

<p>Often people combine one of (1), (2), (3) with (4), since the first 3 are L4 (TCP/UDP) and (4) is L7. So things like URL path/Domain based routing, SSL Termination etc is handled by the ingress provider and the IP lifecycle management and routing is taken care of by the service layer.</p>

<p>For your use case, the ideal setup would involve:</p>

<ol>
<li>A deployment for your microservice, with health endpoints on your pod</li>
<li>An Ingress provider, so that you can tweak/customize your routing/load-balancing as well as use for SSL termination, domain matching etc.</li>
<li>(optional): Use a <code>LoadBalancer</code> provider to front your Ingress provider, so that you don't have to manually configure your Ingress's networking.</li>
</ol>
