<p>Basically I have Following Hdfs Cluster setup using docker-compose:</p>
<p>Node 1 with IP: 192.168.1.1 having service deployed as below:</p>
<pre><code>Namenode1:9000
HMaster1: 8300
ZooKeeper1:1291
</code></pre>
<p>Node 2 with IP: 192.168.1.2 having service deployed as below:</p>
<pre><code>Namenode2:9000
ZooKeeper2:1291
</code></pre>
<p>How does Traefik / Ngnix - (Ingress Controllers) forwards request to two different services having configured with same port number?</p>

<p>There are several great tutorials on how ingress and load balancing works in kubernetes, e.g. <a href="https://medium.com/google-cloud/understanding-kubernetes-networking-ingress-1bc341c84078" rel="nofollow noreferrer">this one by Mark Betz</a>. As a general rule, it helps to think in terms of services and workloads instead of specific nodes where your workloads are running on.</p>
<p>A workload deployed in Kubernetes (a so called <code>Pod</code>) has its own internal IP address, called a <code>ClusterIP</code>. That pod can have one or more ports open, just on that pod-owned ip address.</p>
<p>If you now have several pods to distribute the load, e.g. like 5 web server processes or backend logic, it would be hard for a client (inside the cluster) to keep track of all those pod IPs, because they also change when a pod is updated or just restarted due to a crash. This is why Kubernetes has a so called concept of <code>services</code>. Those provide a stable DNS name and IP which then transparently &quot;forwards&quot; to one of the healthy pods. So your client only needs to know the DNS name and not keep track of the specific pod IPs.</p>
<p>If you now want to expose such a <code>service</code> to the public, there are different ways. Either you set your service to <code>type: LoadBalancer</code> which then sets up some load balancer infrastructure on your cloud provider and routes traffic to the nodes and then to the pods - or - you already have an ingress controller in place and just define the routing based on host names and paths. An ingress controller itself is such a loadbalanced service with an attached cloud load balancer and also has some pods (with e.g. a traefik or nginx container) which then route your packets accordingly.</p>
<p>So coming back to your initial question: If you want to expose a service with several pods of the same kind, then you would first create a <code>Service</code> resource that matches your <code>Pod</code>s using the <code>selector</code> and then you create one single <code>ingress</code> resource that provides a hostname/path and references this service. The ingress controller will pick up those ingress resources and configure the traefik or nginx accordingly. The ingress controller doesn't really care about the host IPs and port numbers, because it acts on the internal kubernetes ClusterIPs, so you even don't need (and shouldn't) expose such a service directly when you have an ingress in place.</p>
<p>I hope this answers your question regarding exposing two workloads over an ingress controller. For details, check <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/" rel="nofollow noreferrer">the Kubernetes docs on Ingresses</a>. Based on the services you named (zookeeper, hdfs) load balancing and ingresses might not be what you need for that case. Zookeeper instances should be internal in most cases and need to be adressed individually, so you might want to check out <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="nofollow noreferrer">headless services</a>, for this use case. Also check the Kubernetes docs for <a href="https://kubernetes.io/docs/tutorials/stateful-application/zookeeper/" rel="nofollow noreferrer">a way to run zookeeper</a>.</p>
