生产环境Consul安装升级
Consul目录规划
- Consul安装目录
/usr/local/consul-VERSION
(VERSION为版本号),安装完成后软链接到/usr/local/consul
。 - Consul二进制程序目录
/usr/local/consul/bin
- Consul配置文件目录为
/usr/local/consul/consul.d
- Consul数据目录为
/usr/local/consul/data
- Consul日志目录为
/usr/local/consul/logs
- Consul快照目录为
/usr/local/consul/snapshot
完整目录结构如下:
~]# tree -C -L 1 /usr/local/consul
/usr/local/consul
├── bin
├── consul.d
├── data
├── logs
└── snapshot
Consul安装
创建相应目录
~]# mkdir -p /usr/local/consul-1.5.1
~]# ln -s /usr/local/consul-1.5.1/ /usr/local/consul
~]# mkdir -p /usr/local/consul/{bin,consul.d,data,logs,snapshot}
下载程序包并解压
官网下载地址:https://releases.hashicorp.com/consul/
下载软件包:
~]# wget https://releases.hashicorp.com/consul/1.5.1/consul_1.5.1_linux_amd64.zip
解压
~]# unzip consul_1.5.1_linux_amd64.zip -d /usr/local/consul/bin/
导出环境变量
~]# echo 'export PATH=$PATH:/usr/local/consul/bin' > /etc/profile.d/consul.sh
~]# source /etc/profile.d/consul.sh
Consul配置
Consul 可通过命令行指定具体启动参数或者指定配置文件目录启动为前台进程。我们可以通过nohup或者systemd等等启动为后台进程。这里我们通过systemd启动为后台进程。
在生产环境中强烈推荐使用json或hcl格式的配置文件进行启动,配置文件提供了某些命令行无法提供的参数和功能,如开启prometheus监控等等。
systemd通过指定全部命令行参数启动
Consul启动参数配置
Consul启动参数配置文件为/etc/sysconfig/consul
。配置文件参数可酌情更改。
Consul-Server配置
############ consul-1.5.1配置#############
~]# cat /etc/sysconfig/consul
# Consul start options
OPTIONS="\
-server \
-datacenter=dc1 \
-node=consul-server-1 \
-bind=10.114.0.59 \
-client=0.0.0.0 \
-config-dir=/usr/local/consul/consul.d/ \
-data-dir=/usr/local/consul/data/ \
-bootstrap-expect=3 \
-join=10.114.0.59 \
-rejoin \
-ui \
-pid-file=/run/consul-server.pid \
-log-file=/usr/local/consul/logs/ \
-log-level=info"
Consul-Client配置
############ consul-1.5.1配置#############
~]# cat /etc/sysconfig/consul
# Consul start options
OPTIONS="\
-datacenter=dc1 \
-node=consul-client-1 \
-bind=10.114.0.61 \
-client=0.0.0.0 \
-config-dir=/usr/local/consul/consul.d/ \
-data-dir=/usr/local/consul/data/ \
-join=10.114.0.59 \
-ui \
-pid-file=/run/consul-client.pid \
-log-file=/usr/local/consul/logs/ \
-log-level=info"
Consul启动Unit配置(CentOS 7)
Consul启动Unit配置文件为/usr/lib/systemd/system/consul.service
。配置文件参数可酌情更改。需配置server端和client端。
~]# cat /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
ConditionFileNotEmpty=/etc/sysconfig/consul
[Service]
User=root
Group=root
EnvironmentFile=/etc/sysconfig/consul
# set GOMAXPROCS to number of processors
Environment=GOMAXPROCS=$(nproc)
ExecStart=/usr/local/consul/bin/consul agent $OPTIONS
ExecReload=/usr/local/consul/bin/consul reload
KillMode=process
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
修改完Unit文件后需重新载入
~]# systemctl daemon-reload
Consul启动
启动Server端和Client端
~]# systemctl start consul
~]# systemctl enable consul
systemd通过指定配置文件目录启动
配置文件支持JSON和HCL格式
配置文件模板
~]# cat config.example
# The configuration file must be in JSON or HCL format.
# Consul server agents typically require a superset of configuration required by
# Consul client