微服务Consul系列之集群搭建

时间:2019-11-18
本文章向大家介绍微服务Consul系列之集群搭建,主要包括微服务Consul系列之集群搭建使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在上一篇中讲解了Consul的安装、部署、基本的使用,使得大家有一个基本的了解,本节开始重点Consul集群搭建,官方推荐3~5台Server,因为在异常处理中,如果出现Leader挂了,只要有超过一半的Server还处于活跃状态,consul就会重新选举新的Leader,保证集群可以正常工作。

准备工作

测试用建议本地搭建几台虚拟机用于调试,这里的虚拟机分别为3台Server模式,1台Client模式,共以下4台:

  • 192.168.6.128 Server模式(初始设置为Leader)

  • 192.168.6.129 Server模式

  • 192.168.6.130 Server模式

  • 192.168.6.131 Client模式

下载相应平台版本的Consul解压copy至/usr/local/bin/(系统的环境变量)目录,这里以1.4.0版本为例,具体安装参照上篇-consul下载安装指南

创建 /usr/src/consul目录,存放Consul的启动配置文件consul_config.json

{
    "datacenter": "consul_cluster",
    "node_name": "consul_1",
    "server": true,
    "bootstrap_expect": 3,
    "data_dir": "/usr/src/consul/data",
    "log_level": "DEBUG",
    "enable_syslog": true,
    "enable_script_checks": true,
    "bind_addr": "192.168.6.128",
    "client_addr": "192.168.6.128",
}
  • node_name:节点名称,等同于-node
  • data_dir:数据存放目录
  • enable_syslog:consul日志写入系统的syslog目录是否启用
  • enable_script_checks:是否启用监控检测脚本
  • bind_addr:等同于-bind
  • client_addr:等同于-client

Server端部署

  • 部署第一台192.168.6.128

注意:在第一台启动的时候加上-ui,只初始化一次,在其它2个节点进行相同操作,但是配置文件中的node_namebind_addrclient_addr要进行更改,每台机器保持唯一。

$ sudo consul agent -ui -config-file=/usr/src/consul/consul_config.json

-config-file:加载启动的配置文件

  • 部署第二台192.168.6.129

修改/usr/src/consul/consul_config.json

{
    "datacenter": "consul_cluster",
    "node_name": "consul_2",
    "server": true,
    "bootstrap_expect": 3,
    "data_dir": "/usr/src/consul/data",
    "log_level": "DEBUG",
    "enable_syslog": true,
    "enable_script_checks": true,
    "bind_addr": "192.168.6.129",
    "client_addr": "192.168.6.129",
}

执行命令启动命令

$ sudo consul agent -config-file=/usr/src/consul/consul_config.json
  • 部署第三台192.168.6.130

修改/usr/src/consul/consul_config.json

{
    "datacenter": "consul_cluster",
    "node_name": "consul_3",
    "server": true,
    "bootstrap_expect": 3,
    "data_dir": "/usr/src/consul/data",
    "log_level": "DEBUG",
    "enable_syslog": true,
    "enable_script_checks": true,
    "bind_addr": "192.168.6.130",
    "client_addr": "192.168.6.130"
}

执行命令启动命令

$ sudo consul agent -config-file=/usr/src/consul/consul_config.json

截止目前服务端已经全部启动,但是还没有加入集群,因此还只是单节点的集群,可以在某台机器上查看成员情况:

注意:直接使用consul members会报错,需要绑定ip地址

$ consul members --http-addr 192.168.6.128:8500
Node      Address             Status  Type    Build  Protocol  DC              Segment
consul_1  192.168.6.128:8301  alive   server  1.4.0  2         consul_cluster  <all>

Server端集群建立

每个Consul Agent之后,都是相对独立的并不知道其它节点的存在,现在我们要做的是加入集群,将上面创建的consul_2、consul_3加入到同一个集群consul_1中。

  • 第二台192.168.6.129加入到consul_1中
$ consul join --http-addr 192.168.6.129:8500 192.168.6.128
Successfully joined cluster by contacting 1 nodes. # 成功返回的消息
  • 第三台192.168.6.130加入到consul_1中
$ consul join --http-addr 192.168.6.130:8500 192.168.6.128

目前服务端的集群已经创建完毕,可以看下我们目前的集群成员情况:

consul members --http-addr 192.168.6.128:8500
Node      Address             Status  Type    Build  Protocol  DC              Segment
consul_1  192.168.6.128:8301  alive   server  1.4.0  2         consul_cluster  <all>
consul_2  192.168.6.129:8301  alive   server  1.4.0  2         consul_cluster  <all>
consul_3  192.168.6.130:8301  alive   server  1.4.0  2         consul_cluster  <all>
  • 通过HTTP API的方式查看集群leader
$ curl 192.168.6.128:8500/v1/status/leader

"192.168.6.128:8300"
  • 通过HTTP API的方式查看集群成员
$ curl 192.168.6.128:8500/v1/status/peers 

["192.168.6.129:8300","192.168.6.130:8300","192.168.6.128:8300"]

Client端部署

现在开始客户端的部署,方式同服务端有不同

修改/usr/src/consul/consul_config.json

{
    "datacenter": "consul_cluster",
    "node_name": "consul_4",
    //"server": true, 不指定为服务端,默认走客户端
    // "bootstrap_expect": 3, 只在server模式有效
    "data_dir": "/usr/src/consul/data",
    "log_level": "DEBUG",
    "enable_syslog": true,
    "enable_script_checks": true,
    "bind_addr": "192.168.6.131",
    "client_addr": "192.168.6.131"
}

执行启动命令:

通过-join参数也可以加入一个已经启动的集群

$ sudo consul agent -config-file=/usr/src/consul/consul_config.json -join=192.168.6.128

在查看当前集群成员,可以看到为3个Server模式和1个Client模式

$ consul members --http-addr 192.168.6.128:8500
Node      Address             Status  Type    Build  Protocol  DC              Segment
consul_1  192.168.6.128:8301  alive   server  1.4.0  2         consul_cluster  <all>
consul_2  192.168.6.129:8301  alive   server  1.4.0  2         consul_cluster  <all>
consul_3  192.168.6.130:8301  alive   server  1.4.0  2         consul_cluster  <all>
consul_4  192.168.6.131:8301  alive   client  1.4.0  2         consul_cluster  <default>

管理工具中查看

在部署第一台192.168.6.128机器的时候,consul agent之后有跟一个-ui参数,这个是用于启动WebUI界面,这个是Consul本身所提供的Web可视化界面,浏览器输入http://192.168.6.128:8500进行访问

 

原文地址:https://www.cnblogs.com/ExMan/p/11882408.html