1.问题描述
刚搭建使用以下命令下载并安装的consul服务。
wget https://releases.hashicorp.com/consul/1.4.4/consul_1.4.4_linux_amd64.zip
unzip consul_1.4.4_linux_amd64.zip
sudo mv consul /usr/local/bin/
可见consul的安装十分简单,但以下基本操作均出现问题:
1) 绑定节点
consul agent -dev -bind=192.168.108.119
Error starting agent: Failed to start Consul server: Failed to start RPC layer: listen tcp 192.168.108.119:8300: bind: cannot assign requested address
2)向集群中增加节点
consul join 192.168.108.119 192.168.108.19
Error joining address '192.168.108.19': Put http://127.0.0.1:8500/v1/agent/join/192.168.108.19: dial tcp 127.0.0.1:8500: connect: connection refused
Error joining address '192.168.108.19': Put http://127.0.0.1:8500/v1/agent/join/192.168.108.19: dial tcp 127.0.0.1:8500: connect: connection refused
Failed to join any nodes.
3)查询集群中成员
consul members
Error retrieving members: Get http://127.0.0.1:8500/v1/agent/members?segment=_all: dial tcp 127.0.0.1:8500: connect: connection refused
2.问题分析
同时报以上几个错误,表明应该是某个关键性的配置出现问题。
从官网上获得提示,应该是端口问题,经过测试发现8300,8500,8600等端口均处于关闭状态
3.解决办法
经过分析发现是没有成功配置节点,按以下方式成功配置节点即可:
1)将当前节点设为server类型的节点,同时默认为leader
consul agent -server -bootstrap -ui-dir /etc/consul_ui -data-dir /tmp/consul -node master1
2) 将当前节点设置为server类型的节点
consul agent -server -data-dir /tmp/consul -node master2
consul agent -server -data-dir /tmp/consul -node master3
3)将其他节点手动加入到集群当中
consul join 192.168.108.117 192.168.108.19
4)查看集群情况
consul members
至此一个consul集群才算真正搭建起来了。
4.知识拓展
1)通过配置文件的方式来进行节点配置
#新建一个文件夹用来存放配置文件
sudo mkdir /etc/consul.d
2)新建配置文件
sudo vim /ect/consul.d/basic_config.json
{
"datacenter": "pcl-aws1",
"data_dir": "/tmp/consul",
"log_level": "INFO",
"node_name": "master1",
"server": true,
"bind_addr":"192.168.108.119","192.168.108.19","192.168.108.117",
"addresses": {
"https": "0.0.0.0"
},
"ports": {
"https": 8080
},
"key_file": "/etc/pki/tls/private/my.key",
"cert_file": "/etc/pki/tls/certs/my.crt",
"ca_file": "/etc/pki/tls/certs/ca-bundle.crt"
}
3)指定配置文件启动
consul agent -dev -bind=192.168.108.117 -config-dir /etc/consul.d/
4)允许其他主机访问