官网地址:https://www.consul.io/docs/intro 截止当前,最新版本是1.13,本篇使用的还是1.0.6版本的方式,可能会有少许偏差。
简介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.
安装流程 下载文件安装下载地址:https://www.consul.io/downloads.html 解压缩安装包,并把目录解压缩出来的文件夹添加到环境变量即可。 当然,也可以直接把解压缩出来的consul文件直接放到现有环境变量目录中,比如ubuntu下
sudo scp consul /usr/local/bin/
Mac下安装
除了上面的方法,mac下也可以使用homebrew安装
brew install consul
查看
版本信息
consul -v
其他提示
consul
Usage: consul [--version] [--help] []
Available commands are:
agent Runs a Consul agent
catalog Interact with the catalog
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
开发者模式启动
consul agent -dev
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.0.6'
Node ID: '2d31fc4d-698b-b0f7-61f6-edc7878ce192'
Node name: 'bogon'
Datacenter: 'dc1' (Segment: '')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false
==> Log data will now stream in as it occurs:
2018/04/08 14:52:03 [DEBUG] Using random ID "2d31fc4d-698b-b0f7-61f6-edc7878ce192" as node ID
2018/04/08 14:52:03 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:2d31fc4d-698b-b0f7-61f6-edc7878ce192 Address:127.0.0.1:8300}]
2018/04/08 14:52:03 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2018/04/08 14:52:03 [INFO] serf: EventMemberJoin: bogon.dc1 127.0.0.1
2018/04/08 14:52:03 [INFO] serf: EventMemberJoin: bogon 127.0.0.1
2018/04/08 14:52:03 [INFO] consul: Adding LAN server bogon (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2018/04/08 14:52:03 [INFO] consul: Handled member-join event for server "bogon.dc1" in area "wan"
2018/04/08 14:52:03 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
2018/04/08 14:52:03 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
2018/04/08 14:52:03 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
2018/04/08 14:52:03 [INFO] agent: started state syncer
2018/04/08 14:52:03 [WARN] raft: Heartbeat timeout from "" reached, starting election
2018/04/08 14:52:03 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2018/04/08 14:52:03 [DEBUG] raft: Votes needed: 1
2018/04/08 14:52:03 [DEBUG] raft: Vote granted from 2d31fc4d-698b-b0f7-61f6-edc7878ce192 in term 2. Tally: 1
2018/04/08 14:52:03 [INFO] raft: Election won. Tally: 1
2018/04/08 14:52:03 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2018/04/08 14:52:03 [INFO] consul: cluster leadership acquired
2018/04/08 14:52:03 [INFO] consul: New leader elected: bogon
2018/04/08 14:52:03 [DEBUG] consul: Skipping self join check for "bogon" since the cluster is too small
2018/04/08 14:52:03 [INFO] consul: member 'bogon' joined, marking health alive
2018/04/08 14:52:03 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically
2018/04/08 14:52:03 [INFO] agent: Synced node info
2018/04/08 14:52:03 [DEBUG] agent: Node info in sync
2018/04/08 14:52:06 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically
2018/04/08 14:52:06 [DEBUG] agent: Node info in sync
其中: -dev(该节点的启动不能用于生产环境,因为该模式下不会持久化任何状态),该启动模式仅仅是为了快速便捷的启动单节点consul 该节点处于server模式 该节点是leader 该节点是一个健康节点
查看节点信息查看consul cluster中的每一个consul节点的信息
consul members
Node Address Status Type Build Protocol DC Segment
fymod 127.0.0.1:8301 alive server 1.0.6 2 dc1
Address:节点地址 Status:alive表示节点健康 Type:server运行状态是server状态 DC:dc1表示该节点属于DataCenter1 注意: members命令的输出是基于gossip协议的,并且是最终一致的(也就是说,某一个时刻你去运用该命令查到的consul节点的状态信息可能是有误的)
输入http://localhost:8500/ui/ 访问Consul,可查看到如下界面:
consul agent 命令详解 输入consul agent --help ,可以看到consul agent 的选项,如下:
bogon:~ dev$ consul agent --help
Usage: consul agent [options]
Starts the Consul agent and runs until an interrupt is received. The
agent represents a single node in a cluster.
HTTP API Options
-datacenter=
Datacenter of the agent.
作用:指定机器加入到哪一个数据中心中
Command Options
-advertise=
Sets the advertise address to use.
-advertise-wan=
Sets address to advertise on WAN instead of -advertise address.
-bind=
Sets the bind address for cluster communication.
作用:指明节点的IP地址,有时候不指定绑定IP,会报Failed to get advertise address: Multiple private IPs found. Please configure one. 的异常
-bootstrap
Sets server to bootstrap mode.
-bootstrap-expect=
Sets server to expect bootstrap mode.
作用:该命令通知consul server我们现在准备加入的server节点个数,该参数是为了延迟日志复制的启动直到我们指定数量的server节点成功的加入后启动。
-client=
Sets the address to bind for client access. This includes RPC, DNS,
HTTP and HTTPS (if configured).
作用:指定节点为client,指定客户端接口的绑定地址,包括:HTTP、DNS、RPC。默认是127.0.0.1,只允许回环接口访问。若不指定为-server,其实就是-client
-config-dir=
Path to a directory to read configuration files from. This
will read every file ending in '.json' as configuration in this
directory in alphabetical order. Can be specified multiple times.
作用:指定service的配置文件和检查定义所在的位置。通常会指定为”某一个路径/consul.d”(通常情况下,.d表示一系列配置文件存放的目录)
-config-file=
Path to a JSON file to read configuration from. Can be specified
multiple times.
作用:指定一个要装载的配置文件。该选项可以配置多次,进而配置多个配置文件(后边的会合并前边的,相同的值覆盖)
-config-format=
Config files are in this format irrespective of their extension.
Must be 'hcl' or 'json'
-data-dir=
Path to a data directory to store agent state.
作用:指定agent储存状态的数据目录。这是所有agent都必须的。对于server尤其重要,因为他们必须持久化集群的状态
-dev
Starts the agent in development mode.
作用:创建一个开发环境下的server节点。该参数配置下,不会有任何持久化操作,即不会有任何数据写入到磁盘。这种模式不能用于生产环境。
-disable-host-node-id
Setting this to true will prevent Consul from using information
from the host to generate a node ID, and will cause Consul to
generate a random node ID instead.
-disable-keyring-file
Disables the backing up of the keyring to a file.
-dns-port=
DNS port to use.
-domain=
Domain to use for DNS interface.
-enable-script-checks
Enables health check scripts.
-encrypt=
Provides the gossip encryption key.
-hcl=
hcl config fragment. Can be specified multiple times.
-http-port=
Sets the HTTP API port to listen on.
-join=
Address of an agent to join at start time. Can be specified
multiple times.
作用:将节点加入到集群
-join-wan=
Address of an agent to join -wan at start time. Can be specified
multiple times.
-log-level=
Log level of the agent.
-node=
Name of this node. Must be unique in the cluster.
作用:指定节点在集群中的名称。该名称在集群中必须是唯一的(默认采用机器的host)推荐:直接采用机器的IP
-node-id=
A unique ID for this node across space and time. Defaults to a
randomly-generated ID that persists in the data-dir.
-node-meta=
An arbitrary metadata key/value pair for this node, of the format
`key:value`. Can be specified multiple times.
-non-voting-server
(Enterprise-only) This flag is used to make the server not
participate in the Raft quorum, and have it only receive the data
replication stream. This can be used to add read scalability to
a cluster in cases where a high volume of reads to servers are
needed.
-pid-file=
Path to file to store agent PID.
-protocol=
Sets the protocol version. Defaults to latest.
-raft-protocol=
Sets the Raft protocol version. Defaults to latest.
-recursor=
Address of an upstream DNS server. Can be specified multiple times.
-rejoin
Ignores a previous leave and attempts to rejoin the cluster.
-retry-interval=
Time to wait between join attempts.
-retry-interval-wan=
Time to wait between join -wan attempts.
-retry-join=
Address of an agent to join at start time with retries enabled. Can
be specified multiple times.
-retry-join-wan=
Address of an agent to join -wan at start time with retries
enabled. Can be specified multiple times.
-retry-max=
Maximum number of join attempts. Defaults to 0, which will retry
indefinitely.
-retry-max-wan=
Maximum number of join -wan attempts. Defaults to 0, which will
retry indefinitely.
-segment=
(Enterprise-only) Sets the network segment to join.
-serf-lan-bind=
Address to bind Serf LAN listeners to.
-serf-wan-bind=
Address to bind Serf WAN listeners to.
-server
Switches agent to server mode.
作用:指定节点为server。每个数据中心(DC)的server数推荐至少为1,至多为5。所有的server都采用raft一致性算法来确保事务的一致性和线性化,事务修改了集群的状态,且集群的状态保存在每一台server上保证可用性。server也是与其他DC交互的门面(gateway)
-syslog
Enables logging to syslog.
-ui
Enables the built-in static web UI server.
-ui-dir=
Path to directory containing the web UI resources.
使用-client 参数可指定允许客户端使用什么ip去访问,例如-client 192.168.1.100 表示可以使用http://192.168.1.100:8500/ui 去访问。
我们尝试一下: consul agent -dev -client 192.168.1.100 发现果然可以使用http://192.168.1.100:8500/ui 访问了。
集群部分移步 http://www.zhaoguojian.com/2018/06/23/Consul%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA/ 和 https://blog.csdn.net/u010046908/article/details/61916389
集群搭建 概述作为服务发现的几种产品,比较可以查看这里。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:退出当前服务集群