consul 集群部署
consul 高可用部署方式,支持测试生产环境部署
consul 基于 docker 环境和基于 cloudiac 部署高可用模式,支持 client+server 模式,同时支持开启 acl 控制和 tls 通信机制
所有证书配置文件均放在服务器的 /opt/consul 下, 挂载到容器的 /consul/config 下
开启 acl 和 tls 准备
配置文件准备
配置文件主要有开启 acl 的配置,开启 tls 的配置,需要的 token,证书相关文件
token 可以使用任意 consul 服务生成,可以移植使用
tls 证书可以使用 openssl , 官方发布的证书,以及 consul 自身生成的证书,此处我们使用 consul 自身生成的证书来演示
acl 配置准备
1. 启动 consul 容器
# /usr/yunji/tls 证书生成的目录
$ mkdir -p /opt/consul
$ docker run -v /opt/consul:/consul/config -d --name=acl-server-consul -p 8500:8500 consul:latest
2. 开启 acl
$ cd /opt/consul
# cat acl.json
{
"acl":{
"enabled": true,
"default_policy": "deny",
"enable_token_persistence":true
},
"datacenter":"dc1",
"primary_datacenter": "dc1"
}
3. 重启consul
$ docker resrat acl-server-consul
4. 生成token
# 生成 acl 的 token
# 进入容器
➜ docker exec -it acl-server-consul sh
# 生成 key
$ consul acl bootstrap
AccessorID: dbeae895-cb7c-0bb4-b6fe-f49bad9d7526
SecretID : 5ecc86f0-fa68-6ddc-e848-ef382d7737ec
Description: Bootstrap Token (Global Management)
Local: false
Create Time: 2022-04-18 06:29:28.7108916 +0000 UTC
Policies:
00000000-0000-0000-0000-000000000001 - global-management
# 把生成的 SecretID 写入 acl.json
/consul/config # cat acl.json
{
"acl":{
"enabled": true,
"default_policy": "deny",
"enable_token_persistence":true,
"tokens":{
"master":"5ecc86f0-fa68-6ddc-e848-ef382d7737ec"
}
},
"datacenter":"dc1",
"primary_datacenter": "dc1"
}
tls 证书准备
1. 证书生成
# 进入 consul 容器
$ docker exec -it acl-server-consul sh
cd /consul/config
/consul/config # ls
acl.json
/consul/config # consul tls ca create
==> Saved consul-agent-ca.pem
==> Saved consul-agent-ca-key.pem
/consul/config # ls
acl.json consul-agent-ca-key.pem consul-agent-ca.pem
/consul/config # consul tls cert create -server -dc dc1
==> WARNING: Server Certificates grants authority to become a
server and access all state in the cluster including root keys
and all ACL tokens. Do not distribute them to production hosts
that are not server nodes. Store them as securely as CA keys.
==> Using consul-agent-ca.pem and consul-agent-ca-key.pem
==> Saved dc1-server-consul-0.pem
==> Saved dc1-server-consul-0-key.pem
/consul/config # ls
acl.json consul-agent-ca-key.pem consul-agent-ca.pem dc1-server-consul-0-key.pem dc1-server-consul-0.pem
挂载目录下 /opt/consul 的文件说明
acl.json 开启 acl 配置 以及 token配置
consul-agent-ca.pem:CA公共证书
dc1-server-consul-0.pem:数据中心的 Consul 服务器节点公共证书 dc1
dc1-server-consul-0-key.pem:数据中心的 Consul 服务器节点私钥 dc1
consul基于docker环境的集群部署
模式采用3节点 server+1节点 client 端 ,同时支持开启 acl 控制访问和 tls 通信保护
- TLS:使用 https 加密 consul 的 API、GRPC
- ACl:控制 Services、Nodes、KV 的读写权限
配置说明
server 端配置
acl 和 tls 配置说明
acl.json 用来控制 acl 访问
server-tls.json 用来控制 tls 通信保护访问
所有的配置文件和证书都放在服务器的 /opt/consul 目录下
$ cat acl.json
{
"acl":{
"enabled": true,
"default_policy": "deny",
"enable_token_persistence":true,
"tokens":{
"master":"5ecc86f0-fa68-6ddc-e848-ef382d7737ec"
}
},
"datacenter":"dc1",
"primary_datacenter": "dc1"
}
# tls配置
$ cat server-tls.json
{
"verify_incoming": true,
"verify_outgoing": true,
"verify_server_hostname": true,
"ca_file": "/consul/config/consul-agent-ca.pem",
"cert_file": "/consul/config/dc1-server-consul-0.pem",
"key_file": "/consul/config/dc1-server-consul-0-key.pem",
"auto_encrypt": {
"allow_tls": true
}
}
client 端配置
client-acl-tls.json 控制开启 acl 和 tls
$ cat client-acl-tls.json
{
"verify_incoming": false,
"verify_outgoing": true,
"verify_server_hostname": true,
"ca_file": "/consul/config/consul-agent-ca.pem",
"auto_encrypt": {
"tls": true
},
"acl":{
"enabled": true,
"default_policy": "deny",
"enable_token_persistence":false,
"tokens":{
"agent":"5ecc86f0-fa68-6ddc-e848-ef382d7737ec"
}
},
"datacenter":"dc1",
"primary_datacenter": "dc1"
}
docker 启动部署
server 端
服务器列表, 此处通过阿里云服务器,内网互通的模式下测试
| 服务器 ip | 节点 | 类型 |
|---|---|---|
| 192.168.0.147 | server1 | server 主节点 |
| 192.168.0.149 | server2 | server |
| 192.168.0.150 | server3 | server |
启动 server 服务
#主节点 server1 启动
docker run -d --name consul-1 --net host -v /opt/consul/:/consul/config consul:latest agent -server -bootstrap-expect 3 -ui -bind 0.0.0.0 -client 0.0.0.0 -advertise 192.168.0.147 -node 192.168.0.147 -enable-script-checks=true
#节点 server2 启动
docker run -d --name consul-2 --net host -v /opt/consul/:/consul/config consul:latest agent -server -ui -bind 0.0.0.0 -client 0.0.0.0 -retry-join 192.168.0.147 -advertise 192.168.0.149 -node 192.168.0.149 -enable-script-checks=true
#节点 server3 启动
docker run -d --name consul-2 --net host -v /opt/consul/:/consul/config consul:latest agent -server -ui -bind 0.0.0.0 -client 0.0.0.0 -retry-join 192.168.0.147 -advertise 192.168.0.150 -node 192.168.0.150 -enable-script-checks=true
client 端
服务器列表
| 服务器 ip | 节点 | 类型 |
|---|---|---|
| 192.168.0.148 | client-cli | client |
启动 client 服务
# client 节点启动
docker run -d --name consul-2 --net host -v /opt/consul/:/consul/config consul:latest agent -client -ui -bind 0.0.0.0 -client 0.0.0.0 -retry-join 192.168.0.147 -advertise 192.168.0.148 -node client-cli -enable-script-checks=true
演示效果
基于 cloudiac
基于 cloudiac 模式,可以一键申请阿里云资源,同时部署 consul 集群服务,使用完可以随时销毁, cloudiac 以『环境即服务』的方式来管理基础设施以及应用,后续会出一个详细教程,大家感兴趣的可以移步 github 了解,完全开源免费 cloudiac