macos安装
brew tap hashicorp/tap
brew install hashicorp/tap/consul
linux amd系统安装
wget https://releases.hashicorp.com/consul/1.10.2/consul_1.10.2_linux_amd64.zip
unzip consul_1.10.2_linux_amd64.zip -d /usr/local/bin
# 如果 consul 能直接运行,下面的不用管
vi ~/.bash_profile
export CONSUL_HOME=/usr/local/bin/consul
export PATH=$PATH:CONSUL_HOME
// 使用环境变量配置生效
source ~/.bash_profile
单节点运行
consul agent -dev
运行多个节点
- service 节点
mkdir -p /etc/consul.d
consul agent -server -bootstrap-expect 2 -data-dir /tmp/consul -node=server1 -bind=192.168.232.130 -ui -config-dir /etc/consul.d -rejoin -join 192.168.232.130 -client 0.0.0.0
- node节点
consul agent -server -bootstrap-expect 2 -data-dir /tmp/consul -node=node1 -bind=192.168.232.130 -ui -config-dir /etc/consul.d -rejoin -join 192.168.232.130 -client 0.0.0.0
运行
管理页面
- http://192.168.232.130:8500/ui/dc1/services
curl http://ip:8500/v1/status/leader
docker部署多节点
参数说明
- -boostrap-expect 2: 集群至少两台服务器,才能选举集群leader
- -ui:运行 web 控制台
- -bind: 监听网口,0.0.0.0 表示所有网口,如果不指定默认未127.0.0.1,则无法和容器通信
- -client : 限制某些网口可以访问
端口说明
- 8500 http 端口,用于 http 接口和 web ui
- 8300 server rpc 端口,同一数据中心 consul server 之间通过该端口通信
- 8301 serf lan 端口,同一数据中心 consul client 通过该端口通信
- 8302 serf wan 端口,不同数据中心 consul server 通过该端口通信
- 8600 dns 端口,用于服务发现
host主机网段
- master节点
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
# 查看master ip地址,跟node节点 -join 172.17.0.4 一致
docker exec -it consul1 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
- node节点
docker run --name consul2 -d -p 8501:8500 consul agent -server -ui -bind=0.0.0.0 -client=0.0.0.0 -join 172.17.0.4
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.4
不同网段实现集群部署
上面的是服务部署单节点
docker network create -d macvlan --subnet=172.18.0.0/24 --gateway=172.18.0.1 -o macnet3
- server 节点
docker run -d -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' --name=consul-node1 --net=macnet3 -p 8500:8500 consul agent -server -bind=172.18.0.2 -bootstrap-expect=3 -node=node1 -client 0.0.0.0
- node 节点
docker run -d -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' --name=consul-node2 --net=macnet3 consul agent -server -bind=172.18.0.3 -join=172.18.0.2 -node-id=$(uuidgen | awk '{print tolower($0)}') -node=node2 -client=172.18.0.3
docker run -d -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' --name=consul-node3 --net=macnet3 consul agent -server -bind=172.18.0.4 -join=172.18.0.2 -node-id=$(uuidgen | awk '{print tolower($0)}') -node=node3 -client=172.18.0.4
- 查看node信息
[root@localhost ~]# docker exec -it consul-node1 consul members
Node Address Status Type Build Protocol DC Segment
node1 172.18.0.2:8301 alive server 1.10.2 2 dc1 <all>
node2 172.18.0.3:8301 alive server 1.10.2 2 dc1 <all>
node3 172.18.0.4:8301 alive server 1.10.2 2 dc1 <all>
原创文章,作者:站长,如若转载,请注明出处:https://wsppx.cn/1396/%e7%bd%91%e7%bb%9c%e5%bc%80%e5%8f%91/