单机部署
Ubuntu 20.04
下载 consul 并配置到系统快捷方式
mkdir -p /storage/app/consul
cd /storage/app/consul
wget https://cdn.jansora.com/lib/consul/consul
chmod 755 /storage/app/consul/consul
配置到系统快捷方式
ln -sv /storage/app/consul/consul /usr/bin/consul
配置到 systemd
新建配置目录 mkdir -p /etc/consul.d
新建consul 服务 vim /etc/systemd/system/consul.service
写入以下内容
[Unit]
Description=Consul agent
Documentation=https://www.consul.io/docs/
After=network-online.target
Wants=network-online.target
[Service]
Environment="DAEMON_ARGS=-data-dir=/var/lib/consul --bind=127.0.0.1 -config-dir=/etc/consul.d -server -ui -client 0.0.0.0 -bootstrap-expect=1 -node=consul -bind=127.0.0.1 -client=0.0.0.0"
EnvironmentFile=-/etc/default/%p
ExecStart=/usr/bin/consul agent $DAEMON_ARGS
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
KillSignal=SIGINT
#AmbientCapabilities=CAP_NET_BIND_SERVICE
NoNewPrivileges=true
PrivateDevices=true
PrivateTmp=true
ProtectSystem=full
ProtectKernelTunables=true
[Install]
WantedBy=multi-user.target
使配置生效
systemctl daemon-reload
配置开机启动 consul
systemctl enable consul
启动
systemctl start consul
日志会打印在 /var/log/syslog 下, 可以据此定位
集群搭建
节点信息
为保证
consul
高可用,请至少三个节点
mode | ip |
---|---|
server | 192.168.0.111 |
server | 192.168.0.112 |
server | 192.168.0.113 |
节点配置信息
注意点: 只需要第一个节点开启 Web UI
即可, 且第一个的 bootstrap
为 true
consul1/config.json
{
"datacenter": "dc1",
"data_dir": "/app/data",
"log_level": "INFO",
"node_name": "consul1",
"server": true,
"ui": true,
"bootstrap": true,
"bind_addr": "192.168.0.111",
"client_addr": "192.168.0.111",
"retry_join": ["192.168.0.111","192.168.0.112", "192.168.0.113"],
"retry_interval": "3s",
"enable_debug": false,
"rejoin_after_leave": true,
"enable_syslog": false
}
consul2/config.json
{
"datacenter": "dc1",
"data_dir": "/app/data",
"log_level": "INFO",
"node_name": "consul2",
"server": true,
"ui": false,
"bootstrap": false,
"bind_addr": "192.168.0.112",
"client_addr": "192.168.0.112",
"retry_join": ["192.168.0.111","192.168.0.112", "192.168.0.113"],
"retry_interval": "3s",
"enable_debug": false,
"rejoin_after_leave": true,
"enable_syslog": false
}
consul3/config.json
{
"datacenter": "dc1",
"data_dir": "/app/data",
"log_level": "INFO",
"node_name": "consul3",
"server": true,
"ui": false,
"bootstrap": false,
"bind_addr": "192.168.0.113",
"client_addr": "192.168.0.113",
"retry_join": ["192.168.0.111","192.168.0.112", "192.168.0.113"],
"retry_interval": "3s",
"enable_debug": false,
"rejoin_after_leave": true,
"enable_syslog": false
}
构建 Docker 镜像
config.json 通过挂载目录映射, 例如
-v /root/storage/docker/consul/data/consul1:/app
FROM jansora/base:v1
RUN apt update && apt install consul -y
CMD ["consul", "agent", "-config-file=/app/config.json"]
创建 Docker 网络
配置一个桥接网络, 以后所有依赖 consul 的应用都可以使用此网络, 比如 dubbo
docker network create -d bridge --subnet=192.168.0.0/24 --gateway=192.168.0.254 --ip-range=192.168.0.0/24 app
启动集群
第一个节点 暴露 Web UI
docker run -d --name=consul1 --network app --restart always --ip 192.168.0.111 -v /root/storage/docker/consul/data/consul1:/app -p 8500:8500 jansora/consul:v1
docker run -d --name=consul2 --network app --restart always --ip 192.168.0.112 -v /root/storage/docker/consul/data/consul2:/app jansora/consul:v1
docker run -d --name=consul3 --network app --restart always --ip 192.168.0.113 -v /root/storage/docker/consul/data/consul3:/app jansora/consul:v1
mac
docker run -d --name=consul1 --network app --restart always --ip 192.168.0.111 -v ~/mapping/storage/docker/consul/data/consul1:/app -p 8500:8500 jansora/consul:v1
docker run -d --name=consul2 --network app --restart always --ip 192.168.0.112 -v ~/mapping/storage/docker/consul/data/consul2:/app jansora/consul:v1
docker run -d --name=consul3 --network app --restart always --ip 192.168.0.113 -v ~/mapping/storage/docker/consul/data/consul3:/app jansora/consul:v1
检查日志
root@jansora:~# docker logs consul1
==> WARNING: Bootstrap mode enabled! Do not enable unless necessary
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
Node name: 'consul1'
Datacenter: 'dc1'
Server: true (bootstrap: true)
Client Addr: 192.168.0.111 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
Cluster Addr: 192.168.0.111 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
Atlas: <disabled>
==> Log data will now stream in as it occurs:
2020/12/12 15:09:47 [INFO] raft: Node at 192.168.0.111:8300 [Follower] entering Follower state
2020/12/12 15:09:47 [INFO] serf: Ignoring previous leave in snapshot
2020/12/12 15:09:47 [INFO] serf: Ignoring previous leave in snapshot
2020/12/12 15:09:47 [INFO] serf: EventMemberJoin: consul1 192.168.0.111
2020/12/12 15:09:47 [INFO] serf: Ignoring previous leave in snapshot
2020/12/12 15:09:47 [INFO] serf: Ignoring previous leave in snapshot
2020/12/12 15:09:47 [INFO] serf: EventMemberJoin: consul1.dc1 192.168.0.111
2020/12/12 15:09:47 [INFO] serf: Attempting re-join to previously known node: consul3: 192.168.0.113:8301
2020/12/12 15:09:47 [INFO] consul: adding LAN server consul1 (Addr: 192.168.0.111:8300) (DC: dc1)
2020/12/12 15:09:47 [WARN] serf: Failed to re-join any previously known node
2020/12/12 15:09:47 [INFO] consul: adding WAN server consul1.dc1 (Addr: 192.168.0.111:8300) (DC: dc1)
2020/12/12 15:09:47 [INFO] agent: Joining cluster...
2020/12/12 15:09:47 [INFO] agent: (LAN) joining: [192.168.0.111 192.168.0.112 192.168.0.113]
2020/12/12 15:09:47 [ERR] agent: failed to sync remote state: No cluster leader
2020/12/12 15:09:47 [INFO] serf: EventMemberJoin: consul3 192.168.0.113
2020/12/12 15:09:47 [INFO] serf: EventMemberJoin: consul2 192.168.0.112
2020/12/12 15:09:47 [INFO] serf: Re-joined to previously known node: consul3: 192.168.0.113:8301
2020/12/12 15:09:47 [INFO] consul: adding LAN server consul3 (Addr: 192.168.0.113:8300) (DC: dc1)
2020/12/12 15:09:47 [INFO] consul: adding LAN server consul2 (Addr: 192.168.0.112:8300) (DC: dc1)
2020/12/12 15:09:47 [INFO] agent: (LAN) joined: 3 Err: <nil>
2020/12/12 15:09:47 [INFO] agent: Join completed. Synced with 3 initial agents
2020/12/12 15:09:48 [INFO] raft: Removed ourself, transitioning to follower
2020/12/12 15:09:48 [INFO] raft: Removed ourself, transitioning to follower
2020/12/12 15:10:07 [INFO] agent: Synced service 'consul'
consul systemd 配置
[Unit]
Description=Consul agent
Documentation=https://www.consul.io/docs/
After=network-online.target
Wants=network-online.target
[Service]
Environment="DAEMON_ARGS=-data-dir=/var/lib/consul --bind=192.168.2.101 -config-dir=/etc/consul.d -server -ui -client 0.0.0.0 -bootstrap-expect=1 -node=consul -advertise=192.168.2.101 -bind=0.0.0.0 -client=0.0.0.0"
EnvironmentFile=-/etc/default/%p
ExecStart=/usr/bin/consul agent $DAEMON_ARGS
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
User=consul
KillSignal=SIGINT
#AmbientCapabilities=CAP_NET_BIND_SERVICE
NoNewPrivileges=true
PrivateDevices=true
PrivateTmp=true
ProtectSystem=full
ProtectKernelTunables=true
[Install]
WantedBy=multi-user.target
常用功能
注销服务
Login in to the consul machine,and issue the command as follow:
consul services deregister -id={Your Service Id}