填空秒懂網(wǎng)站女生seo專(zhuān)員很難嗎為什么
在 Kubernetes 中,Endpoint 是一種 API 對(duì)象,它用于表示集群內(nèi)某個(gè) Service 的具體網(wǎng)絡(luò)地址。換句話說(shuō),它連接到一組由 Service 選擇的 Pod,從而使它們能夠提供服務(wù)。每個(gè) Endpoint 對(duì)象都與相應(yīng)的 Service 對(duì)象具有相同的名稱(chēng),并屬于相同的命名空間。
每個(gè) Endpoint 對(duì)象都會(huì)包含一組 Pod 的 IP 地址和端口,作為 Service 的后端。當(dāng) Service 被定義時(shí),Kubernetes 控制器會(huì)監(jiān)視匹配 Service 選擇器的 Pod,并自動(dòng)更新 Endpoint 對(duì)象。這樣,當(dāng)新的 Pod 被創(chuàng)建或現(xiàn)有的 Pod 被刪除時(shí),Endpoint 對(duì)象將會(huì)自動(dòng)得到更新。
Endpoint 對(duì)象的定義形態(tài)如下:
kind: Endpoints
apiVersion: v1
metadata:name: my-service
subsets:- addresses:- ip: 192.0.2.42ports:- port: 9376
其中,metadata.name
是 Endpoint 的名稱(chēng),subsets.addresses.ip
是后端 Pod 的 IP 地址,subsets.ports.port
是要暴露的端口。
然而,請(qǐng)注意,除非有特別的需求,否則通常不需要手動(dòng)創(chuàng)建或管理 Endpoints 對(duì)象。當(dāng)你使用 Service 來(lái)選擇 Pod 時(shí),Endpoints 對(duì)象會(huì)自動(dòng)被創(chuàng)建和管理。也就是說(shuō),只要 Service 選擇器能夠匹配到 Pod,Kubernetes 就會(huì)自動(dòng)將匹配的 Pod 的 IP 地址和端口添加到 Endpoints 對(duì)象中。
總的來(lái)說(shuō),Endpoint 是 Kubernetes Service 的一個(gè)重要組成部分,負(fù)責(zé)維護(hù)一組提供服務(wù)的 Pod 的 IP 地址和端口信息,使得 Service 能夠通過(guò)網(wǎng)絡(luò)將請(qǐng)求路由到正確的 Pod。
例如:
apiVersion: v1
kind: Service
metadata:name: nginx-svclabels:app: nginx-po
spec:ports:- port: 80targetPort: 80name: webselector: # 匹配哪些pod會(huì)被該service代理app: nginx-po #所有匹配到這些標(biāo)簽的pod都可以通過(guò)該service進(jìn)行訪問(wèn)type: NodePort---apiVersion: apps/v1
kind: StatefulSet
metadata:name: web
spec:serviceName: "nginx"replicas: 2selector:matchLabels:app: nginx-potemplate:metadata:labels:app: nginx-pospec:containers:- name: nginximage: nginx:1.7.9ports:- containerPort: 80name: webupdateStrategy:rollingUpdate:partition: 0type: RollingUpdate
這個(gè)yaml會(huì)創(chuàng)建一個(gè) service nginx-svc,它所對(duì)應(yīng)的selector選擇器是nginx-po,也就是說(shuō)所有l(wèi)abel為 nginx-po 的pod的端口就會(huì)被自動(dòng)加入到endpoint中。
從下圖可以看到所有l(wèi)abel為nginx-po的pod的ip都被加入到和service同名的endpoint中。
將一個(gè)新創(chuàng)建的pod加入到已有的endpoint中去
需要將新創(chuàng)建的pod的labels設(shè)置成已有svc一樣,port端口也要和已有endpoint一樣
注意:如果只設(shè)置了相同的label,而沒(méi)有設(shè)置port,這種情況下endpoint是識(shí)別不到的。
一個(gè)已有的svc關(guān)聯(lián)的endpoint
newpod.yal
apiVersion: v1
kind: Pod
metadata:labels:app: gatename: toolbox-0namespace: default
spec:containers:- image: toolbox:v1.0.0imagePullPolicy: IfNotPresentname: toolboxports:- containerPort: 10000name: test_portprotocol: TCP
創(chuàng)建pod之后,可以看到endpoints新增了一個(gè)ip:port信息。