1. 程式人生 > >kubernetes中port、target port、node port的對比分析,以及kube-proxy代理

kubernetes中port、target port、node port的對比分析,以及kube-proxy代理

ans toc contain exp red lec adb service 接口

轉:http://blog.csdn.net/xinghun_4/article/details/50492041

容器網絡實例

技術分享

服務中的3個端口設置

這幾個port的概念很容易混淆,比如創建如下service:

[plain] view plain copy
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. name: app1
  6. name: app1
  7. namespace: default
  8. spec:
  9. type: NodePort
  10. ports:
  11. - <strong>port: 8080
  12. targetPort: 8080
  13. nodePort: 30062</strong>
  14. selector:
  15. name: app1

port

The port that the service is exposed on the service’s cluster ip (virsual ip). Port is the service port which is accessed by others with cluster ip.

即,這裏的port表示:service暴露在cluster ip上的端口,<cluster ip>:port

是提供給集群內部客戶訪問service的入口。

nodePort

On top of having a cluster-internal IP, expose the service on a port on each node of the cluster (the same port on each node). You‘ll be able to contact the service on any<nodeIP>:nodePortaddress. So nodePort is alse the service port which can be accessed by the node ip by others with external ip.

首先,nodePort是kubernetes提供給集群外部客戶訪問service入口的一種方式(另一種方式是LoadBalancer),所以,<nodeIP>:nodePort 是提供給集群外部客戶訪問service的入口。

targetPort

The port on the pod that the service should proxy traffic to.

targetPort很好理解,targetPort是pod上的端口,從port和nodePort上到來的數據最終經過kube-proxy流入到後端pod的targetPort上進入容器。

port、nodePort總結

總的來說,port和nodePort都是service的端口,前者暴露給集群內客戶訪問服務,後者暴露給集群外客戶訪問服務。從這兩個端口到來的數據都需要經過反向代理kube-proxy流入後端pod的targetPod,從而到達pod上的容器內。

When a client connects to the VIP the iptables rule kicks in, and redirects the packets to the serviceproxy‘s own port (random port). The service proxy chooses a backend, and starts proxying traffic from the client to the backend. This means that service owers can choose any port they want without risk of collision.The same basic flow executes when traffic comes in through a nodePort or through a LoadBalancer, though in those cases the client IP does get altered.

kube-proxy與iptables

當service有了port和nodePort之後,就可以對內/外提供服務。那麽其具體是通過什麽原理來實現的呢?奧妙就在kube-proxy在本地node上創建的iptables規則。

Kube-Proxy 通過配置 DNAT 規則(從容器出來的訪問,從本地主機出來的訪問兩方面),將到這個服務地址的訪問映射到本地的kube-proxy端口(隨機端口)。然後 Kube-Proxy 會監聽在本地的對應端口,將到這個端口的訪問給代理到遠端真實的 pod 地址上去。

技術分享

kube-proxy會在nat表裏生成4個chain,分別如上所示(主要是從容器出來的訪問,從本地主機出來的訪問兩方面)。

創建service以後,kube-proxy會自動在集群裏的node上創建以下兩條規則:
KUBE-PORTALS-CONTAINER
KUBE-PORTALS-HOST
如果是NodePort方式,還會額外生成兩條:
KUBE-NODEPORT-CONTAINER
KUBE-NODEPORT-HOST

KUBE-PORTALS-CONTAINER

主要將由網絡接口到來的通過服務集群入口<cluster ip>:port的請求重定向到本地kube-proxy端口(隨機端口)的映射,即來自本地容器的服務訪問請求

註:我認為,這種情況的網絡包不可能來自外部網絡,因為cluster ip是個virtual ip,外部網絡中不存在這樣的路由將該數據包發送到本機;所以該請求只能來自本地容器,從本地容器出來的訪問,服務訪問請求是通過本地容器虛擬網卡輸入到本地網絡接口的。

KUBE-NODEPORT-CONTAINER

主要將由網絡接口到來的通過服務集群外部入口<node ip>:nodePort的請求重定向到本地kube-proxy端口(隨機端口)的映射;即來自k8s集群外部網絡的服務訪問請求,可以來自本機容器,也可以來自其他node的容器,還可以來自其他node的進程

KUBE-PORTALS-HOST

主要將該node本地進程通過服務集群入口<cluster ip>:port的請求重定向到本地kube-proxy端口(隨機端口)的映射。

KUBE-NODEPORT-HOST

主要將該node本地進程通過服務集群外部入口<node ip>:nodePort的請求重定向到本地kube-proxy端口(隨機端口)的映射。

kube-proxy反向代理

不管是通過集群內部服務入口<cluster ip>:port還是通過集群外部服務入口<node ip>:nodePort的請求都將重定向到本地kube-proxy端口(隨機端口)的映射,然後將到這個kube-proxy端口的訪問給代理到遠端真實的 pod 地址上去。

一個例子

技術分享

另外一個例子

技術分享

kubernetes中port、target port、node port的對比分析,以及kube-proxy代理