当前位置: 首页 > 文档资料 > Consul 快速入门 >

Consul 集群安装部署

优质
小牛编辑
126浏览
2023-12-01

1、安装

1.1、下载

以linux下安装为例,首先下载安装包,下载地址:https://www.consul.io/downloads.html

下载后上传到 linux 服务器,或者直接在 linux 上下载,版本可自行替换

wget https://releases.hashicorp.com/consul/1.7.0/consul_1.7.0_linux_amd64.zip

1.2、解压缩

unzip consul_1.7.0_linux_amd64.zip -d /usr/local/bin

1.3、设置环境变量

笔者已经将/usr/local/bin目录设置到环境变量中,如果你还没有设置环境变量,需要设置下

$ vi /etc/profile
export CONSUL_HOME=/usr/local/bin/consul
export PATH=$PATH:CONSUL_HOME

使用环境变量配置生效

source /etc/profile

1.4、验证

查看consul版本,验证是否安装成功

[root@localhost ~]# consul version
Consul v1.7.0
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

2、运行

2.1、agent 启动参数说明

consul 以 agent 的方式启动,相关参数说明:

  • -server 以服务端模式运行,相应的还有客户端模式-client
  • -ui 启动内置的Web UI
  • -bootstrap-expect=3 集群最少节点数,类似zookeeper的配置一样,为了防止脑裂,一般为奇数
  • -data-dir=/data/consul 数据存储目录
  • -node=agent-1 集群的节点名称
  • -client=0.0.0.0 指定客户端能访问的ip,类似于ip白名单。0.0.0.0表示不限制
  • -bind=10.25.84.163 绑定了当前主机的IP地址
  • -datacenter=dc1 数据中心名称
  • -join agent启动时加入到代理服务器地址,不指定不会加入任何节点,可以在启动后通过consul join命令加入

更多参数说明可以通过–help查看

consul agent --help

或者参考官网配置说明:https://www.consul.io/docs/agent/options.html

2.2、启动agent

接下来搭建consul集群,首先准备三台服务器:

  • 10.25.84.163
  • 10.25.84.164
  • 10.25.84.165

分别在三台服务器输入以下对应的命令:

// 启动10.25.84.163
consul agent -server -ui -bootstrap-expect=3 -data-dir=/data/consul -node=server-1 -client=0.0.0.0 -bind=10.25.84.163 -datacenter=dc1

// 启动10.25.84.164,并加入10.25.84.163节点
consul agent -server -ui -bootstrap-expect=3 -data-dir=/data/consul -node=server-2 -client=0.0.0.0 -bind=10.25.84.164 -datacenter=dc1 -join 10.25.84.163

// 启动10.25.84.165,并加入10.25.84.163节点
consul agent -server -ui -bootstrap-expect=3 -data-dir=/data/consul -node=server-3 -client=0.0.0.0 -bind=10.25.84.165 -datacenter=dc1 -join 10.25.84.163

启动日志:

[root@localhost ~]# consul agent -server -ui -bootstrap-expect=3 -data-dir=/data/consul -node=server-1 -client=0.0.0.0 -bind=10.25.84.163 -datacenter=dc1
bootstrap_expect > 0: expecting 3 servers
==> Starting Consul agent...
           Version: 'v1.7.0'
           Node ID: '4a60c9bd-472b-01a3-57f4-c74b8ba4d3df'
         Node name: 'server-1'
        Datacenter: 'dc1' (Segment: '<all>')
            Server: true (Bootstrap: false)
       Client Addr: [0.0.0.0] (HTTP: 8500, HTTPS: -1, gRPC: -1, DNS: 8600)
      Cluster Addr: 10.25.84.163 (LAN: 8301, WAN: 8302)
           Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false

==> Log data will now stream in as it occurs:

    2020-02-21T11:28:20.323+0800 [INFO]  agent.server.raft: initial configuration: index=0 servers=[]
    2020-02-21T11:28:20.324+0800 [INFO]  agent.server.raft: entering follower state: follower="Node at 10.25.84.163:8300 [Follower]" leader=
    2020-02-21T11:28:20.325+0800 [INFO]  agent.server.serf.wan: serf: EventMemberJoin: server-1.dc1 10.25.84.163
    2020-02-21T11:28:20.326+0800 [INFO]  agent.server.serf.lan: serf: EventMemberJoin: server-1 10.25.84.163
    2020-02-21T11:28:20.326+0800 [INFO]  agent.server: Adding LAN server: server="server-1 (Addr: tcp/10.25.84.163:8300) (DC: dc1)"
    2020-02-21T11:28:20.326+0800 [INFO]  agent.server: Handled event for server in area: event=member-join server=server-1.dc1 area=wan
    2020-02-21T11:28:20.327+0800 [INFO]  agent: Started DNS server: address=0.0.0.0:8600 network=tcp
    2020-02-21T11:28:20.327+0800 [INFO]  agent: Started DNS server: address=0.0.0.0:8600 network=udp
    2020-02-21T11:28:20.328+0800 [INFO]  agent: Started HTTP server: address=[::]:8500 network=tcp
    2020-02-21T11:28:20.328+0800 [INFO]  agent: started state syncer
==> Consul agent running!
    2020-02-21T11:28:26.317+0800 [WARN]  agent.server.raft: no known peers, aborting election
    2020-02-21T11:28:27.447+0800 [ERROR] agent.anti_entropy: failed to sync remote state: error="No cluster leader"
    2020-02-21T11:28:34.785+0800 [ERROR] agent.http: Request error: method=GET url=/v1/internal/ui/services?dc=dc1&index=10249 from=10.113.116.192:3000 error="No cluster leader"
    2020-02-21T11:28:34.965+0800 [ERROR] agent.http: Request error: method=GET url=/v1/internal/ui/nodes?dc=dc1&index=10249 from=10.113.116.192:3001 error="No cluster leader"
    2020-02-21T11:28:45.387+0800 [ERROR] agent: Coordinate update error: error="No cluster leader"
    2020-02-21T11:28:59.448+0800 [ERROR] agent.anti_entropy: failed to sync remote state: error="No cluster leader"
    2020-02-21T11:29:02.697+0800 [INFO]  agent.server.serf.lan: serf: EventMemberJoin: server-2 10.25.84.164
    2020-02-21T11:29:02.697+0800 [INFO]  agent.server: Adding LAN server: server="server-2 (Addr: tcp/10.25.84.164:8300) (DC: dc1)"
    2020-02-21T11:29:02.698+0800 [INFO]  agent.server.serf.wan: serf: EventMemberJoin: server-2.dc1 10.25.84.164
    2020-02-21T11:29:02.698+0800 [INFO]  agent.server: Handled event for server in area: event=member-join server=server-2.dc1 area=wan
    2020-02-21T11:29:19.312+0800 [ERROR] agent: Coordinate update error: error="No cluster leader"
    2020-02-21T11:29:21.261+0800 [INFO]  agent.server.serf.lan: serf: EventMemberJoin: server-3 10.25.84.165
    2020-02-21T11:29:21.261+0800 [INFO]  agent.server: Adding LAN server: server="server-3 (Addr: tcp/10.25.84.165:8300) (DC: dc1)"
    2020-02-21T11:29:21.269+0800 [INFO]  agent.server: Found expected number of peers, attempting bootstrap: peers=10.25.84.163:8300,10.25.84.164:8300,10.25.84.165:8300
    2020-02-21T11:29:21.281+0800 [INFO]  agent.server.serf.wan: serf: EventMemberJoin: server-3.dc1 10.25.84.165
    2020-02-21T11:29:21.281+0800 [INFO]  agent.server: Handled event for server in area: event=member-join server=server-3.dc1 area=wan
    2020-02-21T11:29:24.003+0800 [ERROR] agent.anti_entropy: failed to sync remote state: error="No cluster leader"
    2020-02-21T11:29:27.329+0800 [WARN]  agent.server.raft: heartbeat timeout reached, starting election: last-leader=
    2020-02-21T11:29:27.329+0800 [INFO]  agent.server.raft: entering candidate state: node="Node at 10.25.84.163:8300 [Candidate]" term=2
    2020-02-21T11:29:27.351+0800 [INFO]  agent.server.raft: election won: tally=2
    2020-02-21T11:29:27.351+0800 [INFO]  agent.server.raft: entering leader state: leader="Node at 10.25.84.163:8300 [Leader]"
    2020-02-21T11:29:27.351+0800 [INFO]  agent.server.raft: added peer, starting replication: peer=b3d84299-a458-19bf-2b98-ca9031e6aea4
    2020-02-21T11:29:27.351+0800 [INFO]  agent.server.raft: added peer, starting replication: peer=31174405-571f-f598-ef74-0a9aba59a6a8
    2020-02-21T11:29:27.351+0800 [INFO]  agent.server: cluster leadership acquired
    2020-02-21T11:29:27.351+0800 [INFO]  agent.server: New leader elected: payload=server-1
    2020-02-21T11:29:27.352+0800 [INFO]  agent.server.raft: pipelining replication: peer="{Voter 31174405-571f-f598-ef74-0a9aba59a6a8 10.25.84.165:8300}"
    2020-02-21T11:29:27.352+0800 [WARN]  agent.server.raft: appendEntries rejected, sending older logs: peer="{Voter b3d84299-a458-19bf-2b98-ca9031e6aea4 10.25.84.164:8300}" next=1
    2020-02-21T11:29:27.356+0800 [INFO]  agent.server.raft: pipelining replication: peer="{Voter b3d84299-a458-19bf-2b98-ca9031e6aea4 10.25.84.164:8300}"
    2020-02-21T11:29:27.368+0800 [INFO]  agent.leader: started routine: routine="CA root pruning"
    2020-02-21T11:29:27.368+0800 [INFO]  agent.server: member joined, marking health alive: member=server-1
    2020-02-21T11:29:27.381+0800 [INFO]  agent.server: member joined, marking health alive: member=server-2
    2020-02-21T11:29:27.396+0800 [INFO]  agent.server: member joined, marking health alive: member=server-3
    2020-02-21T11:29:28.609+0800 [INFO]  agent: Synced node info

查看集群启动结果:

[root@localhost ~]# consul members
Node      Address            Status  Type    Build  Protocol  DC   Segment
server-1  10.25.84.163:8301  alive   server  1.7.0  2         dc1  <all>
server-2  10.25.84.164:8301  alive   server  1.7.0  2         dc1  <all>
server-3  10.25.84.165:8301  alive   server  1.7.0  2         dc1  <all>

命令输出显示了集群节点名称、IP端口、健康状态、启动模式、所在数据中心和版本信息。

2.3、访问 Consul 的 WebUI 控制台

三台服务都起来后,我们可以访问Consul的WebUI控制台,WEB控制台默认端口为:8500,在浏览器输入地址: 10.25.84.163:8500

切换nodes标签页:

3、查看数据中心

到目前为止,我们已经搭建好数据中心了,现在我们可以查看下数据中心中的成员

3.1、通过 members 命令查看

members命令针对Consul客户端运行,该客户端通过gossip协议获取其信息。

[root@localhost ~]# consul members
Node      Address            Status  Type    Build  Protocol  DC   Segment
server-1  10.25.84.163:8301  alive   server  1.7.0  2         dc1  <all>
server-2  10.25.84.164:8301  alive   server  1.7.0  2         dc1  <all>
server-3  10.25.84.165:8301  alive   server  1.7.0  2         dc1  <all>

命令输出显示了数据中心的节点、IP端口、健康状态、角色、所在数据中心和版本信息。

还可以用过 -detailed 参数获取更多信息

[root@localhost ~]# consul members -detailed
Node      Address            Status  Tags
server-1  10.25.84.163:8301  alive   acls=0,build=1.7.0:95fb95bf,dc=dc1,expect=3,id=4a60c9bd-472b-01a3-57f4-c74b8ba4d3df,port=8300,raft_vsn=3,role=consul,segment=<all>,vsn=2,vsn_max=3,vsn_min=2,wan_join_port=8302
server-2  10.25.84.164:8301  alive   acls=0,build=1.7.0:95fb95bf,dc=dc1,expect=3,id=b3d84299-a458-19bf-2b98-ca9031e6aea4,port=8300,raft_vsn=3,role=consul,segment=<all>,vsn=2,vsn_max=3,vsn_min=2,wan_join_port=8302
server-3  10.25.84.165:8301  alive   acls=0,build=1.7.0:95fb95bf,dc=dc1,expect=3,id=31174405-571f-f598-ef74-0a9aba59a6a8,port=8300,raft_vsn=3,role=consul,segment=<all>,vsn=2,vsn_max=3,vsn_min=2,wan_join_port=8302

3.2、通过 HTTP API 查看

curl 10.25.84.163:8500/v1/catalog/nodes

返回数据:

[
    {
        "ID":"99f8e10c-edda-bb40-21b4-8719ba851308",
        "Node":"agent-1",
        "Address":"10.25.84.163",
        "Datacenter":"dc1",
        "TaggedAddresses":{
            "lan":"10.25.84.163",
            "lan_ipv4":"10.25.84.163",
            "wan":"10.25.84.163",
            "wan_ipv4":"10.25.84.163"
        },
        "Meta":{
            "consul-network-segment":""
        },
        "CreateIndex":5,
        "ModifyIndex":11
    },
    {
        "ID":"824a86e6-9713-a136-8037-c489222a88e1",
        "Node":"agent-2",
        "Address":"10.25.84.164",
        "Datacenter":"dc1",
        "TaggedAddresses":{
            "lan":"10.25.84.164",
            "lan_ipv4":"10.25.84.164",
            "wan":"10.25.84.164",
            "wan_ipv4":"10.25.84.164"
        },
        "Meta":{
            "consul-network-segment":""
        },
        "CreateIndex":7,
        "ModifyIndex":10
    },
    {
        "ID":"5f073f8b-2130-254d-c83f-30d7850e2192",
        "Node":"agent-3",
        "Address":"10.25.84.165",
        "Datacenter":"dc1",
        "TaggedAddresses":{
            "lan":"10.25.84.165",
            "lan_ipv4":"10.25.84.165",
            "wan":"10.25.84.165",
            "wan_ipv4":"10.25.84.165"
        },
        "Meta":{
            "consul-network-segment":""
        },
        "CreateIndex":8,
        "ModifyIndex":9
    }
]

3.3、通过 DNS 接口查看

除了HTTP API外,我们还可以使用DNS接口查看数据中心成员信息

[root@localhost ~]# dig @10.25.84.163 -p 8600 server-1.node.consul

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> @10.25.84.163 -p 8600 server-1.node.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64004
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;server-1.node.consul.		IN	A

;; ANSWER SECTION:
server-1.node.consul.	0	IN	A	10.25.84.163

;; ADDITIONAL SECTION:
server-1.node.consul.	0	IN	TXT	"consul-network-segment="

;; Query time: 1 msec
;; SERVER: 10.25.84.163#8600(10.25.84.163)
;; WHEN: Fri Feb 21 12:47:15 CST 2020
;; MSG SIZE  rcvd: 101

其中server-1.node.consul中第一部分需要替换成自己的agent节点名

dig命令如果不存在需要安装下,命令:

yum -y install bind-utils

4、停止 agent 服务

通过 consul leave 命令优雅停止服务,我们再打开一个从节点机器终端,运行该命令

[root@localhost ~]# consul leave
Graceful leave complete