我这里还是用了现有的4台centos的vmware的虚拟机做的测试。
Node Type
server1 server
server2 server
server2 server
agent1 agent
Consul 在CentOS上的安装
安装非常简单,安装完成后,就可以run Consul命令啦。
cd /usr/local/bin wget https://dl.bintray.com/mitchellh/consul/0.5.0_linux_amd64.zip unzip *.zip rm *.zip
consul命令行运行的方式非常简单,可以看到以下的参数,也可以参考consul的用户文档。
$ consul
usage: consul [--version] [--help] <command> [<args>]
Available commands are:
agent Runs a Consul agent
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
leave Gracefully leaves the Consul cluster and shuts down
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
reload Triggers the agent to reload configuration files
version Prints the Consul version
开启Bootstrap Server
一开始学习consul, 我们需要让consul 运行起来。consul server推荐至少在3~5个之间,推荐的方法是一开始启动其中一台server,并且配置到bootstrap的模式,该模式node可以指定自己作为leader,而不用进行选举。然后再依次启动其他server,配置为非bootstrap的模式。最后把第一个serverbootstrap模式停止,重新以非bootstrap模式启动,这样server之间就可以自动选举leader。
在上面的表中,我们需要指定server 1 做为bootstrap server,可以run下面的命令,以下是启动的过程。可以看到bootstrap模式下配置自己为leader。
[admin@localhost ~]$ consul agent -server -bootstrap -data-dir /tmp/consul
==> WARNING: Bootstrap mode enabled! Do not enable unless necessary
==> WARNING: It is highly recommended to set GOMAXPROCS higher than 1
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
Node name: 'localhost.localdomain'
Datacenter: 'dc1'
Server: true (bootstrap: true)
Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
Cluster Addr: 10.74.15.87 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
Atlas: <disabled>
==> Log data will now stream in as it occurs:
2015/05/11 02:58:06 [INFO] raft: Node at 10.74.15.87:8300 [Follower] entering Follower state
2015/05/11 02:58:06 [INFO] serf: EventMemberJoin: localhost.localdomain 10.74.15.87
2015/05/11 02:58:06 [INFO] serf: EventMemberJoin: localhost.localdomain.dc1 10.74.15.87
2015/05/11 02:58:06 [INFO] consul: adding server localhost.localdomain (Addr: 10.74.15.87:8300) (DC: dc1)
2015/05/11 02:58:06 [INFO] consul: adding server localhost.localdomain.dc1 (Addr: 10.74.15.87:8300) (DC: dc1)
2015/05/11 02:58:06 [ERR] agent: failed to sync remote state: No cluster leader
2015/05/11 02:58:08 [WARN] raft: Heartbeat timeout reached, starting election
2015/05/11 02:58:08 [INFO] raft: Node at 10.74.15.87:8300 [Candidate] entering Candidate state
2015/05/11 02:58:08 [INFO] raft: Election won. Tally: 1
2015/05/11 02:58:08 [INFO] raft: Node at 10.74.15.87:8300 [Leader] entering Leader state
2015/05/11 02:58:08 [INFO] consul: cluster leadership acquired
2015/05/11 02:58:08 [INFO] consul: New leader elected: localhost.localdomain
2015/05/11 02:58:08 [INFO] raft: Disabling EnableSingleNode (bootstrap)
2015/05/11 02:58:08 [INFO] consul: member 'localhost.localdomain' joined, marking health alive
2015/05/11 02:58:08 [INFO] agent: Synced service 'consul'
启动其他servers,在 server2 and server3, 我们用以下命令启动consul,不需要带bootstrap 选项。
[admin@localhost ~]$ consul agent -server -data-dir /tmp/consul
==> WARNING: It is highly recommended to set GOMAXPROCS higher than 1
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
Node name: 'localhost.localdomain'
Datacenter: 'dc1'
Server: true (bootstrap: false)
Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
Cluster Addr: 10.74.15.87 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
Atlas: <disabled>
==> Log data will now stream in as it occurs:
2015/05/11 03:01:54 [INFO] serf: EventMemberJoin: localhost.localdomain 10.74.15.87
2015/05/11 03:01:54 [INFO] serf: EventMemberJoin: localhost.localdomain.dc1 10.74.15.87
2015/05/11 03:01:54 [INFO] raft: Node at 10.74.15.87:8300 [Follower] entering Follower state
2015/05/11 03:01:54 [INFO] consul: adding server localhost.localdomain (Addr: 10.74.15.87:8300) (DC: dc1)
2015/05/11 03:01:54 [INFO] consul: adding server localhost.localdomain.dc1 (Addr: 10.74.15.87:8300) (DC: dc1)
2015/05/11 03:01:54 [ERR] agent: failed to sync remote state: No cluster leader
2015/05/11 03:01:55 [WARN] raft: EnableSingleNode disabled, and no known peers. Aborting election.
可以从log中看到,无法找到cluster leader和无法配置自己为leader,server2还没有和server1通信。这时,需要run下面的命令加入到server1的集群。
consul join 192.0.2.2 192.0.2.3
consul members //查看现有的cluster下面的node
当3台机子都加入到集群,我们需要配置这3台机子为同等的server,并且让它们自己选择leader,这时可以停止第一台的consul,然后再用以下命令启动:
consul agent -server -data-dir /tmp/consul
consul join 192.0.2.2 192.0.2.3
现在,3太机子会互相复制信息,或者可以处理某台机子挂掉后的情况。当有其他server需要加入时,重复上面的操作就可以。
本文暂时没有评论,来添加一个吧(●'◡'●)