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

來源:互聯網
上載者:User

標籤:article   blog   traffic   select   water   views   which   from   metadata   

轉: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的連接埠,前者暴露給叢集內客戶訪問服務,後者暴露給叢集外客戶訪問服務。從這兩個連接埠到來的資料都需要經過反向 Proxykube-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反向 Proxy

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

 

一個例子

另外一個例子

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.