我是 kubernetes 的新手,我想了解当我们让 NodePort 向外界公开端口时,kubernetes 网络是如何工作的。
假设我们的 kubernetes 集群中有两个节点,分别称为 Node1 和 Node2。我们在 Node2 上部署了一个 pod。
然后我们为这个 pod 创建一个 NodePort 服务。为简单起见,我们假设所有端口(targetPort、nodePort、端口)都是 3000。现在一个数据包到达 Node1 的端口 3000(没有部署 pod)。据我了解,iptables 规则指示此数据包进入 Node2 上的 pod。我的问题是 pod 在收到数据包时看到的 ip 是什么,以及它的响应数据包如何从外部世界返回到我们的初始客户端。
请您参考如下方法:
数据包在 Node1 处进行源 NAT。 Node1 将源 IP 替换为其 IP,将目标 IP 替换为 pod IP。 Pod 的回复发送给 Node1,Node1 将其发回给客户端。
(在文档中,pod 在节点 1 上运行,节点 2 是从客户端接收数据包的节点。)
- Client sends packet to node2:nodePort
- node2 replaces the source IP address (SNAT) in the packet with its own IP address
- node2 replaces the destination IP on the packet with the pod IP
- packet is routed to node 1, and then to the endpoint
- the pod’s reply is routed back to node2
- the pod’s reply is sent back to the client
client
\ ^
\ \
v \
node 1 <--- node 2
| ^ SNAT
| | --->
v |
endpoint




