1. 下载路径
https://www.consul.io/downloads.html
压缩包: consul_1.2.3_linux_amd64.zip
2. 环境
大网IP | 小网IP | node名称 |
10.40.66.143 | 172.20.80.193 | consul-s-1 |
10.40.66.145 | 172.20.80.195 | consul-s-2 |
10.40.66.146 | 172.20.80.196 | consul-s-3 |
10.40.66.144 | 172.20.80.194 | consul-c-3 |
目录:/home/consul
步骤3到步骤8,分别在四台机子上执行(client和server配置文件不一样,看步骤6)
3. 拷贝consul_1.2.3_linux_amd64.zip到/home/consul并解压得到consul可执行文件
unzip consul_1.2.3_linux_amd64.zip
4. 将consul移动到/usr/local/bin/,即添加到PATH目录下
mv consul /usr/local/bin/
5. 检测成功
-bash-4.2# consul version
Consul v1.2.3
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
6. 配置启动文件config-dir.d/full.json(启动默认加载config-dir.d下面所有.json文件)
server的配置文件:
{
"datacenter": "dc1",
"data_dir": "/home/consul/data",
"log_level": "INFO",
"node_name": "consul-s-1",
"server": true,
"bootstrap_expect": 3,
"bind_addr": "172.20.80.193",
"client_addr": "0.0.0.0",
"ui": true,
"retry_join": ["172.20.80.193","172.20.80.195","172.20.80.196"],
"retry_interval": "30s",
"enable_debug": false,
"rejoin_after_leave": true,
"start_join": ["172.20.80.193","172.20.80.195","172.20.80.196"],
"disable_update_check": true,
"log_file":"/home/consul/log/"
}
注释:启动命令 consul agent -config-dir /home/consul/config-dir.d,执行此命令即可启动。
client的配置文件:
{
"datacenter": "dc1",
"data_dir": "/home/consul/data",
"log_level": "INFO",
"node_name": "consul-c-1",
"bind_addr": "172.20.80.194",
"client_addr": "0.0.0.0",
"retry_join": ["172.20.80.193","172.20.80.195","172.20.80.196"],
"retry_interval": "30s",
"enable_debug": false,
"rejoin_after_leave": true,
"start_join": ["172.20.80.193","172.20.80.195","172.20.80.196"],
"disable_update_check": true,
"log_file":"/home/consul/log/"
}
注释:启动命令 consul agent -config-dir /home/consul/config-dir.d
重要参数解释:
-data-dir(data_dir):指定agent储存状态的数据目录,这是所有agent都必须的,对server尤其重要,因为他们必须持久化集群的状态;
-config-dir:指定配置文件夹,通常以.d结尾(比如consul.d),里面若干个配置文件,以.json结尾;
-config-file:指定一个配置文件;
-bind(bind_addr):集群内部通信使用,默认0.0.0.0,即绑定到本机所有地址(本地多网卡有多地址),并使用第一个可用私有IPv4地址与集群中其他成 员通信;
-client(client_addr):绑定客户端命令访问的地址,默认是127.0.0.1,只允许回路访问;如果要对外提供服务改成0.0.0.0(或者本机IP,consul客户端命 令默认使用127.0.0.1:8500访问服务,若绑定本机ip则需指定ip,即consul members -http-addr=10.40.66.146:8500);
-bootstrap(bootstrap):使用bootstrap模式,只有一个server的时候可以设置这个参数,此模式允许自我选为Leader;
-bootstrap-expect(bootstrap_expect):要么设置,要么不设置,当设置此参数时,集群中的server必须达到此数量才能触发启动集群,此参数不能与- bootstrap同时使用;
ui:是否使用默认UI界面,可以使用"ui_dir": "/home/consul/consul_ui",指定自定义UI界面,需要在此目录放入界面静态服务文件;
log_file(-log-file):指定consul日志文件,若指定到目录(此目录必须事先创建),则日志格式类似consul-1537239737396229691.log,也可以自定义日志 文件名称;
-syslog(enable_syslog): 是否允许写入系统日志;
disable_update_check:禁止consul检查更新,不然后台总是不管打印更新失败。
7. 配置systemctl启动:/lib/systemd/system/consul.service
[Unit]
Description=Consul Server
After=network.target
[Service]
ExecStart=/usr/local/bin/consul agent -config-dir /home/consul/config-dir.d
ExecStop=/usr/local/bin/consul leave
ExecReload=/usr/local/bin/consul reload -config-dir /home/consul/config-dir.d
KillSignal=SIGINT
Restart=on-failure
RestartSec=1
[Install]
WantedBy=default.target
注释:ExecReload=/bin/kill -HUP $MAINPID
8. 启动服务并配置开机启动
systemctl start consul.service
systemctl enable consul.service
systemctl status consul.service
9. consul常用命令
consul --help
consul version
consul info
consul members
consul force-leave :强制集群中某个member进入“left”状态。注意,如果member实际上还是alive,它会rejoin the cluster。
这个方法的真正意图是强制移除那些“failed”的节点;
consul reload:触发重新加载配置文件。等于kill -1或kill -HUP
consul leave : 优雅的离开并shutdown;
consul join 集群主机IP:加入集群,例如在 172.20.80.193 中 : consul join 172.20.80.197 172.20.80.198
-bash-4.2# consul members
Node Address Status Type Build Protocol DC Segment
consul-s-1 172.20.80.193:8301 alive server 1.2.3 2 dc1 <all>
consul-s-2 172.20.80.195:8301 alive server 1.2.3 2 dc1 <all>
consul-s-3 172.20.80.196:8301 alive server 1.2.3 2 dc1 <all>
consul-c-1 172.20.80.194:8301 alive client 1.2.3 2 dc1 <default>
-bash-4.2# consul operator raft list-peers
Node ID Address State Voter RaftProtocol
consul-s-2 a474d3bc-1380-7c69-794a-3f30c6c71636 172.20.80.195:8300 leader true 3
consul-s-3 f00591e2-53d9-524f-db34-ff811134fa09 172.20.80.196:8300 follower true 3
consul-s-1 1f1f4b54-c28e-4baa-4dbc-8a0df477142c 172.20.80.193:8300 follower true 3