K8S容器HeadlessService间动态IP通信
阅读原文时间:2023年07月08日阅读:3

文件网址:https://www.kubebiz.com/KubeBiz/MongoDB?k8sv=v1.20

使用文件网址中提供的yaml文件安装三节点的mongodb集群,其service是headless service,因为对应的service.yaml文件中: spec.clusterIP:None

和普通Service相比,Headless Service没有ClusterIP(所以没有负载均衡),它会给一个集群内部的每个成员提供一个唯一的DNS域名来作为每个成员的网络标识,集群内部成员之间使用域名通信。无头服务管理的域名是如下的格式:

$(service_name).$(k8s_namespace).svc.cluster.local

其中的 "cluster.local"是集群的域名,除非做了配置,否则集群域名默认就是cluster.local。

statefulSet.spec.serviceName,当serviceName 配置成与headless service的Name 相同的时候, 可以通过 {hostName}.{headless service}.{namespace}.svc.cluster.local 解析出节点IP。hostName 由 {statefulSet name}-{编号} 组成。

StatefulSet下创建的每个Pod,得到一个对应的DNS子域名,格式如下:

{statefulSet name}-{编号}.{headless service}.{namespace}.svc.cluster.local

放在当前mongoDB中,对应的DNS子域名分别是:

mongo-0.mongo.my-space.svc.cluster.local
mongo-1.mongo.my-space.svc.cluster.local
mongo-2.mongo.my-space.svc.cluster.local

查看ednpoints

# kubectl describe service mongo -n my-space
Name:              mongo
Namespace:         my-space
Labels:            name=mongo
Annotations:       <none>
Selector:          role=mongo
Type:              ClusterIP
IP Families:       <none>
IP:                None
IPs:               None
Port:              <unset>  27017/TCP
TargetPort:        27017/TCP
Endpoints:         10.1.112.241:27017,10.3.7.207:27017,10.3.7.208:27017
Session Affinity:  None

ping headless service的结果,会发现是从3个当中任选一个来进行响应的

/ # ping mongo.my-space.svc.cluster.local
PING mongo.my-space.svc.cluster.local (10.3.7.207): 56 data bytes
64 bytes from 10.3.7.207: seq=0 ttl=63 time=0.632 ms
64 bytes from 10.3.7.207: seq=1 ttl=63 time=0.267 ms
64 bytes from 10.3.7.207: seq=2 ttl=63 time=0.161 ms
64 bytes from 10.3.7.207: seq=3 ttl=63 time=0.149 ms
64 bytes from 10.3.7.207: seq=4 ttl=63 time=0.150 ms
^C
--- mongo.my-space.svc.cluster.local ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.149/0.271/0.632 ms

单独ping 每一个pods的DNS域名:

/ # ping mongo-0.mongo.my-space.svc.cluster.local
PING mongo-0.mongo.my-space.svc.cluster.local (10.1.112.241): 56 data bytes
64 bytes from 10.1.112.241: seq=0 ttl=62 time=1.358 ms
64 bytes from 10.1.112.241: seq=1 ttl=62 time=0.917 ms
64 bytes from 10.1.112.241: seq=2 ttl=62 time=0.945 ms
64 bytes from 10.1.112.241: seq=3 ttl=62 time=0.717 ms
^C
--- mongo-0.mongo.my-space.svc.cluster.local ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.717/0.984/1.358 ms

/ # ping mongo-1.mongo.my-space.svc.cluster.local
PING mongo-1.mongo.my-space.svc.cluster.local (10.3.7.207): 56 data bytes
64 bytes from 10.3.7.207: seq=0 ttl=63 time=0.526 ms
64 bytes from 10.3.7.207: seq=1 ttl=63 time=0.156 ms
64 bytes from 10.3.7.207: seq=2 ttl=63 time=0.135 ms
^C
--- mongo-1.mongo.my-space.svc.cluster.local ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.135/0.272/0.526 ms

/ # ping mongo-2.mongo.my-space.svc.cluster.local
PING mongo-2.mongo.my-space.svc.cluster.local (10.3.7.208): 56 data bytes
64 bytes from 10.3.7.208: seq=0 ttl=63 time=0.290 ms
64 bytes from 10.3.7.208: seq=1 ttl=63 time=0.145 ms
64 bytes from 10.3.7.208: seq=2 ttl=63 time=0.198 ms
^C
--- mongo-2.mongo.my-space.svc.cluster.local ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.145/0.211/0.290 ms

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章