本文共 6352 字,大约阅读时间需要 21 分钟。
consul集群搭建:
一、软件安装Linux 环境下载zip包然后直接解压,然后把解压的文mv consul /bin检验安装是否成功,查看版本[root@node1 ~]consul -vConsul v1.1.0Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)Consul 软件下载地址:默然仓库地址:查看版本:[root@node1 raft]# consul -vConsul v1.1.0Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)[root@node1 raft]#二、集群配置:
集群配置思路需要考虑问题:搭建consul,需要先理解conusl的server leader定制选举1、通过配置文件中设置"bootstrap_expect":的2、先手动自定主,然后其他两个节点join进来方式1:命令启动的方式
启动方式:/usr/bin/consul agent -config-file=/etc/consul/consul.json第一个节点:./consul agent -server -bootstrap-expect 2 -data-dir /opt/consul-data -node=node1 -bind=ip1 -ui -client=0.0.0.0 &其他节点:./consul agent -server -bootstrap-expect 2 -data-dir /opt/consul-data -node=node2 -bind=ip2 -join= ip1 -ui -client=0.0.0.0 &./consul agent -server -bootstrap-expect 2 -data-dir /opt/consul-data -node=node3 -bind=ip3 -join= ip1 -ui -client=0.0.0.0 &方式2:配置文件的方式:
[root@node1 ~]# cat /etc/consul/consul.json { "data_dir": "/opt/consul-data","log_level": "INFO","server": true,"bootstrap_expect": 1,"retry_join": ["172.16.36.67","172.16.36.50"],"retry_interval": "3s","rejoin_after_leave": true,"domain": "ycgwl.com","client_addr":"0.0.0.0","ui": true,"datacenter": "dc1"}注意:leader是"bootstrap_expect": 1,Flower:是“"bootstrap_expect": 2,查看集群节点是够已经正常启动:
[root@node1 ~]# consul membersNode Address Status Type Build Protocol DC Segmentnode1 172.16.36.56:8301 alive server 1.1.0 2 dc1 <all>node2 172.16.36.67:8301 alive server 1.1.0 2 dc1 <all>node3 172.16.36.50:8301 alive server 1.1.0 2 dc1 <all>命令的方式启动leader:
./consul agent -server –bootstrap-expect 1 –data-dir /tmp/consul -node node1 –bind=10.10.49.193 –ui –client 0.0.0.0 -dc dc1 &i. server: 以server身份启动。ii. bootstrap-expect:集群要求的最少server数量,当低于这个数量,集群即失效。经测试,低于这个数量也不影响访问iii. data-dir:data存放的目录,更多信息请参阅consul数据同步机制iv. node:节点id,在同一集群不能重复。v. bind:监听的ip地址。vi. client 客户端的ip地址vii. & :在后台运行,此为linux脚本语法viii. ui:启动webui,端口8500viiii.:-config-dir 指定服务配置文件的目录(这个目录下的所有.json文件,作为服务配置文件读取)访问ip:8500/ui,出现如下页面,则启动成功
Consul agent-ui命令启动:
nohup /usr/bin/consul agent -config-file=/etc/consul/consul.json &定制consul的服务端system启动方式:root@node1]# cat /usr/lib/systemd/system/consul.service [Unit]Description=consulAfter=network[Service]
Type=simplePIDFile=/usr/local/consul/consul.pidExecStart=/usr/bin/consul agent -config-file=/etc/consul/consul.jsonExecReload=/usr/bin/consul reload[Install]
WantedBy=multi-user.target定制consul的客户端system启动方式:
[root@node1 consul]# cat consul.service [Unit]Description=consulAfter=network[Service]
Type=simplePIDFile=/opt/consul-data/consul.pidExecStart=/opt/consul agent -join 172.16.37.39 -data-dir=/opt/consul-data -datacenter=zongbuExecReload=/opt/consul reload[Install]
WantedBy=multi-user.target服务管理:
systemctl enable consul.servicesystemctl status consul.servicesystemctl enable consulservice consul startservice consul sttopservice consul stopserver agent进程:
[root@node2 opt]# netstat -tulnp | grep consultcp 0 0 172.16.36.67:8300 0.0.0.0: LISTEN 23384/consul tcp 0 0 172.16.36.67:8301 0.0.0.0: LISTEN 23384/consul tcp 0 0 172.16.36.67:8302 0.0.0.0: LISTEN 23384/consul tcp6 0 0 :::8500 ::: LISTEN 23384/consul tcp6 0 0 :::8600 ::: LISTEN 23384/consul udp 0 0 172.16.36.67:8301 0.0.0.0: 23384/consul udp 0 0 172.16.36.67:8302 0.0.0.0: 23384/consul udp6 0 0 :::8600 ::: 23384/consulclinet agent 进程:
[root@etcd ~]# netstat -tulnp | grep consultcp 0 0 127.0.0.1:8500 0.0.0.0: LISTEN 29980/./consul tcp 0 0 127.0.0.1:8600 0.0.0.0: LISTEN 29980/./consul tcp6 0 0 :::8301 ::: LISTEN 29980/./consul udp 0 0 127.0.0.1:8600 0.0.0.0: 29980/./consul udp6 0 0 :::8301 :::* 29980/./consul [root@etcd ~]#各个通信端口作用:
• 8500,客户端http api接口,web页面管理• 8600,客户端DNS服务端口• 8400,客户端RPC通信端口• 8300,集群server RPC通信接口• 8301,集群DC内部通信接口• 8302,集群DC之间通信接口查看leader角色:
[root@node1 raft]# consul operator raft list-peersNode ID Address State Voter RaftProtocolnode1 016a92e7-b9ff-1dd7-f758-38fb3b2a9088 172.16.37.39:8300 leader true 3列出所有数据中心:
[root@node1 ~]# consul catalog datacenters -http-addr=IP:8500zongbu[root@node1 ~]#列出所有节点:
[root@node2 system]# consul catalog nodes Node ID Address DCnode1 0ffe6841 172.16.36.56 dc1node2 a6b9b6d7 172.16.36.67 dc1node3 23eb28a2 172.16.36.50 dc1列出所有服务:
[root@node1 opt]# ./consul catalog services -http-addr=IP:8500Consul列出数据中心:
[root@node2 system]# consul catalog datacentersdc1zongbu命令查看consul节点:
curl 172.16.37.35:8500/v1/catalog/nodes把client节点加到server:
把172.16.37.10节点加入到consul./consul join -wan clientIP -http-addr=遇到问题1、网卡有多个,网卡冲突
报错解决:Multiple private IPv4 addresses found. Please configure one with ‘bind’ and/or ‘advertise’.[root@etcd ~]# [root@node1 ~]# /usr/bin/consul agent -config-file=/etc/consul/consul.json==> Multiple private IPv4 addresses found. Please configure one with 'bind' and/or 'advertise'.[root@node1 ~]# ps -ef | grep flanneroot 664 1 0 09:57 ? 00:00:00 /usr/bin/flanneld -etcd-endpoints= -etcd-prefix=/k8s/network --iface=eth0root 4721 710 0 11:08 pts/0 00:00:00 grep --color=auto flanne遇到问题2、system服务启动不了:consul软件路径写错了
[root@node1]# systemctl enable consul.serviceFailed to execute operation: Bad message[root@node1]# sudo systemctl enable consul.serviceFailed to execute operation: Bad message[root@node1]# systemctl -f enable consul.serviceFailed to execute operation: Bad message[root@node1]# ls /usr/lib/systemd/system/consul.service/usr/lib/systemd/system/consul.service经过检查,结果发现:opt/consul-agent/consul路径写错了少了consul-agent这级目录[root@etcd ~]# ls /usr/lib/systemd/system/consul.service/usr/lib/systemd/system/consul.service[root@etcd ~]# cat /usr/lib/systemd/system/consul.service [Unit]Description=consulAfter=network[Service]
Type=simplePIDFile=/opt/consul-data/consul.pidExecStart=/opt/consul-agent/consul agent -join 172.16.36.50 -data-dir=/opt/consul-data ExecReload=/opt/consul reload[Install]
WantedBy=multi-user.target后续:
• consul 采用DNS或者http获取服务信息,没有主动通知,需要自己轮训获取• consul之间如何做服务健康检查?• • dig解析:linux下提供nslookup,dig命令的软件就是 bind-utils• consul的ACL控制:•博客参考:
• 博客参考地址:• • • •RPC协议:
consul相关资源
• 可执行程序下载地址: ...• 官方说明文档: ...• api说明文档: ...• golang api代码位置:github.com/hashicorp/consul/api• Consul官方文档:转载于:https://blog.51cto.com/firephoenix/2133105