前言
Consul 是一个服务发现和配置工具
它有如下功能和特性:
- 服务发现
- 健康检查
- 健值存储
- 分布式且多数据中心
Consul 的作用类似于 Zookeeper 或 etcd ,和 etcd 一样也是使用 Go 实现的,也是使用的 Raft 算法
Consul 的架构
Docker Swarm 中使用 Consul 来进行服务发现,这里简单分享一下 Consul 集群相关的基础操作,详细内容可以参考 官方文档
Tip: 当前的最新版本为 Consul 0.6.4
概要
发现机制
当一个Consul代理启动后,它并不知道其它节点的存在,它是一个孤立的 单节点集群,如果想感知到其它节点的存在,它必须加入到一个现存的集群,要加入到一个现存的集群,它只用加入集群中任意一个现存的成员,当加入一个现存的成员后,会通过成员间的通讯很快发现集群中的其它成员,一个Consul代理可以加入任意一个代理,而不仅仅是服务节点
构建集群
启动首个节点
代码语言:javascript复制[root@h104 ~]# consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=a1 -bind=192.168.100.104 -config-dir /etc/consul.d
==> WARNING: BootstrapExpect Mode is specified as 1; this is the same as Bootstrap mode.
==> WARNING: Bootstrap mode enabled! Do not enable unless necessary
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
Node name: 'a1'
Datacenter: 'dc1'
Server: true (bootstrap: true)
Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
Cluster Addr: 192.168.100.104 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
Atlas: <disabled>
==> Log data will now stream in as it occurs:
2016/03/18 21:22:31 [INFO] raft: Node at 192.168.100.104:8300 [Follower] entering Follower state
2016/03/18 21:22:31 [INFO] serf: EventMemberJoin: a1 192.168.100.104
2016/03/18 21:22:31 [INFO] serf: EventMemberJoin: a1.dc1 192.168.100.104
2016/03/18 21:22:31 [INFO] consul: adding WAN server a1.dc1 (Addr: 192.168.100.104:8300) (DC: dc1)
2016/03/18 21:22:31 [INFO] consul: adding LAN server a1 (Addr: 192.168.100.104:8300) (DC: dc1)
2016/03/18 21:22:31 [ERR] agent: failed to sync remote state: No cluster leader
2016/03/18 21:22:32 [WARN] raft: Heartbeat timeout reached, starting election
2016/03/18 21:22:32 [INFO] raft: Node at 192.168.100.104:8300 [Candidate] entering Candidate state
2016/03/18 21:22:32 [INFO] raft: Election won. Tally: 1
2016/03/18 21:22:32 [INFO] raft: Node at 192.168.100.104:8300 [Leader] entering Leader state
2016/03/18 21:22:32 [INFO] consul: cluster leadership acquired
2016/03/18 21:22:32 [INFO] consul: New leader elected: a1
2016/03/18 21:22:32 [INFO] raft: Disabling EnableSingleNode (bootstrap)
2016/03/18 21:22:32 [INFO] consul: member 'a1' joined, marking health alive
2016/03/18 21:22:34 [INFO] agent: Synced service 'consul'
2016/03/18 21:22:34 [INFO] agent: Synced service 'web'
...
...
...
ARG | Comment |
---|---|
-server | 以服务模式运行 |
-bootstrap-expect | 指定期望加入的节点数 |
-data-dir | 指定数据存放的位置 |
-node | 指定节点名 |
-bind | 指定绑定的IP |
-config-dir | 指定配置目录 |