参考原文 https://kubernetes.io/docs/concepts/cluster-administration/networking/#kubernetes-model
网络是Kubernetes里面的核心,在制定网络方案的时候有4个问题需要解决:
1、高度互联的容器之间,怎么通信?
pod以外的容器通过pods互联,pod以内的容器通过localhost互联
2、pod之间如何通信?
下面会详细阐述。
3、pod跟服务之间如何通信?
详见服务
4、外界跟服务之间如何通信?
详见服务
每个pod都有独立的ip地址,这个可以看做是一个虚拟机。实现Kubernetes的网络模型,需要满足以下两个条件:
1、pod跟所有的其他在或者不在同一个节点上的pod都必须可以直接通信,不需要借助NAT
2、节点上的agent进程(比如deamon、kubelet)都必须可以跟这个节点上的所有pod通信。
Kubernetes IP addresses exist at the Pod
scope - containers within a Pod
share their network namespaces - including their IP address. This means that containers within a Pod
can all reach each other’s ports on localhost
. This also means that containers within a Pod
must coordinate port usage, but this is no different from processes in a VM. This is called the “IP-per-pod” model.
Kubernetes上的ip地址是pod维度的,pod内部的容器,是通过共享网络命名空间(当然也包括ip地址)实现通信的,也就是说pod内的容器通过localhost访问彼此的端口来通信,这个就跟我们现在在一个宿主机上运行多个进程,通过不同的端口来决定访问哪个应用是一样的。这个叫IP-per-pod模式。