welcome here!
当你觉得不行的时候,就去路上走一走,这样你就是一个行人了

Consul 集群部署Ⅱ

Consul注册中心集群部署

IP地址规划

IP 部署节点规划 主机名称 资源名称
10.252.68.154 devops-consul-server-1 hmsc-test-1-mq 测试-消息队列服务器
10.252.68.203 devops-consul-server-2 proc-test-1-logcatch 日志监控服务器1
10.252.68.204 devops-consul-server-3 proc-test-2-logcatch 日志监控服务器2

安装部署

源码下载

官网下载链接

这里可以在需要部署的三台服务器全部需要下载最新版本的源码压缩包consul_1.8.0_linux_amd64.zip
这里以10.252.68.154(hmsc-test-1-mq)机器为例。其他2台服务器都一样的操作。

[root@hmsc-test-1-mq ~]# cd /usr/local/src/

[root@hmsc-test-1-mq src]# wget https://releases.hashicorp.com/consul/1.8.0/consul_1.8.0_linux_amd64.zip

[root@hmsc-test-1-mq src]# unzip consul_1.8.0_linux_amd64.zip -d /usr/local/consul 
Archive:  consul_1.8.0_linux_amd64.zip
  inflating: /usr/local/consul/consul

[root@hmsc-test-1-mq src]# cd /usr/local/consul/
[root@hmsc-test-1-mq consul]# ls
consul                         #一个可执行文件

配置环境变量

[root@hmsc-test-1-mq consul]# echo "export PATH=$PATH:/usr/local/consul" >> /etc/profile

[root@hmsc-test-1-mq consul]# . /etc/profile

[root@hmsc-test-1-mq consul]# consul -v
Consul v1.8.0
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

上述的操作需要在部署集群的机器中全部执行一次

consul命令使用

[root@hmsc-test-1-mq consul]# consul --help
Usage: consul [--version] [--help] <command> [<args>]

Available commands are:
    acl            Interact with Consul's ACLs
    agent          Runs a Consul agent
    catalog        Interact with the catalog
    config         Interact with Consul's Centralized Configurations
    connect        Interact with Consul Connect
    debug          Records a debugging archive for operators
    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.
    intention      Interact with Connect service intentions
    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
    login          Login to Consul using an auth method
    logout         Destroy a Consul token created with login
    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
    services       Interact with services
    snapshot       Saves, restores and inspects snapshots of Consul server state
    tls            Builtin helpers for creating CAs and certificates
    validate       Validate config files/directories
    version        Prints the Consul version
    watch          Watch for changes in Consul

启动集群节点

consul agent有两种运行模式:server和client。这里的server和client只是consul集群层面的区分,与搭建在cluster之上的应用服务无关。

以server模式运行的consul agent节点用于维护consul集群的状态,

官方建议每个consul cluster至少有3个或以上的运行在server mode的agent,client节点不限。

我们这里以安装三个节点为例,环境配置如下

10.252.68.154 以server模式运行

10.252.68.203,10.252.68.204 以client模式运行

注意agent后面的参数是 -server 还是 -client

[root@hmsc-test-1-mq ~]# consul agent -server  -data-dir=/tmp/consul -node=server-1 -bind=10.252.68.154 -bootstrap-expect 3  -client=0.0.0.0  -ui &
[root@proc-test-1-logcatch ~]# consul agent -server  -data-dir=/tmp/consul -node=server-2 -bind=10.252.68.203 -join=10.252.68.154  -client=0.0.0.0  -ui &
[root@proc-test-2-logcatch ~]# consul agent -server  -data-dir=/tmp/consul -node=server-3 -bind=10.252.68.204 -join=10.252.68.154  -client=0.0.0.0  -ui &

选项详解

[root@hmsc-test-1-mq ~]# consul agent -h              #查看参数选项

consul配置参数大全、详解、总结

选项 说明
consul anent 该命令会启动一个consulAnent
-server 表示该 agent是一个 serverAgent,不添加这个选项则表示是一个 clientAgent
-data-dir 表示相关数据存储的目录位置,在 serverAgent上该命令所指示的目录下会存储一些集群的状态信息
-node 指定该 agent 节点的名称,该名称在集群众必须是唯一的
-bind 指定 agent 的 Ip
-bootstrap-expect 该命令通知 consul 准备加入集群的节点个数
-client 0.0.0.0 -ui 启动 consulUI,如不添加该指令,则 UI 只能在当前机器上访问
join 将该节点加入集群中
-datacenter 数据中心名称,默认dc1,一个数据中心的所有节点都必须在一个LAN中

集群参数get/set测试

# consul kv -h   #语法
Usage: consul kv <subcommand> [options] [args]

Subcommands:
    delete    Removes data from the KV store
    export    Exports a tree from the KV store as JSON
    get       Retrieves or lists data from the KV store
    import    Imports a tree stored as JSON to the KV store
    put       Sets or updates data in the KV store

10.252.68.204(proc-test-2-logcatch)的set操作:

[root@proc-test-2-logcatch ~]# consul kv put TEST tongyongzhongxin_kaivi
Success! Data written to: TEST

10.252.68.203(proc-test-1-logcatch)的get操作:

[root@proc-test-1-logcatch ~]# consul kv get tset        #数据库名大小写敏感
Error! No key exists at: tset
[root@proc-test-1-logcatch ~]# consul kv get TEST
tongyongzhongxin_kaivi

10.252.68.154(hmsc-test-1-mq)的get操作(Leader节点):

[root@hmsc-test-1-mq ~]# consul kv get TEST
tongyongzhongxin_kaivi

同理:在10.252.68.154(hmsc-test-1-mq)的leader节点可以正常设置key的值为value,并能正常查回来。三台服务器获取key的值能正常返回,如此可知key的值已经在集群中同步。这样这个集群就能正常使用。

验证

验证集群是否搭建成功,执行consul members,在任意的一台服务器上:

# consul members
Node      Address             Status  Type    Build  Protocol  DC   Segment
server-1  10.252.68.154:8301  alive   server  1.8.0  2         dc1  <all>
server-2  10.252.68.203:8301  alive   server  1.8.0  2         dc1  <all>
server-3  10.252.68.204:8301  alive   server  1.8.0  2         dc1  <all>

集群状态查看

[root@hmsc-test-1-mq ~]# consul operator raft list-peers
Node      ID                                    Address             State     Voter  RaftProtocol
server-3  76312cd7-9ace-4357-2273-263838c9c310  10.252.68.204:8300  follower  true   3
server-1  f2961194-65e6-6f4c-27d4-4266fdefafa7  10.252.68.154:8300  leader    true   3
server-2  8fda918c-fe2c-688f-43a9-013df2f35335  10.252.68.203:8300  follower  true   3

consul集群:当一个consul agent启动后,它不知道任何其他节点,要学习到集群中的其他节点,agent必须加入一个已经存在的集群(cluster)。要加入这样的集群,它只需要知道这个集群中的一个节点即可。它加入后,将会和这个member gossip(交谈)并迅速发现集群中的其他节点。一个consul agent可以加入任何类型的其他agent,而不只是那些运行于server mode的agent。必须选举出来 leader 之后才能使用集群
参考文章:

通过URL验证

http://yourIP:8500/

在这里插入图片描述
在这里插入图片描述
上图的KV值在三个 服务器对应的ui-web界面都是可以访问得到的。

至上部署基本完成

知识扩展

端口使用

Consul最多需要6个不同的端口才能正常工作,有些使用TCP,UDP或两种协议。下面我们记录每个端口的要求。

服务器RPC(默认8300)。这由服务器用来处理来自其他代理的传入请求。仅限TCP。

Serf LAN(默认8301)。这是用来处理局域网中的八卦。所有代理都需要。TCP和UDP。

Serf WAN(默认8302)。这被服务器用来在WAN上闲聊到其他服务器。TCP和UDP。从Consul 0.8开始,建议通过端口8302在LAN接口上为TCP和UDP启用服务器之间的连接,以及WAN加入泛滥功能。另见: Consul 0.8.0 CHANGELOG和GH-3058

HTTP API(默认8500)。这被客户用来与HTTP API交谈。仅限TCP。

DNS接口(默认8600)。用于解析DNS查询。TCP和UDP。

[root@hmsc-test-1-mq ~]# ps -ef |grep consul     #找到consul服务的PID为122214
root       1166 127663  0 11:21 pts/2    00:00:00 grep --color=auto consul
root     122214      1  1 10:36 ?        00:00:42 consul agent -server -data-dir=/tmp/consul -node=server-1 -bind=10.252.68.154 -bootstrap-expect 3 -client=0.0.0.0 -ui

[root@hmsc-test-1-mq ~]# ss -ntlp |grep 122214  #通过过滤PID来查看对应端口
LISTEN     0      128    10.252.68.154:8300                     *:*                   users:(("consul",pid=122214,fd=6))
LISTEN     0      128    10.252.68.154:8301                     *:*                   users:(("consul",pid=122214,fd=12))
LISTEN     0      128    10.252.68.154:8302                     *:*                   users:(("consul",pid=122214,fd=8))
LISTEN     0      128       [::]:8600                  [::]:*                   users:(("consul",pid=122214,fd=16))
LISTEN     0      128       [::]:8500                  [::]:*                   users:(("consul",pid=122214,fd=14))
[root@hmsc-test-1-mq ~]# 

查询数据中心节点

# curl http://10.252.68.154:8500/v1/catalog/nodes?dc=dc1

[root@proc-test-1-logcatch ~]# curl http://10.252.68.154:8500/v1/catalog/nodes?dc=dc1
[{"ID":"f2961194-65e6-6f4c-27d4-4266fdefafa7","Node":"server-1","Address":"10.252.68.154","Datacenter":"dc1","TaggedAddresses":{"lan":"10.252.68.154","lan_ipv4":"10.252.68.154","wan":"10.252.68.154","wan_ipv4":"10.252.68.154"},"Meta":{"consul-network-segment":""},"CreateIndex":5,"ModifyIndex":10},{"ID":"8fda918c-fe2c-688f-43a9-013df2f35335","Node":"server-2","Address":"10.252.68.203","Datacenter":"dc1","TaggedAddresses":{"lan":"10.252.68.203","lan_ipv4":"10.252.68.203","wan":"10.252.68.203","wan_ipv4":"10.252.68.203"},"Meta":{"consul-network-segment":""},"CreateIndex":7,"ModifyIndex":11},{"ID":"76312cd7-9ace-4357-2273-263838c9c310","Node":"server-3","Address":"10.252.68.204","Datacenter":"dc1","TaggedAddresses":{"lan":"10.252.68.204","lan_ipv4":"10.252.68.204","wan":"10.252.68.204","wan_ipv4":"10.252.68.204"},"Meta":{"consul-network-segment":""},"CreateIndex":8,"ModifyIndex":9}]

可以用json转换在线转化为json格式显示

查询所有K/V

curl -v http://localhost:8500/v1/kv/?recurse

[root@hmsc-test-1-mq ~]# curl http://10.252.68.154:8500/v1/kv/?recurse
[{"LockIndex":0,"Key":"DEV","Flags":0,"Value":"dWMtdG9uZ3lvbmd6aG9ueGlu","CreateIndex":1559,"ModifyIndex":1559},{"LockIndex":0,"Key":"TEST","Flags":0,"Value":"dG9uZ3lvbmd6aG9uZ3hpbl9rYWl2aQ==","CreateIndex":1507,"ModifyIndex":1507}]

可以用json转换在线转化为json格式显示

赞(0) 打赏
开源学习 相互进步www.likai.tech » Consul 集群部署Ⅱ
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

点赞是最好的支持

微信扫一扫打赏