title: Mapping ports in Kubernetes
body: <p>I'm trying to wrap my head around how kubernetes (k8s) utilises ports. Having read the API documentation as well as the available docs, I'm not sure how the port mapping and port flow works.</p>
  
  <p>Let's say I have three containers with an externally hosted database, my k8s cluster is three on-prem CoreOS nodes, and there is a software-defined load balancer in front of all three nodes to forward traffic to all three nodes on ports 3306 and 10082.</p>
  
  <ol>
  <li>Container A utilises incoming port 8080, needs to talk to Container B and C, but does not need external access. It is defined with Replication Controller A that has 1 replica.</li>
  <li>Container B utilises incoming port 8081 to talk to Container A and C, but needs to access the external database on port 3306. It is defined with Replication Controller B that has 2 replicas.</li>
  <li>Container C utilises incoming port 8082, needs to talk to Container A and B, but also needs external access on port 10082 for end users. It is defined with Replication Controller C that has 3 replicas.</li>
  </ol>
  
  <p>I have three services to abstract the replication controllers.</p>
  
  <ol>
  <li>Service A selects Replication Controller A and needs to forward incoming traffic on port 9080 to port 8080.</li>
  <li>Service B selects Replication Controller B and needs to forward incoming traffic on ports 9081 and 3306 to ports 8081 and 3306. </li>
  <li>Service C selects Replication Controller C and needs to forward incoming traffic on port 9082 to port 8082.</li>
  </ol>
  
  <p>I have one endpoint for the external database, configured to on port 3306 with an IPv4 address.</p>
  
  <p>Goals:</p>
  
  <ul>
  <li>Services need to abstract Replication Controller ports.</li>
  <li>Service B needs to be able to be reached from an external system on port 3306
  on all nodes.</li>
  <li>Service C needs to be able to be reached from an external system on port 10082
  on all nodes.</li>
  </ul>
  
  <p>With that:</p>
  
  <ol>
  <li>When would I use each of the types of ports; i.e. <code>port</code>, <code>targetPort</code>, <code>nodePort</code>, etc.?</li>
  </ol>