一、docker搭建consul集群
1.在docker上安裝consul(默認安裝最新版本)
???????? docker pull consul
2.啟動第一個consul服務:consul1
???????? docker run --name consul1 -d -p 8500:8500 -p 8300:8300 -p 8301:8301 -p 8302:8302 -p 8600:8600 consul:latest agent -server -bootstrap-expect 2 -ui -bind=0.0.0.0 -client=0.0.0.0
???????? 8500 http 端口,用于 http 接口和 web ui
???????? 8300 server rpc 端口,同一數(shù)據(jù)中心 consul server 之間通過該端口通信
???????? 8301 serf lan 端口,同一數(shù)據(jù)中心 consul client 通過該端口通信
???????? 8302 serf wan 端口,不同數(shù)據(jù)中心 consul server 通過該端口通信
???????? 8600 dns 端口,用于服務發(fā)現(xiàn)
???????? -server:定義agent運行在server模式還是Client模式
???????? -bbostrap-expect 2: 集群至少兩臺服務器,才能選舉集群leader
???????? -ui:運行 web 控制臺
???????? -bind: 監(jiān)聽網(wǎng)口,0.0.0.0 表示所有網(wǎng)口,如果不指定默認為127.0.0.1,則無法和容器通信
???????? -client : 限制某些網(wǎng)口可以訪問
3.獲取 consul server1 的 ip 地址,默認為172.17.0.2
???????? docker inspect consul1
4.啟動第二個consul服務:consul2, 并加入consul1(使用join命令)
???????? docker run --name consul2 -d -p 8501:8500 consul:latest agent -server -ui -bind=0.0.0.0 -client=0.0.0.0 -join 172.17.0.2
5.啟動第三個consul服務:consul3,并加入consul1
???????? docker run --name consul3 -d -p 8502:8500 consul agent -server -ui -bind=0.0.0.0 -client=0.0.0.0 -join 172.17.0.2
6.查看運行的容器(consul集群狀態(tài))---docker ps;在web UI查看集群和服務(在瀏覽器打開localhost:8500)
二、windows直接運行consul并持久化
1.下載consul并加入到環(huán)境
2.編寫bat腳本:consul agent -server -bootstrap-expect=1 -ui -bind=127.0.0.1 -client=0.0.0.0 -data-dir=D:/consul/data/ -config-dir=D:/consul/consuls/