Consul是HashiCorp公司推出的开源软件,基于 GO 语言编写,提供服务注册和发现、配置、多数据中心的高可用方案等能力,分布式一致方面采用 raft 算法实现,并且很容易和 Spring Cloud 等微服务框架集成,使用起来非常简单,具有简单、易用、可插排等特点。
简而言之,Consul 是服务网格的控制平面,Consul 提供了一种完整的服务网格解决方案。
Consul 运行的节点连接在一起,称为数据中心,一个数据中心将有 3 到 5 台服务器和许多客户端。Consul集群架构如下:
此处采用的是 3Server + 1Client 的集群架构,3 台 Server 在单独的虚拟机上,1 台 Client 则直接在部署服务的宿主机运行。
| 节点名称 | 节点IP | Agent类型 | OS版本 |
|---|---|---|---|
| consul-01 | 192.168.94.10 | server | Ubuntu 22.04 LTS |
| consul-02 | 192.168.94.11 | server | Ubuntu 22.04 LTS |
| consul-03 | 192.168.94.12 | server | Ubuntu 22.04 LTS |
| consul-client | 192.168.94.13 | client | Ubuntu 22.04 LTS |
配置主机名,命令示例:(以下操作在所有节点配置)
hostnamectl set-hostname consul-01 hostnamectl set-hostname consul-02 hostnamectl set-hostname consul-03 hostnamectl set-hostname consul-client
下载consul二进制文件
export VER="1.12.3"
wget https://releases.hashicorp.com/consul/${VER}/consul_${VER}_linux_amd64.zip
解压安装
apt install -y unzip
unzip consul_${VER}_linux_amd64.zip -d /usr/local/bin/
查看版本
consul version
创建consul用户和组
sudo groupadd --system consul sudo useradd -s /sbin/nologin --system -g consul consul
创建consul安装目录
mkdir -p /data/consul/{config,data,logs}
chown -R consul:consul /data/consul
chmod -R 775 /data/consul
设置 DNS 或编辑/etc/hosts 文件以配置所有服务器的主机名,替换example.com为您的实际域名。
cat >/etc/hosts<2、Server 节点配置 生成Consul secret
consul keygen为server节点创建json配置文件,其他节点配置修改IP地址及节点名称即可
consul-01节点
cat >/data/consul/config/consul.json<"advertise_addr": "192.168.94.10", "bind_addr": "192.168.94.10", "bootstrap_expect": 3, "client_addr": "0.0.0.0", "datacenter": "DC1", "node_name": "consul-01", "data_dir": "/data/consul/data", "domain": "consul", "enable_script_checks": true, "dns_config": { "enable_truncate": true, "only_passing": true }, "enable_syslog": true, "encrypt": "F0VSq05Die32sMUimbZ2LAzUIc5Ry4jCL2ouwz/Gu8Q=", "leave_on_terminate": true, "log_level": "INFO", "rejoin_after_leave": true, "retry_join": [ "consul-01", "consul-02", "consul-03" ], "server": true, "start_join": [ "consul-01", "consul-02", "consul-03" ], "ui": true } EOF 配置参数说明:
datacenter:此标志控制运行代理的数据中心。如果未提供,则默认为dc1。Consul 对多个数据中心有一流的支持,但它依赖于正确的配置。同一数据中心中的节点应位于单个 LAN 上。
data_dir:该标志为代理提供了一个数据目录来存储状态。
node_name:节点名字,一般为主机名。
server:指定是否为server节点。
bootstrap_expect:在一个datacenter中期望提供的server节点数目,当提供该值的时候,consul一直等到达到指定sever数目才会引导整个集群,该标记不能和bootstrap公用。
bind_addr:该地址用来在集群内部的通讯,集群内的所有节点到地址都必须是可达的,默认是0.0.0.0。
client_addr:Consul 将绑定客户端接口的地址,包括 HTTP 和 DNS 服务器。默认情况下,这是127.0.0.1,仅允许环回连接。在 Consul 1.0 及更高版本中,这可以设置为要绑定到的以空格分隔的地址列表。
log_json:此标志使代理能够以 JSON 格式输出日志。默认为false。
log_level:在 Consul 代理启动后显示的日志级别。默认为 info 。可用的日志级别是trace、debug、info、warn和err。
retry_join:指定将要置入集群的IP列表,如果失败,会自动重试,知道直到成功加入。
start_join:启动时加入集群的地址
ui_config:此对象允许设置多个子键,用于控制 UI 中可用的显示或功能。
rejoin_after_leave :允许重新加入集群
consul-02节点
cat >/data/consul/config/consul.json<"advertise_addr": "192.168.94.11", "bind_addr": "192.168.94.11", "bootstrap_expect": 3, "client_addr": "0.0.0.0", "datacenter": "DC1", "node_name": "consul-02", "data_dir": "/data/consul/data", "domain": "consul", "enable_script_checks": true, "dns_config": { "enable_truncate": true, "only_passing": true }, "enable_syslog": true, "encrypt": "F0VSq05Die32sMUimbZ2LAzUIc5Ry4jCL2ouwz/Gu8Q=", "leave_on_terminate": true, "log_level": "INFO", "rejoin_after_leave": true, "retry_join": [ "consul-01", "consul-02", "consul-03" ], "server": true, "start_join": [ "consul-01", "consul-02", "consul-03" ], "ui": true } EOF consul-03节点
cat >/data/consul/config/consul.json<3、Client 节点配置"advertise_addr": "192.168.94.12", "bind_addr": "192.168.94.12", "bootstrap_expect": 3, "client_addr": "0.0.0.0", "datacenter": "DC1", "node_name": "consul-03", "data_dir": "/data/consul/data", "domain": "consul", "enable_script_checks": true, "dns_config": { "enable_truncate": true, "only_passing": true }, "enable_syslog": true, "encrypt": "F0VSq05Die32sMUimbZ2LAzUIc5Ry4jCL2ouwz/Gu8Q=", "leave_on_terminate": true, "log_level": "INFO", "rejoin_after_leave": true, "retry_join": [ "consul-01", "consul-02", "consul-03" ], "server": true, "start_join": [ "consul-01", "consul-02", "consul-03" ], "ui": true } EOF client是consul客户端,客户端不保存数据,客户端将接收到的请求转发给Server端。Server之间通过局域网或广域网通信实现数据一致性。每个Server或Client都是一个consul agent。
创建consul-client节点配置文件
cat >/data/consul/config/consul.json<4、systemd 启动文件"advertise_addr": "192.168.94.13", "bind_addr": "192.168.94.13", "client_addr": "0.0.0.0", "datacenter": "DC1", "node_name": "consul-client", "data_dir": "/data/consul/data", "domain": "consul", "enable_script_checks": true, "dns_config": { "enable_truncate": true, "only_passing": true }, "enable_syslog": true, "encrypt": "F0VSq05Die32sMUimbZ2LAzUIc5Ry4jCL2ouwz/Gu8Q=", "leave_on_terminate": true, "log_level": "INFO", "rejoin_after_leave": true, "retry_join": [ "consul-01", "consul-02", "consul-03" ], "server": false, "start_join": [ "consul-01", "consul-02", "consul-03" ], "ui": true } EOF 所有节点创建systemd 服务文件/etc/systemd/system/consul.service,所有节点具有相同内容,配置如下:
cat >/etc/systemd/system/consul.service<所有节点启动consul服务
systemctl enable --now consul.service查看consul服务运行状态
root@consul-01:~# systemctl status consul ● consul.service - Consul Service Discovery Agent Loaded: loaded (/etc/systemd/system/consul.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2022-07-19 11:17:02 CST; 3s ago Docs: https://www.consul.io/ Main PID: 46248 (consul) Tasks: 8 (limit: 4537) Memory: 23.3M CPU: 249ms CGroup: /system.slice/consul.service └─46248 /usr/local/bin/consul agent -config-dir=/data/consul/config/consul.json Jul 19 11:17:03 consul-01 consul[46248]: agent: Joining cluster Jul 19 11:17:03 consul-01 consul[46248]: agent: (LAN) joining: lan_addresses=[consul-01, consul-02, consul-03] Jul 19 11:17:03 consul-01 consul[46248]: agent: (LAN) joined: number_of_nodes=3 Jul 19 11:17:03 consul-01 consul[46248]: agent: Join cluster completed. Synced with initial agents: cluster=LAN num_agents=3 Jul 19 11:17:03 consul-01 consul[46248]: agent: (LAN) joined: number_of_nodes=3 Jul 19 11:17:03 consul-01 consul[46248]: agent: Join completed. Initial agents synced with: agent_count=3 Jul 19 11:17:03 consul-01 consul[46248]: agent: started state syncer Jul 19 11:17:03 consul-01 consul[46248]: agent: Consul agent running! Jul 19 11:17:03 consul-01 consul[46248]: 2022-07-19T11:17:03.431+0800 [INFO] agent: Synced node info Jul 19 11:17:03 consul-01 consul[46248]: agent: Synced node info5、查看 Consul 集群状态3 台 Server 和 1 台 Client 上的 Consul 服务都启动后会根据 consul.json 中的配置自动组成一个集群,并且 Server 会通过 Raft 协议在 Server 节点中选举出 leader 节点。
可以用如下命令在任意一台机器上查看集群节点状态:
root@consul-01:~# consul members Node Address Status Type Build Protocol DC Partition Segment consul-01 192.168.94.10:8301 alive server 1.12.3 2 dc1 defaultconsul-02 192.168.94.11:8301 alive server 1.12.3 2 dc1 default consul-03 192.168.94.12:8301 alive server 1.12.3 2 dc1 default consul-client 192.168.94.13:8301 alive client 1.12.3 2 dc1 default 查看server节点成员状态信息:
root@consul-01:~# consul operator raft list-peers Node ID Address State Voter RaftProtocol consul-02 edefb129-e0fd-804d-bbfb-76e259077d03 192.168.94.11:8300 leader true 3 consul-01 50f9830f-45cd-683c-2382-77e1cf6d6e48 192.168.94.10:8300 follower true 3 consul-03 5dae09d2-7079-4611-1ab5-7728d0966cbe 192.168.94.12:8300 follower true 3访问Consul UI:http://192.168.94.10:8500/ui
参考:
- https://www.cnblogs.com/wangguishe/p/15599233.html
- https://computingforgeeks.com/how-to-install-consul-cluster-18-04-lts/



