使用docker-compose搭建consul集群环境
consul是一个集群环境,用来管理微服务架构下面的服务发现和配置管理功能。
- 集群包含三个server:node1, node2, node3
- 集群包含一个client:node4;并且在client上提供web UI访问服务。
1.下载consul docker image
1 |
docker pull consul |
2.编辑docker-compose.yaml文件
1 |
mkdir -p /data/consul/consul{1..4}/{data,config} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
version: '3.6' services: consul1: image: consul:latest container_name: consul1 restart: always command: agent -server -client=0.0.0.0 -bootstrap-expect=3 -node=consul1 volumes: - /data/consulconsul1/data:/consul/data - /data/consulconsul1/config:/consul/config consul2: image: consul:latest container_name: consul2 restart: always command: agent -server -client=0.0.0.0 -retry-join=consul1 -node=consul2 volumes: - /data/consulconsul2/data:/consul/data - /data/consulconsul2/config:/consul/config consul3: image: consul:latest container_name: consul3 restart: always command: agent -server -client=0.0.0.0 -retry-join=consul1 -node=consul3 volumes: - /data/consulconsul3/data:/consul/data - /data/consulconsul3/config:/consul/config consul4: image: consul:latest container_name: consul4 restart: always ports: - 8500:8500 command: agent -client=0.0.0.0 -retry-join=consul1 -ui -node=client1 volumes: - /data/consulconsul4/data:/consul/data - /data/consulconsul4/config:/consul/config |
说明:
启动了4个consul,其中consul1 是主节点,consul2、consul3 是子节点。consul4是提供ui服务的。
启动服务:
1 |
docker-compose up -d |
1 2 3 4 5 6 |
docker exec -t node1 consul members Node Address Status Type Build Protocol DC Segment node1 172.21.0.2:8301 alive server 1.4.0 2 dc1 <all> node2 172.21.0.4:8301 alive server 1.4.0 2 dc1 <all> node3 172.21.0.3:8301 alive server 1.4.0 2 dc1 <all> ndoe4 172.21.0.5:8301 alive client 1.4.0 2 dc1 <default> |
访问:服务器ip:8500可以看到集群
consul基本概念
1 2 3 4 5 6 7 8 9 10 |
server模式和client模式 server模式和client模式是consul节点的类型;client不是指的用户客户端。 server模式提供数据持久化功能。 client模式不提供持久化功能,并且实际上他也不工作,只是把用户客户端的请求转发到server模式的节点。所以可以把client模式的节点想象成LB(load balance),只负责请求转发。 通常server模式的节点需要配置成多个例如3个,5个。而client模式节点个数没有限制。 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
server模式启动的命令行参数 -server:表示当前使用的server模式;如果没有指定,则表示是client模式。 -node:指定当前节点在集群中的名称。 -config-dir:指定配置文件路径,定义服务的;路径下面的所有.json结尾的文件都被访问;缺省值为:/consul/config。 -data-dir: consul存储数据的目录;缺省值为:/consul/data。 -datacenter:数据中心名称,缺省值为dc1。 -ui:使用consul自带的web UI界面 。 -join:加入到已有的集群中。 -enable-script-checks: 检查服务是否处于活动状态,类似开启心跳。 -bind: 绑定服务器的ip地址。 -client: 客户端可访问ip,缺省值为:“127.0.0.1”,即仅允许环回连接。 -bootstrap-expect:在一个datacenter中期望的server节点数目,consul启动时会一直等待直到达到这个数目的server才会引导整个集群。这个参数的值在同一个datacenter的所有server节点上必须保持一致。 |
1 2 |
这里说明一下,另外一个参数-bootstrap,用来控制一个server是否运行在bootstrap模式:当一个server处于bootstrap模式时,它可以选举自己为leader;注意在一个datacenter中只能有一个server处于bootstrap模式。所以这个参数一般只能用在只有一个server的开发环境中,在有多个server的cluster产品环境中,不能使用这个参数,否则如果多个server都标记自己为leader那么会导致数据不一致。另外该标记不能和-bootstrap-expect同时指定。 |
- 本文固定链接: https://www.yoyoask.com/?p=6531
- 转载请注明: shooter 于 SHOOTER 发表