title: NodeIP, ClusterIP and LoadBalancer in Kubernetes
body: <p>My question is built on the question and answers from this question - <a href="https://stackoverflow.com/questions/41509439/whats-the-difference-between-clusterip-nodeport-and-loadbalancer-service-types">What&#39;s the difference between ClusterIP, NodePort and LoadBalancer service types in Kubernetes?</a></p>
  <p>The question might not be well-formed for some of you.</p>
  <p>I am trying to understand the differences between <code>clusterIP</code>, <code>nodePort</code> and <code>Loadbalancer</code> and when to use these with an example. I suppose that my understanding of the following concept is correct
  K8s consists of the following components</p>
  <ul>
  <li>Node - A VM or physical machine. Runs kubectl and docker process</li>
  <li>Pod - unit which encapsulates container(s) and volumes (storage). If a pod contains multiple containers then shared volume could be the way for process communication</li>
  <li>Node can have one or multiple pods. Each pod will have its own IP</li>
  <li>Cluster - replicas of a Node. Each node in a cluster will contain same pods (instances, type)</li>
  </ul>
  <p>Here is the scenario:</p>
  <p>My application has a <code>web server</code> (always returning 200OK) and a <code>database</code> (always returning the same value) for simplicity. Also, say I am on <code>GCP</code> and I make images of <code>webserver</code> and of the <code>database</code>. Each of these will be run in their own respective <code>pods</code> and will have 2 replicas.</p>
  <p>I suppose I'll have two clusters (<code>cluster-webserver (node1-web (containing pod1-web), node2-web (containing pod2-web))</code> and <code>cluster-database (node1-db (containing pod1-db), node2-db (containing pod2-db))</code>. Each node will have its own <code>ip</code> address (<code>node1-webip, node2-webip, node1-dbip, node2-dbip</code>)</p>
  <p>A client application (browser) should be able to access the web application from outside <code>web</code> cluster but the <code>database</code> shouldn't be accessible from outside <code>database</code> cluster. However <code>web</code> nodes should be able to access <code>database</code> nodes)</p>
  <ul>
  <li>Question 1 - Am I correct that if I create a service for <code>web</code> (<code>webServiceName</code>) and a service for <code>database</code> then by default, I'll get only <code>clusterIP</code> and a <code>port</code> (or <code>targetPort</code>).</li>
  <li>Question 1.2 - Am I correct that <code>clusterIP</code> is an <code>IP</code> assigned to a <code>pod</code>, not the <code>node</code> i.e. in my example, clusterIP gets assigned to <code>pod1-web</code>, not <code>node1-web</code> even though <code>node1</code> has only <code>pod1</code>.</li>
  <li>Question 1.3 - Am I correct that as <code>cluster IP</code> is accessible from only within the cluster, <code>pod1-web</code> and <code>pod2-web</code> can talk to each other and <code>pod1-db</code> and <code>pod2-db</code> can talk to each other using <code>clusterIP/dns:port</code> or <code>clusterIP/dns:targetPort</code> but <code>web</code> can't talk to <code>database</code> (and vice versa) and external client can't talk to web? Also, the <code>nodes</code> are not accessible using the <code>cluster IP</code>.</li>
  <li>Question 1.4 - Am I correct that <code>dns</code> i.e. <code>servicename.namespace.svc.cluster.local</code> would map the <code>clusterIP</code>?</li>
  <li>Question 1.5 - For which type of applications I might use only <code>clusterIP</code>? Where multiple instances of an application need to communicate with each other (eg master-slave configuration)?</li>
  </ul>
  <p>If I use <code>nodePort</code> then <code>K8s</code> will open a <code>port</code> on each of the <code>node</code> and will forward <code>nodeIP/nodePort</code> to <code>cluster IP (on pod)/Cluster Port</code></p>
  <ul>
  <li>Question 2 - Can <code>web</code> nodes now access <code>database</code> nodes using <code>nodeIP:nodePort</code> which will route the traffic to <code>database's</code> <code>clusterIP (on pod):clusterport/targertPort</code>? ( I have read that clusterIP/dns:nodePort will not work).</li>
  <li>Question 2.1 - How do I get a <code>node's</code> <code>IP</code>? Is <code>nodeIP</code> the <code>IP</code> I'll get when I run <code>describe pods</code> command?</li>
  <li>Question 2.2 - Is there a <code>dns</code> equivalent for the <code>node IP</code> as <code>node IP</code> could change during failovers. Or does <code>dns</code> now resolve to the node's IP instead of <code>clusterIP</code>?</li>
  <li>Question 2.3 - I read that <code>K8s</code> will create <code>endpoints</code> for each <code>service</code>. Is <code>endpoint</code> same as <code>node</code> or is it same as <code>pod</code>? If I run <code>kubectl describe pods</code> or <code>kubectl get endpoints</code>, would I get same <code>IPs</code>)?</li>
  </ul>
  <p>As I am on <code>GCP</code>, I can use <code>Loadbalancer</code> for <code>web</code> cluster to get an external IP. Using the external IP, the client application can access the <code>web</code> service</p>
  <p>I saw this configuration for a <code>LoadBalancer</code></p>
  <pre><code>spec:
    selector:
      app: MyApp
    ports:
      - protocol: TCP
        port: 80
        targetPort: 9376
    type: LoadBalancer
  </code></pre>
  <p>Questi</p>
  <ul>
  <li>Question 3 - Is it exposing an external <code>IP</code> and port <code>80</code> to outside world? What would be the value of <code>nodePort</code> in this case?</li>
  </ul>