本文共 2681 字,大约阅读时间需要 8 分钟。
一、环境
操作系统版本: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/consulKillSignal=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/mykeycurl localhost:8500/v1/kv/mykey # will show you the keycurl localhost:8500/v1/kv/mykey # nothing is returnedcurl 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.confrm -rf /etc/logrotate.d/consulecho ':programname, isequal, "consul" /var/log/consul.log' >> /etc/rsyslog.d/consul.confecho '& ~' >> /etc/rsyslog.d/consul.confecho '/var/log/consul.log' >> /etc/logrotate.d/consulecho '{' >> /etc/logrotate.d/consulecho ' daily' >> /etc/logrotate.d/consulecho ' rotate 7' >> /etc/logrotate.d/consulecho ' missingok' >> /etc/logrotate.d/consulecho ' dateext' >> /etc/logrotate.d/consulecho ' copytruncate' >> /etc/logrotate.d/consulecho ' compress' >> /etc/logrotate.d/consulecho '}' >> /etc/logrotate.d/consul重启服务/bin/systemctl restart consul配置开机启动echo "/bin/systemctl start consul" >> /etc/rc.local 本文转自aaron428 51CTO博客,原文链接:http://blog.51cto.com/aaronsa/2064886,如需转载请自行联系原作者