转载自 Consul集群搭建
概述
作为服务发现的几种产品,比较可以查看这里。Consul官方也提供了几种产品之间的比较,点击查看。
服务发现产品
Consul有很多组件,但总体来说,它是一个发现和配置服务工具,特性:
服务发现 Service Discovery: Clients of Consul can provide a service, such as api or mysql, and other clients can use Consul to discover providers of a given service. Using either DNS or HTTP, applications can easily find the services they depend upon.
健康检查 Health Checking: Consul clients can provide any number of health checks, either associated with a given service (“is the webserver returning 200 OK”), or with the local node (“is memory utilization below 90%”). This information can be used by an operator to monitor cluster health, and it is used by the service discovery components to route traffic away from unhealthy hosts.
key-value存储 KV Store: Applications can make use of Consul’s hierarchical key/value store for any number of purposes, including dynamic configuration, feature flagging, coordination, leader election, and more. The simple HTTP API makes it easy to use.
多数据中心 Multi Datacenter: Consul supports multiple datacenters out of the box. This means users of Consul do not have to worry about building additional layers of abstraction to grow to multiple regions.
更详细的信息可以查看官网。
选择了三台服务器,ip地址分别为192.168.1.12、192.168.1.13、192.168.1.14。
安装Consul
下载地址是:https://www.consul.io/downloads.html
从里面选择系统对应的版本,我使用了Linux 64-bit,Consul版本为1.1.0。下载完成后解压缩,只有一个文件,将文件添加到环境变量或者移动到已有环境变量的目录中。
sudo wget https://releases.hashicorp.com/consul/1.1.0/consul_1.1.0_linux_amd64.zip
sudo apt-get install unzip
unzip consul_1.1.0_linux_amd64.zip
sudo cp consul /usr/local/sbin/
三台服务器上都完成上述相同的步骤。
如果是Mac,可以使用Homebrew进行安装
brew install consul
启动Consul
192.168.1.12上运行
consul agent -server -bootstrap -bind=0.0.0.0 -client=192.168.1.12 -data-dir=/home/ubuntu/data -ui -node=s12
192.168.1.13上运行
consul agent -server -bind=0.0.0.0 -client=192.168.1.13 -data-dir=/home/ubuntu/data -ui -node=s13 -join 192.168.1.12
192.168.1.14上运行
consul agent -server -bind=0.0.0.0 -client=192.168.1.14 -data-dir=/home/ubuntu/data -ui -node=s14 -join 192.168.1.12
参数说明:
server: 以server身份启动。
data-dir:data存放的目录
node:节点id,在同一集群不能重复。
bind:监听的ip地址。
client 客户端的ip地址
其他参数见consul官方说明: https://www.consul.io/docs/agent/options.html
查看
网页查看:http://192.168.1.12:8500/
查看Leader: http://192.168.1.12:8500/v1/status/leader
查看Peers: http://192.168.1.12:8500/v1/status/peers
版本信息:
consul -v
使用命令有
consul members:查看集群成员
consul info:查看当前服务器的状况
consul leave:退出当前服务集群
其他信息也可以点击查看这里。