Docker 部署Consul
docker run -d --name consul -p 8500:8500 consul
端口说明:https://www.consul.io/docs/install/ports.html
- 挂载说明
- /consul/data:持久化数据存储
- /consul/config:配置文件
Consul 配置:https://www.consul.io/docs/agent/options.html
浏览器打开 http://ip:8500
Docker部署集群
启动4个Consul Agent,3个Server(会选举出一个leader),1个Client
#启动第1个Server节点,集群要求要有3个Server,将容器8500端口映射到主机8900端口,同时开启管理界面 docker run -d --name=consul1 -p 8900:8500 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true --bootstrap-expect=3 --client=0.0.0.0 -ui #启动第2个Server节点,并加入集群 docker run -d --name=consul2 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true --client=0.0.0.0 --join 172.17.0.2 #启动第3个Server节点,并加入集群 docker run -d --name=consul3 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true --client=0.0.0.0 --join 172.17.0.2 #启动第4个Client节点,并加入集群 docker run -d --name=consul4 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=false --client=0.0.0.0 --join 172.17.0.2
第1个启动容器的IP一般是172.17.0.2,后边启动的几个容器IP会排着来:172.17.0.3、172.17.0.4、172.17.0.5。
这些Consul节点在Docker的容器内是互通的,他们通过桥接的模式通信。但是如果主机要访问容器内的网络,需要做端口映射。在启动第一个容器时,将Consul的8500端口映射到了主机的8900端口,这样就可以方便的通过主机的浏览器查看集群信息。
docker-compose 方式部署 consul 集群
cat > /data0/consul/docker-compose.yaml << \EOF version: '2' networks: byfn: services: consul1: image: consul container_name: node1 volumes: - /data0/consul/conf_with_acl:/consul/config command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config networks: - byfn consul2: image: consul container_name: node2 volumes: - /data0/consul/conf_with_acl:/consul/config command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config ports: - 8500:8500 depends_on: - consul1 networks: - byfn consul3: image: consul volumes: - /data0/consul/conf_with_acl:/consul/config container_name: node3 command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config depends_on: - consul1 networks: - byfn consul4: image: consul container_name: node4 volumes: - /data0/consul/conf_with_acl:/consul/config command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -ui -config-dir=/consul/config ports: - 8501:8500 depends_on: - consul2 - consul3 networks: - byfn consul5: image: consul container_name: node5 volumes: - /data0/consul/conf_without_acl:/consul/config command: agent -retry-join=node1 -node=ndoe5 -bind=0.0.0.0 -client=0.0.0.0 -ui -config-dir=/consul/config ports: - 8502:8500 depends_on: - consul2 - consul3 networks: - byfn EOF
单机启动脚本
创建同级目录config和data分别作为配置文件以及数据文件的目录
config目录中增加acl.json, 其中token作为登录的认证,相当于密码
{ "acl_datacenter": "dc1", "acl_master_token": "p2BE1AtpwPbrxZdC6k+eXA==", "acl_default_policy": "deny", "acl_down_policy": "extend-cache" }
./consul agent -config-dir=./config -server -data-dir=./data -bootstrap -ui
加上 -client=0.0.0.0 表示对外提供服务
集群启动脚本
在consul.exe目录下新建三个目录node1、node2、node3,分别新建配置文件basic.json
node1目录下basic.json
{ "datacenter": "dc1", "data_dir": "./node1/data/", "log_level": "INFO", "server": true, "node_name": "node1", "ui": true, "bind_addr": "192.168.1.108", "client_addr": "192.168.1.108", "advertise_addr": "192.168.1.108", "bootstrap_expect": 3, "ports":{ "http": 8500, "dns": 8600, "server": 8300, "serf_lan": 8301, "serf_wan": 8302 } }
node2目录下basic.json
{ "datacenter": "dc1", "data_dir": "./node2/data/", "log_level": "INFO", "server": true, "node_name": "node2", "ui": true, "bind_addr": "192.168.1.108", "client_addr": "192.168.1.108", "advertise_addr": "192.168.1.108", "bootstrap_expect": 3, "ports":{ "http": 8510, "dns": 8610, "server": 8310, "serf_lan": 8311, "serf_wan": 8312 } }
node3目录下basic.json
{ "datacenter": "dc1", "data_dir": "./node3/data/", "log_level": "INFO", "server": true, "node_name": "node3", "ui": true, "bind_addr": "192.168.1.108", "client_addr": "192.168.1.108", "advertise_addr": "192.168.1.108", "bootstrap_expect": 3, "ports":{ "http": 8520, "dns": 8620, "server": 8320, "serf_lan": 8321, "serf_wan": 8322 } }
在consul同目录下新建.bat文件,用于启动consul并加入集群, -retry-join指定要加入的集群节点
node1.bat
consul agent -server -client 0.0.0.0 -ui -config-file=./node1/basic.json -retry-join=192.168.1.108:8301 pause
node2.bat
consul agent -server -client 0.0.0.0 -ui -config-file=./node2/basic.json -retry-join=192.168.1.108:8301 pause
node3.bat
consul agent -server -client 0.0.0.0 -ui -config-file=./node3/basic.json -retry-join=192.168.1.108:8301 pause
启动三个consul节点后,访问localhost:8500即可看到自带的管理页面