填空秒懂網(wǎng)站女生seo專員很難嗎為什么
在 Kubernetes 中,Endpoint 是一種 API 對象,它用于表示集群內(nèi)某個 Service 的具體網(wǎng)絡地址。換句話說,它連接到一組由 Service 選擇的 Pod,從而使它們能夠提供服務。每個 Endpoint 對象都與相應的 Service 對象具有相同的名稱,并屬于相同的命名空間。
每個 Endpoint 對象都會包含一組 Pod 的 IP 地址和端口,作為 Service 的后端。當 Service 被定義時,Kubernetes 控制器會監(jiān)視匹配 Service 選擇器的 Pod,并自動更新 Endpoint 對象。這樣,當新的 Pod 被創(chuàng)建或現(xiàn)有的 Pod 被刪除時,Endpoint 對象將會自動得到更新。
Endpoint 對象的定義形態(tài)如下:
kind: Endpoints
apiVersion: v1
metadata:name: my-service
subsets:- addresses:- ip: 192.0.2.42ports:- port: 9376
其中,metadata.name
是 Endpoint 的名稱,subsets.addresses.ip
是后端 Pod 的 IP 地址,subsets.ports.port
是要暴露的端口。
然而,請注意,除非有特別的需求,否則通常不需要手動創(chuàng)建或管理 Endpoints 對象。當你使用 Service 來選擇 Pod 時,Endpoints 對象會自動被創(chuàng)建和管理。也就是說,只要 Service 選擇器能夠匹配到 Pod,Kubernetes 就會自動將匹配的 Pod 的 IP 地址和端口添加到 Endpoints 對象中。
總的來說,Endpoint 是 Kubernetes Service 的一個重要組成部分,負責維護一組提供服務的 Pod 的 IP 地址和端口信息,使得 Service 能夠通過網(wǎng)絡將請求路由到正確的 Pod。
例如:
apiVersion: v1
kind: Service
metadata:name: nginx-svclabels:app: nginx-po
spec:ports:- port: 80targetPort: 80name: webselector: # 匹配哪些pod會被該service代理app: nginx-po #所有匹配到這些標簽的pod都可以通過該service進行訪問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
這個yaml會創(chuàng)建一個 service nginx-svc,它所對應的selector選擇器是nginx-po,也就是說所有l(wèi)abel為 nginx-po 的pod的端口就會被自動加入到endpoint中。
從下圖可以看到所有l(wèi)abel為nginx-po的pod的ip都被加入到和service同名的endpoint中。
將一個新創(chuàng)建的pod加入到已有的endpoint中去
需要將新創(chuàng)建的pod的labels設置成已有svc一樣,port端口也要和已有endpoint一樣
注意:如果只設置了相同的label,而沒有設置port,這種情況下endpoint是識別不到的。
一個已有的svc關聯(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新增了一個ip:port信息。