一、环境
操作系统版本:centos7.2
操作系统内核:4.8.4
系统工具:yum
数据分区(200G容量以上):/data
启用端口:8300、8301、8302、8500、8600
二、下载安装文件
下载安装文件:wget https://releases.hashicorp.com/consul/0.9.2/consul_0.9.2_linux_amd64.zip
三、解压安装文件
unzip consul_0.9.2_linux_amd64.zip -d /usr/bin
四、创建启动文件
编辑文件: vim /lib/systemd/system/consul.service
#####以下为文件内容######
[Unit]
Description=consul
[Service]
ExecStart=/usr/bin/consul agent -config-dir /etc/consul
KillSignal=SIGINT
五、创建配置文件
创建文件存放路径: mkdir /etc/consul/
编辑文件: vim /etc/consul/server.json
#####以下为server端文件内容,请注意中文说明部分需要修改######
{
"datacenter": "数据中心名称",
"data_dir": "/tmp/consul",
"log_level": "INFO",
"node_name": "主机名",
"server": true,
"acl_enforce_version_8":false,
"bootstrap_expect": 3,
"client_addr": "0.0.0.0",
"advertise_addr": "主机IP地址",
"advertise_addr_wan": "主机外网IP地址",
"domain":"consul",
"ports":{
"dns": 53
},
"telemetry": {
"statsd_address": "127.0.0.1:8125"
},
"acl_datacenter": "数据中心名称",
"acl_agent_token": "请自行创建访问token(字符串即可)",
"acl_master_token": "请自行创建访问token(字符串即可)",
"acl_default_policy": "deny"
}
#####以下为clent端配置文件############
{
"datacenter": "数据中心名称",
"data_dir": "/tmp/consul",
"log_level": "INFO",
"node_name": "主机名",
"acl_enforce_version_8":false,
"acl_datacenter": "数据中心名称",
"bind_addr":"主机IP地址",
"domain":"consul",
"telemetry": {
"statsd_address": "127.0.0.1:8125"
},
"rejoin_after_leave":true,
"enable_script_checks":true,
"acl_agent_token": "server端配置的token",
"acl_master_token": "server端配置的token",
"acl_default_policy": "deny"
}
六、启动服务
systemctl start consul
七、查看日志
journalctl -fu consul
八、服务测试
curl -X PUT -d ‘myvalue‘ localhost:8500/v1/kv/mykey
curl localhost:8500/v1/kv/mykey # will show you the key
curl localhost:8500/v1/kv/mykey # nothing is returned
curl localhost:8500/v1/kv/mykey?dc=[id_other_datacenter] # will again show you the key
九、集群节点添加
consul join 集群主机IP
十、多数据中心添加
consul join -wan [public_ip_address_other_server]
十一、使用技巧
日志配置
rm -rf /etc/rsyslog.d/consul.conf
rm -rf /etc/logrotate.d/consul
echo ‘:programname, isequal, "consul" /var/log/consul.log‘ >> /etc/rsyslog.d/consul.conf
echo ‘& ~‘ >> /etc/rsyslog.d/consul.conf
echo ‘/var/log/consul.log‘ >> /etc/logrotate.d/consul
echo ‘{‘ >> /etc/logrotate.d/consul
echo ‘ daily‘ >> /etc/logrotate.d/consul
echo ‘ rotate 7‘ >> /etc/logrotate.d/consul
echo ‘ missingok‘ >> /etc/logrotate.d/consul
echo ‘ dateext‘ >> /etc/logrotate.d/consul
echo ‘ copytruncate‘ >> /etc/logrotate.d/consul
echo ‘ compress‘ >> /etc/logrotate.d/consul
echo ‘}‘ >> /etc/logrotate.d/consul
重启服务
/bin/systemctl restart consul
配置开机启动
echo "/bin/systemctl start consul" >> /etc/rc.local
原文地址:http://blog.51cto.com/aaronsa/2064886
时间: 2024-11-03 22:50:51