搭建 consul 集群

=============================
consul 命令行工具
=============================
consul 支持 Windows/Linux 等多种平台, 一般生产环境部署在 Linux, 下载包中仅有一个可执行程序, 所有的服务都通过命令行开启.

D:\my_program\consul\consul_1.2.3_windows_amd64>consul
Usage: consul [--version] [--help] <command> [<args>]

Available commands are:
    agent          Runs a Consul agent
    catalog        Interact with the catalog
    connect        Interact with Consul Connect
    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
    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
    snapshot       Saves, restores and inspects snapshots of Consul server state
    validate       Validate config files/directories
    version        Prints the Consul version
    watch          Watch for changes in Consul

常用的命令有:
consul agent #开启 server agent 或 client agent
consul reload #在不重启本机的 agent 的情况下, 让 agent 重新加载配置文件.
consul members #查看集群中的成员节点
consul join #将指定的agent加到指定的集群中
consul leave #将指定的agent从所属的集群中退出, 同时关闭该agent


--------------------------
consul agent 命令
--------------------------
consul agent 参数也有很多, 下面是一些重要参数:

-server 参数, 节点作为 server 角色, 不加该参数的话, 表明节点工作在 client 模式下.

-bootstrap-expect=<int> 参数, 表明该 datacenter 中期望的引导节点个数,当设定该参数后, consul 直到 server 数量到达该设定值后, 才会引导整个集群,该标记不能和 bootstrap 共用, 引导节点不需要太多, 1 到 3 个就可以了.

-bootstrap 参数, 参数"-bootstrap"表示在 datacenter 中只能有一个 server 处于 bootstrap 模式并使自己成为 leader 角色, 该参数不能和 bootstrap-expect 参数公用.

-bind=<IP> 参数, 输入本节点的IP, 该IP用于集群内部的通讯, 集群中所有节点都应能访问该 IP, 默认为 0.0.0.0. 一般不需要修改. 如果有多张网卡的话, consul 将不允许使用 0.0.0.0, 需要我们指定一个真实的网卡 IP.

-node=<节点名称>, 用来设定 agent 在集群中的名称, 默认为该节点的 hostname

-join=<IP>, 将节点加到指定的集群, IP地址为集群中其他agent的IP, 可以传入多个 -join 参数. 默认agent启动时不会加入任何节点, 后续需要使用 consul join 命令加入到集群中.

-config-dir=<目录>, 设定配置文件目录, 其下所有的.json 文件都会被自动加载.

-data-dir=<目录>, 设定 data 存放路径, client和server类的agent都需要指定.

-client=<IP>, 参数后面接一个 ip 地址, 代表将来开放给客户端访问的地址, 用来提供 Http/DNS/RPC 服务, 默认是 127.0.0.1, 只运行本地访问, 如果要开放访问, 需要设定为 0.0.0.0

-ui 参数, 开启默认的 Web UI

-advertise-wan=<IP>, 用于多数据中心集群, 边缘节点server agent需要设置一个公网IP, 这样多个数据中心集群可以相互通讯.

-dev 参数, 代表运行在开发模式下, 在该模式下, consul不做任何数据持久化, 非常适合做 demo.


consul除了支持命令行参数的方式外, 还支持json格式的参数, consul json 配置文件的详细说明, 可参考:
https://deepzz.com/post/the-consul-of-discovery-and-configure-services.html#toc_9 和 https://www.tuicool.com/articles/EzE7NfY


两个示例:
示例1: 开启一个server agent 的示例, 192.168.1.1 为一个Windows 机器.
consul agent -server -bootstrap-expect=1 -node=s1 -bind=192.168.1.1 -client=0.0.0.0 -data-dir=D:\consul_data -ui
说明: -bind 参数一般情况下取值为 0.0.0.0, 如果有多张网卡的话, consul 将不允许使用 0.0.0.0, 需要我们指定一个真实的网卡 IP.

示例2: 在第一个 server 启动后, 其他 agent 启动的时候, 可以使用 -join 参数加入到集群中.
比如下面是在 192.168.1.200 Windows机器上, 启动一个客户端 agent, 并加入到上述 server 集群中.
consul agent -node=c1 -bind=192.168.1.200 -join 192.168.1.1 -data-dir=D:\consul_data -ui

--------------------------
consul leave 和 join 命令
--------------------------
一个 consul agent 可以加到一个或多个consul集群中, 如果一个agent 没有加入到任何一个集群中, 它将是一个孤立的节点, 也就是说它是一个孤立的集群. 想加入一个存在的集群, 只需要知道一个存在的节点, 这个存在的节点既可以是server类agent, 也可以是client类 agent.

consul leave -http-addr=192.168.1.11:8500
# 上面命令是将 192.168.1.11 agent 优雅地关闭, 也就是 192.168.1.11 agent将退出参与的所有集群,
# 这个操作不需要在 192.168.1.11 本机执行. 


consul join -http-addr=192.168.1.11:8500 192.168.1.12,
# 上面命令将 192.168.1.11 agent 加到 192.168.1.12 所处的集群中,这个操作不需要在 192.168.1.11 本机执行.


--------------------------
consul catalog 命令
--------------------------
consul catalog datacenters, 列出数据中心
consul catalog nodes , 列出节点
consul catalog nodes -service=redis ,列出指定服务的节点
consul catalog services , 列出服务

=============================
Windows 下的开发环境
=============================
Windows 下开发环境的命令如下, 非常简单.

consul agent -dev -client 0.0.0.0

-dev 参数, 代表运行在开发模式下, 在该模式下, consul不做任何数据持久化.
-client 参数后面接一个 ip 地址, 代表将来开放给客户端访问的地址, 用来提供 Http/DNS/RPC 服务, 默认是 127.0.0.1, 只运行本地访问, 如果要开放访问, 需要设定为 0.0.0.0

命令执行后, consul 将监听多个端口, 其中 8500 端口是 webUI 服务, 可以通过 http://127.0.0.1:8500 验证 consul 运行的的情况.

=============================
Linux 下的开发环境
=============================
在 Linux 下, 我使用 docker 容器运行 consul, 下载次新版 1.2.3

# pull image
docker pull consul:1.2.3 

# 验证一下版本信息
docker run --rm consul:1.2.3 consul version

# 开启开发模式
docker run -d      \
 --name consul_dev \
 --net=host        \
 -p 8500:8500      \
 -p 8600:8600      \
 -p 8301:8301      \
 -p 8302:8302      \
 consul:1.2.3      \
 consul agent -dev -client 0.0.0.0
 

因为是通过容器来运行, 所以需要将端口都暴露出来, 容器网络模式选用 host 模式, 并开放其他机器访问该 consul 服务. 


=============================
consul 集群
=============================
consul 集群中至少一个 server 角色的 agent, 但显然这个集群本身并不是高可用架构. 正常集群需要 3~5 个 server, 我分别在三台虚拟机通过 docker 容器开启 server agent. Windows 主机工作在 client agent 模式下.

下面是集群的规划清单:
Linux | vm1 | server 模式 | 192.168.1.11
Linux | vm2 | server 模式 | 192.168.1.12
Linux | vm3 | server 模式 | 192.168.1.13

--------------------------
vm1 server agent 启动
--------------------------
在 vm1 上 启动一个server, 特别说明的为容器分配一个匿名的 volume 用来映射容器中的 /consul/data 目录, 该目录保存 consul 的 data, 脚本文件如下:

#file: /var/lib/boot2docker/consul_vm1.sh
docker rm consul_vm1 
docker run -d      \
 --name consul_vm1 \
 --net host        \
 -p 8500:8500      \
 -p 8600:8600      \
 -p 8301:8301      \
 -p 8302:8302      \
 -v /consul/data   \
 consul:1.2.3      \
 consul agent      \
         -server                   \
         -ui                       \
         -datacenter=dc1           \
         -bootstrap-expect=1       \
         -data-dir=/consul/data    \
         -node=consul_vm1          \
         -bind=192.168.1.11        \
         -client=0.0.0.0           


通过 ui 检查
http://192.168.1.11:8500/

# 通过 consul members 命令检查:
docker run --rm consul:1.2.3 consul members -http-addr=192.168.1.11:8500   

docker run --rm consul:1.2.3 consul members
直接执行不带参数的consul members 会报错, 因为默认情况下, consul 会通过 127.0.0.1:8500 获取成员清单, 而我们用的是 eth1 这个网卡IP 192.168.1.11启动的consul, 所以不能用 loopback lo 网卡的 127.0.0.1 IP 查询, 上面命令会报错"tcp 127.0.0.1:8500: connect: connection refused", 需要使用下面命令查询:


--------------------------
vm2 server agent 启动
--------------------------
在 vm2 启动 consul 容器的命令基本和 vm1 相同, 不同的地方有:
* 增加了 -join 参数, 加入的 vm1 的集群中.
* 删除了 -bootstrap-expect=1
* 修改了 对应了 -bind 参数IP
* 修改了容器名 和 consul node 名

#file: /var/lib/boot2docker/consul_vm2.sh
docker rm consul_vm2 
docker run -d      \
 --name consul_vm2 \
 --net host        \
 -p 8500:8500      \
 -p 8600:8600      \
 -p 8301:8301      \
 -p 8302:8302      \
 -v /consul/data   \
 consul:1.2.3      \
 consul agent      \
         -server                   \
         -ui                       \
         -datacenter=dc1           \ 
         -data-dir=/consul/data    \
         -node=consul_vm2          \
         -bind=192.168.1.12        \
         -client=0.0.0.0           \
         -join=192.168.1.11

--------------------------
vm3 server agent 启动
--------------------------
在 vm3 启动 consul 容器的命令基本和 vm2 相同, 除了IP和名称不同外, 仅仅是删除了 -join 参数, 我们将在后面通过join命令加入集群.

#file: /var/lib/boot2docker/consul_vm3.sh
docker rm consul_vm3 
docker run -d      \
 --name consul_vm3 \
 --net host        \
 -p 8500:8500      \
 -p 8600:8600      \
 -p 8301:8301      \
 -p 8302:8302      \
 -v /consul/data   \
 consul:1.2.3      \
 consul agent      \
         -server                   \
         -ui                       \
         -datacenter=dc1           \ 
         -data-dir=/consul/data    \
         -node=consul_vm3          \
         -bind=192.168.1.13        \
         -client=0.0.0.0           \
# 手工将 192.168.1.13 agent 加到 192.168.1.12 所在的集群中:
docker run --rm consul:1.2.3 consul join -http-addr=192.168.1.13:8500 192.168.1.12 

# 另外, 手工关闭 192.168.1.13 agent 的命令为: 
docker run --rm consul:1.2.3 consul leave -http-addr=192.168.1.13:8500


=============================
参考
=============================
Consul -- 分布式配置中心
https://340starobserver.github.io/2018/03/08/Consul-Cluster/

Consul 使用手册
http://www.liangxiansen.cn/2017/04/06/consul/

consul 支持多数据中心的服务发现与配置共享工具
https://deepzz.com/post/the-consul-of-discovery-and-configure-services.html

猜你喜欢

转载自www.cnblogs.com/harrychinese/p/consul_cluster.html