在《k8s~Endpoints的使用》文章中,我们知道了如何通过Endpoints来访问外部资源,而本篇主要说一下它的另一个用法,service的负载均衡。
service的负载pod
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>