基于Docker的Consul集群实现服务发现(2)

/usr/local/server.json
{
    "datacenter": "dc1",
    "data_dir": "/usr/local/",
    "log_level": "INFO",
    "server": true,
    "bootstrap_expect": 3,
    "bind_addr": "172.18.0.11",
    "client_addr": "0.0.0.0",
    "start_join": ["172.18.0.11","172.18.0.12","172.18.0.13"],
    "ui":true
}

依次登录三个容器中,以server模式启动consul服务
./consul agent -server -config-dir=/usr/local > /usr/local/consul.log &
由于配置文件中制定了集群的IP列表,因此无需显式加入集群(cluster join),正常情况下,启动三个节点后,会自动组成一个集群,并且自动选举出来一个leader。

consul 集群服务的状态

./consul members --http-addr 172.18.0.11:8500

./consul operator raft list-peers -http-addr=172.18.0.12:8500

基于Docker的Consul集群实现服务发现



consul客户端安装配置

容器客户端节点安装,6个节点IP分别是:172.18.0.21,172.18.0.22,172.18.0.23,172.18.0.24,172.18.0.25,172.18.0.26
docker run -itd --name redis01 --net mynetwork --ip 172.18.0.21 -v /usr/local/docker_file/redis01/:/usr/local/ centos 
docker run -itd --name redis02 --net mynetwork --ip 172.18.0.22 -v /usr/local/docker_file/redis02/:/usr/local/ centos 
docker run -itd --name redis03 --net mynetwork --ip 172.18.0.23 -v /usr/local/docker_file/redis03/:/usr/local/ centos 
docker run -itd --name redis04 --net mynetwork --ip 172.18.0.24 -v /usr/local/docker_file/redis04/:/usr/local/ centos 
docker run -itd --name redis05 --net mynetwork --ip 172.18.0.25 -v /usr/local/docker_file/redis05/:/usr/local/ centos 
docker run -itd --name redis06 --net mynetwork --ip 172.18.0.26 -v /usr/local/docker_file/redis06/:/usr/local/ centos

 

6个client节点的配置以及服务定义,服务探测脚本如下
如下是172.18.0.21节点的配置,不同节点仅需修改bind_addr为对应机器(容器)的IP

client.json { "data_dir": "usr/local/consuldata", "enable_script_checks": true, "bind_addr": "172.18.0.21", "retry_join": ["172.18.0.11","172.18.0.12","172.18.0.13"], "retry_interval": "30s", "rejoin_after_leave": true, "start_join": ["172.18.0.11","172.18.0.12","172.18.0.13"] }

分别启动三个client节点的consul服务,以client的模式运行,启动后,正常情况下会自动加入到consul的服务端集群中。

./consul agent -config-dir=/usr/local/consuldata > /usr/local/consuldata/consul.log &

./consul members --http-addr 172.18.0.11:8500

基于Docker的Consul集群实现服务发现

 

 

consul客户端代理服务注册

6个容器节点依次安装redis,做成一个集群(步骤略),这里的consul客户端代理的是一个3中3从的Redis集群,这里不列出来Redis集群的安装。
Redis集群安装参考https://www.cnblogs.com/wy123/p/12012848.html,还是非常方便的,在本地(容器节点)一键创建6个节点3主3从的集群。
其中主节点是172.18.0.21,172.18.0.22,172.18.0.23,从节点是172.18.0.24,172.18.0.25,172.18.0.26

基于Docker的Consul集群实现服务发现

这里是使用 w-master-redis-8888.service.consul名字作为三个redis集群节点的服务代理。
172.18.0.21节点上的redis-master-8888.json(172.18.0.22,172.18.0.23,172.18.0.24,172.18.0.25,172.18.0.26 类同,仅需修改address)

{ "services": [ { "name": "w-master-redis-8888", "tags": [ "master" ], "address": "172.18.0.21", "port": 8888, "checks": [ { "args":["sh","-c","/usr/local/consuldata/check_redis_master.sh 172.18.0.21 8888 ******"], "Shell":"/bin/bash", "interval": "15s" } ] } ] }

redis-slave-8888.json 

{ "services": [ { "name": "r-slave-redis-8888", "tags": [ "master" ], "address": "172.18.0.21", "port": 8888, "checks": [ { "args":["sh","-c","/usr/local/consuldata/check_redis_slave.sh 172.18.0.21 8888 ******"], "Shell":"/bin/bash", "interval": "15s" } ] } ] }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/06c4638eaec1f47907983374d1ab40de.html