标签:ima nss bin 很多 opcode var root names web
上一讲;讲到consul发现的模板形式,这一讲我们聊一聊比较基础的consul的服务注册与发现,并且使用bind代理给服务做负载均衡,这个功能也是后面要补充的redis + consul 或者mha + consul的一个基础,我们先看一下环境:配置环境(还是那句话,在生产环境把server配置成3-5台,agent随意):
192.168.113.174 server 192.168.113.175 agent 192.168.113.176 agent 192.168.113.177 agent
1、启动master:
[root@master src]# consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -bind 0.0.0.0 -client 0.0.0.0 -ui &
参数说明:
-server:集群启动为service
-bootstrap-expect:期待加入service节点的数量
-data-dir:数据目录
-node:节点名称,集群中每个节点名称需唯一
-bind:指定一个consul运行时的监听地址
-ui:启动时启动ui
2、为了便于测试,我们在三台agent上面安装nginx服务。
[root@master src]# curl http://192.168.113.176/health/ 192.168.113.176 [root@master src]# curl http://192.168.113.175/health/ 192.168.113.175 [root@master src]# curl http://192.168.113.177/health/ 192.168.113.177
3、开始服务的注册:
Consul支持两种服务注册的方式,一种是通过Consul的服务注册HTTP API,由服务自身在启动后调用API注册自己,另外一种则是通过在配置文件中定义服务的方式进行注册。
Consul文档中建议使用后面一种方式来做服务 配置和服务注册。
4、我们以脚本的方式来注册,看一下脚本(每台都操作,一175为例):
[root@slave confd_data]# ls server.json [root@slave confd_data]# cat server.json { "service": { "name": "web", "tags": ["dev"], "address": "192.168.113.175", "port": 80, "checks": [ { "http": "http://192.168.113.175/health/", "interval": "10s" } ] } }
#-node的时候,175为web1,176为web2,177为web3
[root@slave confd_data]# consul agent -data-dir /tmp/consul -node=web1 -bind=192.168.113.175 -config-dir=/confd_data/ &
#启动之后还是独立的个体,这个时候是还没有注册的server的,我们可以收工去注册添加到集群,当然这个集群建议开始就创建好,配置文件reload即可:
[root@slave ~]# consul join 192.168.113.174 Successfully joined cluster by contacting 1 nodes.
5、配置完成后查看集群状态:
6、下面我们关闭web3的nginx然后再观察一下web UI的状态:
7、服务发现:
consul提供来服务发现接口,我们使用DNS解析的方式来查看一下,现在还解析道哪些服务:
[root@master src]# dig @127.0.0.1 -p 8600 web.service.consul SRV ; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> @127.0.0.1 -p 8600 web.service.consul SRV ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61209 ;; flags: qr aa rd; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 5 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;web.service.consul. IN SRV ;; ANSWER SECTION: web.service.consul. 0 IN SRV 1 1 80 web2.node.dc1.consul. web.service.consul. 0 IN SRV 1 1 80 web1.node.dc1.consul. ;; ADDITIONAL SECTION: web2.node.dc1.consul. 0 IN A 192.168.113.176 web2.node.dc1.consul. 0 IN TXT "consul-network-segment=" web1.node.dc1.consul. 0 IN A 192.168.113.175 web1.node.dc1.consul. 0 IN TXT "consul-network-segment="
8、开启web3的nginx再做测试:
[root@master src]# dig @127.0.0.1 -p 8600 web.service.consul SRV ; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> @127.0.0.1 -p 8600 web.service.consul SRV ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12981 ;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 7 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;web.service.consul. IN SRV ;; ANSWER SECTION: web.service.consul. 0 IN SRV 1 1 80 web1.node.dc1.consul. web.service.consul. 0 IN SRV 1 1 80 web3.node.dc1.consul. web.service.consul. 0 IN SRV 1 1 80 web2.node.dc1.consul. ;; ADDITIONAL SECTION: web1.node.dc1.consul. 0 IN A 192.168.113.175 web1.node.dc1.consul. 0 IN TXT "consul-network-segment=" web3.node.dc1.consul. 0 IN A 192.168.113.177 web3.node.dc1.consul. 0 IN TXT "consul-network-segment=" web2.node.dc1.consul. 0 IN A 192.168.113.176 web2.node.dc1.consul. 0 IN TXT "consul-network-segment="
9、域名解析问题:
我们看到consul的域名跳转来,如何在线上使用呢, 有三个方案:
1. 原内网dns服务器,做域名转发,consul后缀的,都转到consul server上(我们线上是采用这个) 2. dns全部跳到consul DNS服务器上,非consul后缀的,使用 recursors 属性跳转到原DNS服务器上 3. dnsmaq 转: server=/consul/10.16.X.X#8600 解析consul后缀的
10、构建bind域名解析:
[root@master src]# yum install bind -y
配置name服务做解析:
[root@master src]# cat /etc/named.conf options { listen-on port 53 { 192.168.113.174; }; listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; allow-query { any; }; recursion yes; dnssec-enable no; dnssec-validation no; /* Path to ISC DLV key */ bindkeys-file "/etc/named.iscdlv.key"; managed-keys-directory "/var/named/dynamic"; }; include "/etc/named/consul.conf";
[root@master src]# cat /etc/named/consul.conf zone "consul" IN { type forward; forward only; forwarders { 192.168.113.174 port 8600; }; };
11、再次测试一下解析:
[root@slave confd_data]# dig @192.168.113.174 -p 8600 web.service.consul A ; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> @192.168.113.174 -p 8600 web.service.consul A ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57750 ;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;web.service.consul. IN A ;; ANSWER SECTION: web.service.consul. 0 IN A 192.168.113.177 web.service.consul. 0 IN A 192.168.113.176 web.service.consul. 0 IN A 192.168.113.175
#测试一下bind域名转发:
[root@master confdata]# dig @192.168.113.174 -p 53 web.service.consul A ; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> @192.168.113.174 -p 53 web.service.consul A ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6024 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;web.service.consul. IN A ;; ANSWER SECTION: web.service.consul. 0 IN A 192.168.113.177 web.service.consul. 0 IN A 192.168.113.176 web.service.consul. 0 IN A 192.168.113.175
12、然后修改域名解析到192.168.113.174:
[root@master confdata]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 192.168.113.174
13、访问一下这个域名看一下:
[root@master confdata]# curl web.service.consul/health/ 192.168.113.175
各台ping测试:
[root@mha ~]# ping web.service.consul PING web.service.consul (192.168.113.177) 56(84) bytes of data. 64 bytes from mha (192.168.113.177): icmp_seq=1 ttl=64 time=1.34 ms [root@master confdata]# ping web.service.consul PING web.service.consul (192.168.113.175) 56(84) bytes of data. 64 bytes from slave (192.168.113.175): icmp_seq=1 ttl=64 time=0.524 ms 64 bytes from slave (192.168.113.175): icmp_seq=2 ttl=64 time=0.343 ms
总结:到处我们做的服务注册与发现已经完成,看到网络上很多概念自己有点蒙,所以就实现了一把这个东西,后面有时间我们在更新redis+哨兵+consul无缝迁移的时候,会用到类似的东西,当然应用场景是很多的,在微服务器架构下我们如何注册并发现一个启动的服务器,做代理,是一个问题。总之consul非常强大。
标签:ima nss bin 很多 opcode var root names web
原文地址:http://blog.51cto.com/xiaoluoge/2134126