kubernetes中port、target port、node port的對(duì)比分析,以及kube-proxy代理
2016年01月10日 18:55:03?xinghun_4?閱讀數(shù):40031
這幾個(gè)port的概念很容易混淆,比如創(chuàng)建如下service:
apiVersion: v1kind: Servicemetadata:? labels:? ? name: app1? name: app1? namespace:defaultspec:? type: NodePort? ports:? -port: 8080? ? targetPort: 8080? ? nodePort: 30062selector:? ? name: app1
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?是提供給集群內(nèi)部客戶訪問(wèn)service的入口。
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提供給集群外部客戶訪問(wèn)service入口的一種方式(另一種方式是LoadBalancer),所以,<nodeIP>:nodePort 是提供給集群外部客戶訪問(wèn)service的入口。
The port on the pod that the service should proxy traffic to.
targetPort很好理解,targetPort是pod上的端口,從port和nodePort上到來(lái)的數(shù)據(jù)最終經(jīng)過(guò)kube-proxy流入到后端pod的targetPort上進(jìn)入容器。
總的來(lái)說(shuō),port和nodePort都是service的端口,前者暴露給集群內(nèi)客戶訪問(wèn)服務(wù),后者暴露給集群外客戶訪問(wèn)服務(wù)。從這兩個(gè)端口到來(lái)的數(shù)據(jù)都需要經(jīng)過(guò)反向代理kube-proxy流入后端pod的targetPod,從而到達(dá)pod上的容器內(nèi)。
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.
當(dāng)service有了port和nodePort之后,就可以對(duì)內(nèi)/外提供服務(wù)。那么其具體是通過(guò)什么原理來(lái)實(shí)現(xiàn)的呢?奧妙就在kube-proxy在本地node上創(chuàng)建的iptables規(guī)則。
Kube-Proxy 通過(guò)配置 DNAT 規(guī)則(從容器出來(lái)的訪問(wèn),從本地主機(jī)出來(lái)的訪問(wèn)兩方面),將到這個(gè)服務(wù)地址的訪問(wèn)映射到本地的kube-proxy端口(隨機(jī)端口)。然后 Kube-Proxy 會(huì)監(jiān)聽(tīng)在本地的對(duì)應(yīng)端口,將到這個(gè)端口的訪問(wèn)給代理到遠(yuǎn)端真實(shí)的 pod 地址上去。
kube-proxy會(huì)在nat表里生成4個(gè)chain,分別如上所示(主要是從容器出來(lái)的訪問(wèn),從本地主機(jī)出來(lái)的訪問(wèn)兩方面)。
創(chuàng)建service以后,kube-proxy會(huì)自動(dòng)在集群里的node上創(chuàng)建以下兩條規(guī)則:
KUBE-PORTALS-CONTAINER
KUBE-PORTALS-HOST
如果是NodePort方式,還會(huì)額外生成兩條:
KUBE-NODEPORT-CONTAINER
KUBE-NODEPORT-HOST
主要將由網(wǎng)絡(luò)接口到來(lái)的通過(guò)服務(wù)集群入口:port的請(qǐng)求重定向到本地kube-proxy端口(隨機(jī)端口)的映射,即來(lái)自本地容器的服務(wù)訪問(wèn)請(qǐng)求;
注:我認(rèn)為,這種情況的網(wǎng)絡(luò)包不可能來(lái)自外部網(wǎng)絡(luò),因?yàn)閏luster ip是個(gè)virtual ip,外部網(wǎng)絡(luò)中不存在這樣的路由將該數(shù)據(jù)包發(fā)送到本機(jī);所以該請(qǐng)求只能來(lái)自本地容器,從本地容器出來(lái)的訪問(wèn),服務(wù)訪問(wèn)請(qǐng)求是通過(guò)本地容器虛擬網(wǎng)卡輸入到本地網(wǎng)絡(luò)接口的。
主要將由網(wǎng)絡(luò)接口到來(lái)的通過(guò)服務(wù)集群外部入口:nodePort的請(qǐng)求重定向到本地kube-proxy端口(隨機(jī)端口)的映射;即來(lái)自k8s集群外部網(wǎng)絡(luò)的服務(wù)訪問(wèn)請(qǐng)求,可以來(lái)自本機(jī)容器,也可以來(lái)自其他node的容器,還可以來(lái)自其他node的進(jìn)程;
主要將該node本地進(jìn)程通過(guò)服務(wù)集群入口:port的請(qǐng)求重定向到本地kube-proxy端口(隨機(jī)端口)的映射。
主要將該node本地進(jìn)程通過(guò)服務(wù)集群外部入口<node ip>:nodePort的請(qǐng)求重定向到本地kube-proxy端口(隨機(jī)端口)的映射。
不管是通過(guò)集群內(nèi)部服務(wù)入口<cluster ip>:port還是通過(guò)集群外部服務(wù)入口<node ip>:nodePort的請(qǐng)求都將重定向到本地kube-proxy端口(隨機(jī)端口)的映射,然后將到這個(gè)kube-proxy端口的訪問(wèn)給代理到遠(yuǎn)端真實(shí)的 pod 地址上去。
歡迎一起討論