<p>I am new to kubernetes and I am trying to understand how kubernetes networking works when we have NodePort exposing a port for the outside world. </p>

<p>Let's imagine we have two nodes in our kubernetes cluster, call it Node1 and Node2. We have one pod deployed on Node2. </p>

<p>Then we create a NodePort service for this pod. For simplicity lets assume all ports (targetPort, nodePort, port) are 3000. Now a packet comes to Node1's port 3000 (where there is no pod deployed). As far as I understand, iptables rules dictate that this packet goes to the pod on Node2. My question is what ip does the pod see when it receives the packet and how its response packet goes back to our initial client from outside world.</p>

Hovhannes  Vardanyan
<p>Packet is source NAT'd at Node1. Node1 replaces the source IP with its IP and destination IP as pod IP. Pod's reply is sent to Node1 and Node1 sends it back to client. </p>

<p><em><a href="https://kubernetes.io/docs/tutorials/services/source-ip/#source-ip-for-services-with-type-nodeport" rel="noreferrer">From the docs:</a></em></p>

<p>(In the docs pod is running on node1 and node 2 is the one receiving the packet from client.)</p>

<blockquote>
  <ul>
  <li>Client sends packet to node2:nodePort </li>
  <li>node2 replaces the source IP
  address (SNAT) in the packet with its own IP address</li>
  <li>node2 replaces
  the destination IP on the packet with the pod IP </li>
  <li>packet is routed to
  node 1, and then to the endpoint </li>
  <li>the pod’s reply is routed back to
  node2 </li>
  <li>the pod’s reply is sent back to the client</li>
  </ul>
</blockquote>

<pre><code>           client
             \ ^
              \ \
               v \
   node 1 &lt;--- node 2
    | ^   SNAT
    | |   ---&gt;
    v |
 endpoint
</code></pre>
