k8s~Endpoints的使(shi)用之負(fu)載均衡
在《k8s~Endpoints的使用》文章中,我(wo)們知道了如何通過Endpoints來訪(fang)問外(wai)部資源,而本篇主要說(shuo)一下它的另一個用法,service的負載(zai)均衡。
service的負載pod
注(zhu)意:service轉發到(dao)endpoints只是(shi)實(shi)現(xian)(xian)了(le)負(fu)載均衡,并沒有實(shi)現(xian)(xian)健康檢查,也(ye)就是(shi)當,說endpoint指向(xiang)的(de)節點宕機后,service也(ye)是(shi)會將流量轉向(xiang)到(dao)這(zhe)些節點的(de);如果希望(wang)實(shi)現(xian)(xian)監控檢查,可以使用envoy。
service里有個(ge)(ge)endpoints,它里面存儲所(suo)有pod的(de)(de)地址信息,即請求從外部打(da)到(dao)k8s的(de)(de)service的(de)(de)nodeport端(duan)口之后(沒有使(shi)用(yong)clusterIP虛擬ip,而是(shi)使(shi)用(yong)nodeport的(de)(de)方式,ip地址是(shi)節點宿主機(ji)的(de)(de),端(duan)口也是(shi)宿主機(ji)的(de)(de)),它會將endpoints列表轉發,而endpionts里存儲的(de)(de)就是(shi)每個(ge)(ge)pod的(de)(de)ip地址,最后落到(dao)容器里。
-
通過外部負載均衡器把請求打到 k8s的service里

-
service通過endpoints進行負載,把請求打到具體pod上面

-
查看service信息(xi),里面(mian)只顯示可用的endpoint信息(xi)
[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>

