consul通过DNS协议查找服务
1、centos默认没有dig命令,dig命令属于bind-utils工具包,安装这个包之后就可以使用dig命令了。
yum -y install bind-utils
2、使用DNS API来获取A记录ip地址
对于DNS API,服务的DNS名称是 NAME.service.consul 。默认所有的DNS名称都是在 consul 名称空间下,当然这个是可配置的。service 子域名告诉Consul我们正在查询服务,并且 NAME 就是要查询的服务的名称。
对于我们注册的minio服务,对应的查询设置就是 minio.service.consul:
[root@nginx ~]# dig @127.0.0.1 -p 8600 minio.service.consul
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> @127.0.0.1 -p 8600 minio.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15982
;; flags: qr aa rd; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 5
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;minio.service.consul. IN A
;; ANSWER SECTION:
minio.service.consul. 0 IN A 172.16.10.228
minio.service.consul. 0 IN A 172.16.10.227
minio.service.consul. 0 IN A 172.16.10.229
minio.service.consul. 0 IN A 172.16.10.226
;; ADDITIONAL SECTION:
minio.service.consul. 0 IN TXT "consul-network-segment="
minio.service.consul. 0 IN TXT "consul-network-segment="
minio.service.consul. 0 IN TXT "consul-network-segment="
minio.service.consul. 0 IN TXT "consul-network-segment="
;; Query time: 5 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: 四 3月 26 10:52:16 CST 2020
;; MSG SIZE rcvd: 257
可以看到,4个带了节点的IP地址的 A 记录被返回了,它就是可用的服务。 A 记录只能包含IP地址。
3、使用DNS API来获取完整的地址/端口的 SRV 记录:
[root@nginx ~]# dig @127.0.0.1 -p 8600 minio.service.consul SRV
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> @127.0.0.1 -p 8600 minio.service.consul SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48403
;; flags: qr aa rd; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 9
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;minio.service.consul. IN SRV
;; ANSWER SECTION:
minio.service.consul. 0 IN SRV 1 1 9000 minio-2.node.shenzhen.consul.
minio.service.consul. 0 IN SRV 1 1 9000 minio-1.node.shenzhen.consul.
minio.service.consul. 0 IN SRV 1 1 9000 minio-4.node.shenzhen.consul.
minio.service.consul. 0 IN SRV 1 1 9000 minio-3.node.shenzhen.consul.
;; ADDITIONAL SECTION:
minio-2.node.shenzhen.consul. 0 IN A 172.16.10.227
minio-2.node.shenzhen.consul. 0 IN TXT "consul-network-segment="
minio-1.node.shenzhen.consul. 0 IN A 172.16.10.226
minio-1.node.shenzhen.consul. 0 IN TXT "consul-network-segment="
minio-4.node.shenzhen.consul. 0 IN A 172.16.10.229
minio-4.node.shenzhen.consul. 0 IN TXT "consul-network-segment="
minio-3.node.shenzhen.consul. 0 IN A 172.16.10.228
minio-3.node.shenzhen.consul. 0 IN TXT "consul-network-segment="
;; Query time: 1 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: 四 3月 26 10:53:03 CST 2020
;; MSG SIZE rcvd: 449
SRV 记录显示了minio服务证运行在节点 minio-*.node.shenzhen.consul 的9000端口上。额外的部分和 A 记录返回的内容一样。