hosts
10.0.0.11 node1
10.0.0.12 node2
10.0.0.13 node3
安装
下载
curl --silent --remote-name https://releases.hashicorp.com/consul/1.9.4/consul_1.9.4_linux_amd64.zip
unzip consul_1.9.4_linux_amd64.zip
cp consul /usr/bin/
consul --version
配置自动补全
consul -autocomplete-install
complete -C /usr/bin/consul consul
创建启动用户及数据目录
useradd --system --home /etc/consul.d --shell /bin/false consul
mkdir /opt/consul
chown -R consul:consul /opt/consul
单机安装
consul agent -server -data-dir=/opt/consul -bootstrap -ui -advertise=10.0.0.11 -client=10.0.0.11
访问UI:http://10.0.0.11:8500/ui
常用参数
-bind:为该节点绑定一个地址
-enable-script-checks=true:设置检查服务为可用
-join:加入到已有的集群中
-server 表示当前使用的server模式
-node:集群中的每个node必须有一个唯一的名称。默认情况下,Consul使用机器的hostname
-config-file: 要加载的配置文件
-config-dir:指定配置文件,定义服务的,默认所有以.json结尾的文件都会读
-datacenter: 数据中心没名称,不设置的话默认为dc
-client: 客户端模式
-ui: 使用consul自带的ui界面
-data-dir consul存储数据的目录
-bootstrap:用来控制一个server是否在bootstrap模式,在一个datacenter中只能有一个server处于bootstrap模式,当一个server处于bootstrap模式时,可以自己选举为raft leader。
-bootstrap-expect:在一个datacenter中期望提供的server节点数目,当该值提供的时候,consul一直等到达到指定sever数目的时候才会引导整个集群,该标记不能和bootstrap共用
配置systemd
vim /usr/lib/systemd/system/consul.service
[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
[Service]
Type=notify
User=consul
Group=consul
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
集群安装
生成密钥(可选)
consul keygen
输出的密钥用于在服务端的配置文件中在 encrypt 项中配置
生成TLS证书以进行RPC加密(可选)
创建CA
consul tls ca create
==> Saved consul-agent-ca.pem
==> Saved consul-agent-ca-key.pem
创建证书
# 创建服务端证书
consul tls cert create -server -dc <dc_name>
创建客户端证书
consul tls cert create -client -dc <dc_name>
分发证书
scp consul-agent-ca.pem <dc-name>-<server/client>-consul-<cert-number>.pem <dc-name>-<server/client>-consul-<cert-number>-key.pem <USER>@<PUBLIC_IP>:/etc/consul.d/
创建配置文件
一般创建 consul.json 用于所有agent,创建 server.json 用于服务端
mkdir /etc/consul.d
vim /etc/consul.d/consul.json
node1
{
"datacenter": "dc",
"data_dir": "/opt/consul",
"server": true,
"bootstrap_expect": 3,
"log_file": "/opt/consul/consul.log",
"log_level": "INFO",
"node_name": "node1",
"advertise_addr": "10.0.0.11",
"bind_addr": "10.0.0.11",
"client_addr": "10.0.0.11",
"retry_join": ["10.0.0.11", "10.0.0.12", "10.0.0.13"],
"ui_config": {
"enabled": true
}
}
node2
{
"datacenter": "dc",
"data_dir": "/opt/consul",
"server": true,
"bootstrap_expect": 3,
"log_file": "/opt/consul/consul.log",
"log_level": "INFO",
"node_name": "node2",
"advertise_addr": "10.0.0.12",
"bind_addr": "10.0.0.12",
"client_addr": "10.0.0.12",
"retry_join": ["10.0.0.11", "10.0.0.12", "10.0.0.13"],
"ui_config": {
"enabled": false
}
}
node3
{
"datacenter": "dc",
"data_dir": "/opt/consul",
"server": true,
"bootstrap_expect": 3,
"log_file": "/opt/consul/consul.log",
"log_level": "INFO",
"node_name": "node3",
"advertise_addr": "10.0.0.13",
"bind_addr": "10.0.0.13",
"client_addr": "10.0.0.13",
"retry_join": ["10.0.0.11", "10.0.0.12", "10.0.0.13"],
"ui_config": {
"enabled": false
}
}
chown -R consul:consul /etc/consul.d
chmod 640 /etc/consul.d/consul.json
启动文件
vim /usr/lib/systemd/system/consul.service
[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
[Service]
Type=notify
User=consul
Group=consul
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
检查配置
consul validate /etc/consul.d/consul.json
三个节点分别启动
systemctl start consul.service
在任意节点查看集群
consul members -http-addr=http://10.0.0.11:8500