consul官方文档:https://www.consul.io/intro/index.html
1、consul安装
wget https://releases.hashicorp.com/consul/0.8.1/consul_0.8.1_linux_amd64.zip
unzip consul_0.8.1_linux_amd64.zip
mv consul /usr/local/bin/
2、验证安装
安装Consul后,通过执行consul命令,输出如下:
# consul
usage: consul [--version] [--help] <command> [<args>]
Available commands are:
agent Runs a Consul agent
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
snapshot Saves, restores and inspects snapshots of Consul server state
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
3、启动consul
运行Consul代理
Consul是典型的C/S架构,可以运行服务模式或客户模式。
每一个数据中心必须有至少一个服务节点,3到5个服务节点最好。非常不建议只运行一个服务节点,因为在节点失效的情况下数据有极大的丢失风险。
其它的所有节点都运行在客户端模式下,一个客户节点是非常轻量的注册服务进程,用来处理健康检查,转发请求到服务节点等事务,代理必须在集群中的每个节点上运行。
现在10.10.67.106机器上启动consul使之在后台运行,命令如下:
consul agent -server -bootstrap-expect 2 -data-dir /tmp/consul -config-dir /etc/consul.d/ -advertise 10.10.67.106 -node=Node-106 -bind=10.10.67.106 -datacenter=dc1&
查看集群成员:
# consul members
Node Address Status Type Build Protocol DC
Node-106 10.10.67.106:8301 alive server 0.8.1 2 dc1
然后在10.10.67.107上启动consul,但是需要选取一个leader,可以使用命令将此节点加入Node-106,然后自动选举出leader,命令如下:
consul agent -server -bootstrap-expect 2 -data-dir /tmp/consul -config-dir /etc/consul.d/ -advertise 10.10.67.107 -node=Node-107 -join=10.10.67.106 -bind=10.10.67.107 -dc=dc1&
输入此命令后,可以查看106机器上的打印信息,显示leader选取成功!
查看leader:
在任一节点机器上输入:# curl 127.0.0.1:8500/v1/status/leader
# curl 127.0.0.1:8500/v1/status/leader
"10.10.67.106:8300"
查看peers:
# curl 127.0.0.1:8500/v1/status/peers
["10.10.67.106:8300","10.10.67.107:8300"]
查看集群成员:
# consul members -detailed
Node Address Status Tags
Node-106 10.10.67.106:8301 alive build=0.7.4:'1c442cb,dc=dc1,expect=2,id=421f2625-4063-576e-105f-edbf72630050,port=8300,role=consul,vsn=2,vsn_max=3,vsn_min=2
Node-107 10.10.67.107:8301 alive build=0.7.4:'1c442cb,dc=dc1,expect=2,id=421f21dd-b998-4055-ef16-1088bef0b2ec,port=8300,role=consul,vsn=2,vsn_max=3,vsn_min=2
至此,consul集群搭建成功,我搭建consul集群,仅作为测试使用,所以仅部署了2个节点,如果需要部署3个节点,启动命令将-bootstrap-expect参数改为3即可!
4、启动失败,重启即可
在启动的过程中,遇到启动失败的情况,解决办法是删除启动时指定路径下的数据,kill进程,重新启动即可!
# cd /tmp/consul
# ll
total 16
-rw-r--r--. 1 root root 394 Oct 13 19:00 checkpoint-signature
-rw-------. 1 root root 36 Oct 13 18:59 node-id
drwxr-xr-x. 3 root root 4096 Oct 13 18:59 raft
drwxr-xr-x. 2 root root 4096 Oct 17 12:02 serf
# pwd
/tmp/consul
# rm -rf *
删除后,查看进程
# ps -ef | grep -w "consul" | grep -v grep | grep -v log
root 8959 1 0 Oct13 ? 00:24:55 consul agent -server -bootstrap-expect 2 -data-dir /tmp/consul -config-dir /etc/consul.d/ -advertise 10.10.67.107 -node=Node-107 -join=10.10.67.106 -bind=10.10.67.107 -dc=dc1
# kill -9 8959
然后重新启动即可!
5、查看KV
查看所有的kv
curl 127.0.0.1:8500/v1/kv/?recurse
查看单个KV
curl 127.0.0.1:8500/v1/kv/Key
6、查看services
curl 127.0.0.1:8500/v1/agent/services
7、删除kv
curl -X DELETE http://127.0.0.1:8500/v1/kv/Key
8、删除service
curl --request PUT http://127.0.0.1:8500/v1/agent/service/deregister/serviceID