在《k8s~Endpoints的使用》文章中,我們知道了如何通過Endpoints來訪問外部資源,而本篇主要說一下它的另一個用法,service的負載均衡。
service的負載pod
注意:service轉發到endpoints只是實現了負載均衡,並沒有實現健康檢查,也就是當,說endpoint指向的節點宕機后,service也是會將流量轉向到這些節點的;如果希望實現監控檢查,可以使用envoy。
service里有個endpoints,它里面存儲所有pod的地址信息,即請求從外部打到k8s的service的nodeport端口之后(沒有使用clusterIP虛擬ip,而是使用nodeport的方式,ip地址是節點宿主機的,端口也是宿主機的),它會將endpoints列表轉發,而endpionts里存儲的就是每個pod的ip地址,最后落到容器里。
-
通過外部負載均衡器把請求打到 k8s的service里
-
service通過endpoints進行負載,把請求打到具體pod上面
-
查看service信息,里面只顯示可用的endpoint信息
[root@elasticsearch02 service-spi]# kubectl describe svc keycloak -n=cas
Name: keycloak
Namespace: pkulaw-cas
Labels: <none>
Annotations: field.cattle.io/publicEndpoints:
[{"addresses":["192.168.3.2"],"port":32080,"protocol":"TCP","serviceName":"cas:keycloak","allNodes":true}]
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"keycloak","namespace":"cas"},"spec":{"ports":[{"name":"htt...
Selector: app=keycloak
Type: NodePort
IP: 10.43.120.224
Port: http 8080/TCP
TargetPort: 8080/TCP
NodePort: http 32080/TCP
Endpoints: 10.42.1.180:8080,10.42.3.120:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
- 查看endpoints信息,會區分可用和不可用的Subsets
[root@elasticsearch02 service-spi]# kubectl describe ep keycloak -n=cas
Name: keycloak
Namespace: cas
Labels: <none>
Annotations: <none>
Subsets:
Addresses: 10.42.1.180,10.42.3.120
NotReadyAddresses: 10.42.1.185
Ports:
Name Port Protocol
---- ---- --------
http 8080 TCP
Events: <none>