这是本节的多页打印视图。 点击此处打印.

返回本页常规视图.

Consul学习笔记

1 - 介绍

Consul的介绍,以及Consul的资料收集

1.1 - 概况

介绍Consul概况

介绍

consul 介绍

来自consul网站首页的介绍 https://www.consul.io/

基于身份的网络

Consul使用服务身份和传统的网络实践,帮助企业安全地连接在任何环境中运行的应用程序。

定位

服务网络的支柱

一个现代的服务网络解决方案要求我们回答四个具体问题。我的服务在哪里运行?我如何保障它们之间的通信?我如何使常规的网络任务自动化?我如何控制对我的环境的访问?

  • 发现服务:创建一个中央注册中心,实时跟踪服务、更新和健康状态。

  • 安全联网:确保所有服务与服务之间的通信都是经过认证、授权和加密的。

  • 自动化网络:通过自动执行关键的网络任务来减少运维的负担

  • 访问服务:在入口处控制对服务的访问并集中管理流量

版本

开源版本

开源版本是自管理的,总是免费的

下载开源的Consul二进制文件,并在本地或你的环境中运行。

云托管版本

通过HCP(HashiCorp Cloud Platform / HashiCorp云平台)上的完全托管的Consul集群,在几分钟内就能启动和运行。

常见的使用案例

  • 用DNS或HTTP发现服务:使用DNS或HTTP API来发现已注册的服务和它们在Consul的位置

  • 控制对服务的访问:通过API网关控制对在Consul服务网中运行的服务的访问

  • 动态负载平衡:将人工网络任务自动化,并减少票据队列作为奖励。

  • 自动化的网络任务:更快地配置应用程序。自动执行复杂的网络任务。分离关注点,使运营商能够轻松管理和优化网络。

  • mTLS加密:用mTLS进行认证,并对服务之间的连接进行加密。

  • 多平台服务网格:Consul服务网格可以在任何环境下部署,并支持多种运行机制,包括Kubernetes、Nomad和VM。

  • 可观察性:通过Consul的内置UI或包含的APM集成,可视化服务网格。

  • L7流量管理:实施细粒度的流量策略,以便在不同的服务中路由和拆分流量。

1.2 - 资料

收集Consul的各种资料

官方资料

学习资料

2 - 安装部署

在各种环境下安装部署consul服务

2.1 - 安装概述

概述介绍Consul的安装

下载安装

从官方下载对应操作系统的版本:

https://www.consul.io/downloads.html

consul不需要安装,简单解压缩下载文件即可。

简单启动

以最简单的方式启动一个 consul server 以便简单体验,或者开发使用。

cd consul
./consul agent -server -data-dir ./server1 -bind=127.0.0.1 -ui
  • -data-dir 参数指定数据存储目录
  • -bind 参数指定绑定的地址,有多个网络接口时需要指定
  • -ui 参数指定开启UI界面,这样可以通过 http://localhost:8500/ui 这样的地址访问 consul 自带的web UI 界面。

2.2 - Linux安装

在linxu上安装配置consul

参考官方文档: https://developer.hashicorp.com/consul/tutorials/get-started-vms/virtual-machine-gs-deploy

Consul是一个服务网络解决方案,使你能够管理服务之间以及跨内部和多云环境和运行时间的安全网络连接。Consul提供服务发现、服务网、流量管理和网络基础设施设备的自动更新。

在本教程中,你将在一个虚拟机(VM)上配置、部署和启动一个Consul服务器。部署完Consul后,你将使用用户界面、CLI和API与Consul进行交互。

在下面的教程中,你将部署一个演示应用程序,并将其配置为使用Consul的服务发现和服务网格功能。在这个过程中,你将学习如何利用Consul来安全地连接你在任何环境下运行的服务。

搭建单个集群

前提条件

  • 在vm上安装 consul 二进制文件

    https://developer.hashicorp.com/consul/downloads 页面下载 consul 的 linunx_amd64 版本:

    https://releases.hashicorp.com/consul/1.13.3/consul_1.13.3_linux_amd64.zip

    解压缩之后将 consul 二进制放到 ~/work/soft/consul目录下,将 consul 加入到path中:

    # consul
    export PATH=$PATH:/home/sky/work/soft/consul
    

    检查consul 版本:

    $ consul version    
    Consul v1.13.3
    Revision b29e5894
    Build Date 2022-10-19T19:49:59Z
    Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
    
  • git

  • curl,jq,dig

    sudo apt install curl jq
    

生成consul服务器配置

为了安全地配置Consul,确保Consul服务器和客户之间的所有通信都不能被非预期的代理访问,你需要向Consul提供:

  • 一个 gossip 加密密钥。
  • 一个来自私有CA的根证书授权(CA)证书。Consul将使用该CA证书来签署Consul数据中心的所有证书。
  • 用于你打算部署的每台服务器的证书密钥对,由上述CA签署。

此外,你要启用ACL,以确保对你的Consul数据中心的每个请求都得到授权。

本教程和交互式实验环境使用教程的GitHub资源库中的脚本来生成这些秘密和Consul配置文件。你将使用这些脚本来生成你的Consul服务器配置。

在个人环境中,clone 下面的 learn-consul-get-started-vms 仓库:

cd soft/consul
git clone https://github.com/hashicorp-education/learn-consul-get-started-vms.git

创建一个名为 default 的默认集群配置,日常开发用,就用自己的用户名:

export DATACENTER="dc1" \
export DOMAIN="consul" \
export CONSUL_DATA_DIR="/home/sky/work/soft/consul/default/data" \
export CONSUL_CONFIG_DIR="/home/sky/work/soft/consul/default/config"

创建配置和运行 consul server agent 的所有必须的文件:

 $./scripts/generate_consul_server_config.sh  

Clean existing configuration
Generate Consul folders
Generate agent configuration - agent-server-secure.hcl
Generate server configuration - agent-server-specific.hcl
Generate gossip encryption key configuration - agent-gossip-encryption.hcl
Create CA for Consul datacenter
==> Saved consul-agent-ca.pem
==> Saved consul-agent-ca-key.pem
Create server Certificate and key pair
==> 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
Generate TLS configuration - agent-server-tls.hcl
Generate ACL configuration - agent-server-acl.hcl
Validate configuration
Config validation failed: Multiple private IPv4 addresses found. Please configure one with 'bind' and/or 'advertise'.
Configuration invalid. Exiting.

错误提示:有多个IPv4地址。检查 scripts/generate_consul_server_config.sh 文件可以看到最后执行检验的命令是:

$ consul validate ${CONSUL_CONFIG_DIR}
Config validation failed: Multiple private IPv4 addresses found. Please configure one with 'bind' and/or 'advertise'.

此时生成的配置文件存放在前面制定的 /home/sky/work/soft/consul/default/config 目录下:

$  config pwd
/home/sky/work/soft/consul/default/config
$  config ls
agent-gossip-encryption.hcl  agent-server-specific.hcl  consul-agent-ca.pem
agent-server-acl.hcl         agent-server-tls.hcl       dc1-server-consul-0-key.pem
agent-server-secure.hcl      consul-agent-ca-key.pem    dc1-server-consul-0.pem

修改 agent-server-secure.hcl 文件,在address 下面加入 bind_addr 和 advertise_addr 设置:

# Addresses and ports
addresses {
  grpc = "127.0.0.1"
  https = "0.0.0.0"
  dns = "0.0.0.0"
}

# add if there are multiple ipv4 address
bind_addr = "192.168.0.10"
advertise_addr = "192.168.0.10"

也可以后续在启动 consul agent 时手工绑定某个IP地址,比如 consul agent -bind 192.168.0.10

配置文件

脚本生成了多个配置文件,将配置分开,这样更容易阅读和调整它们,以适应环境。下面是生成的文件和它们的作用描述。

  • agent-gossip-encryption.hcl 文件配置 gossip 加密。
  • agent-server-acl.hcl 文件配置 ACL 系统。
  • agent-server-secure.hcl 文件配置了安全服务器代理。这是配置示例,不应该在生产环境中使用。
  • agent-server-specific.hcl 文件包含特定的服务器参数。
  • agent-server-tls.hcl 文件配置了特定的TLS加密。
  • *.pem 文件是证书密钥对,Consul用来执行数据中心通信的mTLS。

启动 consul 服务器

consul agent -node=consul -config-dir=${CONSUL_CONFIG_DIR} -data-dir=${CONSUL_DATA_DIR} 

报错:

==> Error loading from /etc/consul/config/consul-agent-ca.pem: open /etc/consul/config/consul-agent-ca.pem: no such file or directory

打开生成的配置文件 agent-server-tls.hcl

## TLS Encryption (requires cert files to be present on the server nodes)
ca_file   = "/etc/consul/config/consul-agent-ca.pem"
cert_file = "/home/sky/work/soft/consul/default/config/dc1-server-consul-0.pem"
key_file  = "/home/sky/work/soft/consul/default/config/dc1-server-consul-0-key.pem"

这里的 ca_file 有问题,修改为使用 CONSUL_CONFIG_DIR :

## TLS Encryption (requires cert files to be present on the server nodes)
#ca_file   = "/etc/consul/config/consul-agent-ca.pem"   
ca_file   = "/home/sky/work/soft/consul/default/config/consul-agent-ca.pem"
cert_file = "/home/sky/work/soft/consul/default/config/dc1-server-consul-0.pem"
key_file  = "/home/sky/work/soft/consul/default/config/dc1-server-consul-0-key.pem"

重新执行:

consul agent -node=consul -config-dir=${CONSUL_CONFIG_DIR} -data-dir=${CONSUL_DATA_DIR} 

就可以正常启动了:

==> Starting Consul agent...
              Version: '1.13.3'
           Build Date: '2022-10-19 19:49:59 +0000 UTC'
              Node ID: '2e015547-e8dc-66ba-e5b4-6d85d7360d68'
            Node name: 'consul'
           Datacenter: 'dc1' (Segment: '<all>')
               Server: true (Bootstrap: true)
          Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: 8443, gRPC: 8502, DNS: 8600)
         Cluster Addr: 192.168.0.10 (LAN: 8301, WAN: 8302)
    Gossip Encryption: true
     Auto-Encrypt-TLS: true
            HTTPS TLS: Verify Incoming: false, Verify Outgoing: true, Min Version: TLSv1_2
             gRPC TLS: Verify Incoming: false, Min Version: TLSv1_2
     Internal RPC TLS: Verify Incoming: true, Verify Outgoing: true (Verify Hostname: true), Min Version: TLSv1_2

备注:提交了一个 issue https://github.com/hashicorp/consul/issues/15331

为方便使用,添加 alias

alias start-consul='consul agent -node=consul -config-dir="/home/sky/work/soft/consul/default/config" -data-dir="/home/sky/work/soft/consul/default/data"'

配置 consul CLI

consul acl bootstrap --format json > /home/sky/work/soft/consul/default/acl-token-bootstrap.json

备注:注意这个命令只能跑一次,因此要小心保存生成的acl文件。

执行下面命令获取一下SecretID:

cat "${CONSUL_CONFIG_DIR}"/../acl-token-bootstrap.json | jq -r ".SecretID"

5919d75e-41a5-aa0c-480a-e26863c0ff55

记下这个 SecretID,后面登录 consul UI 页面时需要用到。

配置环境变量(前四个是之前配置的,这里也要用到),注意修改 CONSUL_HTTP_ADDR :

export DATACENTER="dc1" \
export DOMAIN="consul" \
export CONSUL_DATA_DIR="/home/sky/work/soft/consul/default/data" \
export CONSUL_CONFIG_DIR="/home/sky/work/soft/consul/default/config" \
export CONSUL_HTTP_ADDR="https://192.168.0.10:8443" \
export CONSUL_HTTP_SSL=true \
export CONSUL_CACERT="${CONSUL_CONFIG_DIR}/consul-agent-ca.pem" \
export CONSUL_TLS_SERVER_NAME="server.${DATACENTER}.${DOMAIN}" \
export CONSUL_FQDN_ADDR="consul" \
export CONSUL_HTTP_TOKEN=`cat "${CONSUL_CONFIG_DIR}"/../acl-token-bootstrap.json | jq -r ".SecretID"`

执行 consul info 命令检验一下:

consul info                                                                                     
agent:
	check_monitors = 0
	check_ttls = 0
	checks = 0
	services = 0
build:
	prerelease = 
	revision = b29e5894
	version = 1.13.3
	version_metadata = 
consul:
	acl = enabled
	bootstrap = true
	known_datacenters = 1
	leader = true
	leader_addr = 192.168.0.10:8300
	server = true
raft:
	applied_index = 21
	commit_index = 21
	fsm_pending = 0
	last_contact = 0
	last_log_index = 21
	last_log_term = 2
	last_snapshot_index = 0
	last_snapshot_term = 0
	latest_configuration = [{Suffrage:Voter ID:138d8891-d164-e6c0-48e1-9ac5232eb83c Address:192.168.0.10:8300}]
	latest_configuration_index = 0
	num_peers = 0
	protocol_version = 3
	protocol_version_max = 3
	protocol_version_min = 0
	snapshot_version_max = 1
	snapshot_version_min = 0
	state = Leader
	term = 2
runtime:
	arch = amd64
	cpu_count = 24
	goroutines = 117
	max_procs = 24
	os = linux
	version = go1.18.1
serf_lan:
	coordinate_resets = 0
	encrypted = true
	event_queue = 1
	event_time = 2
	failed = 0
	health_score = 0
	intent_queue = 0
	left = 0
	member_time = 1
	members = 1
	query_queue = 0
	query_time = 1
serf_wan:
	coordinate_resets = 0
	encrypted = true
	event_queue = 0
	event_time = 1
	failed = 0
	health_score = 0
	intent_queue = 0
	left = 0
	member_time = 1
	members = 1
	query_queue = 0
	query_time = 1

创建服务器端令牌

暂时先不设置,应该不影响测试

和 consul 服务器交互

用命令行查看consul成员:

$ consul members
Build   Protocol  DC   Partition  Segment
consul  192.168.0.10:8301  alive   server  1.13.3  2         dc1  default    <all>

也可以打开UI页面 https://192.168.0.10:8443/, 登录时使用前面得到的 SecretID

方便期间,设置环境变量的事情也交给 alias 吧:

alias set_consul='export DATACENTER=dc1;export DOMAIN=consul;export CONSUL_DATA_DIR="/home/sky/work/soft/consul/default/data ";export CONSUL_CONFIG_DIR="/home/sky/work/soft/consul/default/config";export CONSUL_HTTP_ADDR="https://192.168.0.10:8443" ;exportCONSUL_HTTP_SSL=true;export CONSUL_CACERT="${CONSUL_CONFIG_DIR}/consul-agent-ca.pem";export CONSUL_TLS_SERVER_NAME="server.${DATACENTER}.${DOMAIN}";export CONSUL_FQDN_ADDR="consul";export CONSUL_HTTP_TOKEN=5919d75e-41a5-aa0c-480a-e26863c0ff55'

搭建多个测试用的集群

为了方便测试跨集群访问的功能,需要在当前机器上额外搭建多个consul集群

搭建集群2

准备目录并设置:

export DATACENTER="dc1" \
export DOMAIN="consul" \
export CONSUL_DATA_DIR="/home/sky/work/soft/consul/cluster2/data" \
export CONSUL_CONFIG_DIR="/home/sky/work/soft/consul/cluster2/config"

执行:

./scripts/generate_consul_server_config.sh

记得修改 agent-server-tls.hcl 中错误的 ca_file 设置

ca_file   = "/home/sky/work/soft/consul/cluster2/config/consul-agent-ca.pem"

修改 agent-server-secure.hcl 文件,在所有端口前加2:

ports {
  grpc  = 28502
  http  = 28500
  https = 28443
  dns   = 28600
  server = 28300
}
# add if there are multiple ipv4 address
bind_addr = "192.168.0.10"
advertise_addr = "192.168.0.10"

启动:

consul agent -node=consul -config-dir=${CONSUL_CONFIG_DIR} -data-dir=${CONSUL_DATA_DIR} -serf-wan-port=28302 -serf-lan-port=28301

备注:serf-wan-port 和 serf-lan-port 不知道怎么在配置文件中设置,只好在命令行中输入。

还是加个 alias 吧:

alias start-consul2='consul agent -node=consul -config-dir="/home/sky/work/soft/consul/cluster2/config" -data-dir="/home/sky/work/soft/consul/cluster2/data" -serf-wan-port=28302 -serf-lan-port=28301'

启动客户端前配置环境变量(前四个是之前配置的,这里也要用到),注意修改 CONSUL_HTTP_ADDR :

export DATACENTER="dc1" \
export DOMAIN="consul" \
export CONSUL_DATA_DIR="/home/sky/work/soft/consul/cluster2/data" \
export CONSUL_CONFIG_DIR="/home/sky/work/soft/consul/cluster2/config" \
export CONSUL_HTTP_ADDR="https://192.168.0.10:28443" \
export CONSUL_HTTP_SSL=true \
export CONSUL_CACERT="${CONSUL_CONFIG_DIR}/consul-agent-ca.pem" \
export CONSUL_TLS_SERVER_NAME="server.${DATACENTER}.${DOMAIN}" \
export CONSUL_FQDN_ADDR="consul"

生成acl:

consul acl bootstrap --format json > /home/sky/work/soft/consul/cluster2/acl-token-bootstrap.json

执行下面命令获取一下SecretID:

cat "${CONSUL_CONFIG_DIR}"/../acl-token-bootstrap.json | jq -r ".SecretID"

63ceeceb-d3f2-b7b2-039c-fbd2ed986b21

记下这个 SecretID,后面登录 consul UI 页面时需要用到。

export CONSUL_HTTP_TOKEN=`cat "${CONSUL_CONFIG_DIR}"/../acl-token-bootstrap.json | jq -r ".SecretID"`

方便起见,设置环境变量的事情交给 alias 吧:

alias set_consul2='export DATACENTER=dc1;export DOMAIN=consul;export CONSUL_DATA_DIR="/home/sky/work/soft/consul/cluster2/data ";export CONSUL_CONFIG_DIR="/home/sky/work/soft/consul/cluster2/config";export CONSUL_HTTP_ADDR="https://192.168.0.10:28443" ;exportCONSUL_HTTP_SSL=true;export CONSUL_CACERT="${CONSUL_CONFIG_DIR}/consul-agent-ca.pem";export CONSUL_TLS_SERVER_NAME="server.${DATACENTER}.${DOMAIN}";export CONSUL_FQDN_ADDR="consul";export CONSUL_HTTP_TOKEN=63ceeceb-d3f2-b7b2-039c-fbd2ed986b21'

检验:

consul info
consul members

打开UI页面: https://192.168.0.10:28443/

2.3 - MacOS M1安装

在 MacOS M1上安装配置consul

参考官方文档: https://developer.hashicorp.com/consul/tutorials/get-started-vms/virtual-machine-gs-deploy 和在linux上的安装。

搭建单个集群

前提条件

  • 在vm上安装 consul 二进制文件

    https://developer.hashicorp.com/consul/downloads 页面下载 consul 的 linunx_amd64 版本:

    https://releases.hashicorp.com/consul/1.13.3/consul_1.13.3_darwin_arm64.zip

    解压缩之后将 consul 二进制放到 ~/work/soft/consul目录下,将 consul 加入到path中:

    # consul
    export PATH=$PATH:/Users/sky/work/soft/consul
    

    检查consul 版本:

    $ consul --version
    Consul v1.13.3
    Revision b29e5894
    Build Date 2022-10-19T19:49:59Z
    Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
    
  • git

  • curl,jq,dig

    brew install curl jq
    

生成consul服务器配置

clone 下面的 learn-consul-get-started-vms 仓库:

cd work/soft/consul
git clone https://github.com/hashicorp-education/learn-consul-get-started-vms.git

创建一个名为 default 的默认集群配置,日常开发用,就用自己的用户名:

export DATACENTER="dc1" \
export DOMAIN="consul" \
export CONSUL_DATA_DIR="/Users/sky/work/soft/consul/default/data" \
export CONSUL_CONFIG_DIR="/Users/sky/work/soft/consul/default/config"

创建配置和运行 consul server agent 的所有必须的文件:

 # 确保 CONSUL_DATA_DIR 和 CONSUL_CONFIG_DIR 目录已经创建好
 
 $./scripts/generate_consul_server_config.sh  

......
Configuration invalid. Exiting.

配置文件

启动 consul 服务器

consul agent -node=consul -config-dir=${CONSUL_CONFIG_DIR} -data-dir=${CONSUL_DATA_DIR} 

报错:

==> Error loading from /etc/consul/config/consul-agent-ca.pem: open /etc/consul/config/consul-agent-ca.pem: no such file or directory

打开生成的配置文件 agent-server-tls.hcl

## TLS Encryption (requires cert files to be present on the server nodes)
ca_file   = "/etc/consul/config/consul-agent-ca.pem"
cert_file = "/Users/sky/work/soft/consul/default/config/dc1-server-consul-0.pem"
key_file  = "/Users/sky/work/soft/consul/default/config/dc1-server-consul-0-key.pem"

这里的 ca_file 有问题,修改为使用 CONSUL_CONFIG_DIR :

## TLS Encryption (requires cert files to be present on the server nodes)
#ca_file   = "/etc/consul/config/consul-agent-ca.pem"   
ca_file   = "/Users/sky/work/soft/consul/default/config/consul-agent-ca.pem"
cert_file = "/Users/sky/work/soft/consul/default/config/dc1-server-consul-0.pem"
key_file  = "/Users/sky/work/soft/consul/default/config/dc1-server-consul-0-key.pem"

重新执行:

consul agent -node=consul -config-dir=${CONSUL_CONFIG_DIR} -data-dir=${CONSUL_DATA_DIR} 

为方便使用,添加 alias

alias start-consul='consul agent -node=consul -config-dir="/Users/sky/work/soft/consul/default/config" -data-dir="/Users/sky/work/soft/consul/default/data"'

配置 consul CLI

打开另一个终端:

# set ENV first
export DATACENTER="dc1" \
export DOMAIN="consul" \
export CONSUL_DATA_DIR="/Users/sky/work/soft/consul/default/data" \
export CONSUL_CONFIG_DIR="/Users/sky/work/soft/consul/default/config"

consul acl bootstrap --format json > /Users/sky/work/soft/consul/default/acl-token-bootstrap.json

备注:注意这个命令只能跑一次,因此要小心保存生成的acl文件。

执行下面命令获取一下SecretID:

cat "${CONSUL_CONFIG_DIR}"/../acl-token-bootstrap.json | jq -r ".SecretID"

0b8ca5d0-aace-9d33-cf13-62059fd39573

记下这个 SecretID,后面登录 consul UI 页面时需要用到。

配置环境变量(前四个是之前配置的,这里也要用到),注意修改 CONSUL_HTTP_ADDR :

export DATACENTER="dc1" \
export DOMAIN="consul" \
export CONSUL_DATA_DIR="/Users/sky/work/soft/consul/default/data" \
export CONSUL_CONFIG_DIR="/Users/sky/work/soft/consul/default/config" \
export CONSUL_HTTP_ADDR="https://127.0.0.1:8443" \
export CONSUL_HTTP_SSL=true \
export CONSUL_CACERT="${CONSUL_CONFIG_DIR}/consul-agent-ca.pem" \
export CONSUL_TLS_SERVER_NAME="server.${DATACENTER}.${DOMAIN}" \
export CONSUL_FQDN_ADDR="consul" \
export CONSUL_HTTP_TOKEN=`cat "${CONSUL_CONFIG_DIR}"/../acl-token-bootstrap.json | jq -r ".SecretID"`

执行 consul info 命令检验一下:

consul info                                                                                     
agent:
	check_monitors = 0
	check_ttls = 0
	checks = 0
	services = 0

创建服务器端令牌

暂时先不设置,应该不影响测试

和 consul 服务器交互

用命令行查看consul成员:

$ consul members
Build   Protocol  DC   Partition  Segment
consul  192.168.0.10:8301  alive   server  1.13.3  2         dc1  default    <all>

也可以打开UI页面 https://192.168.0.10:8443/, 登录时使用前面得到的 SecretID

方便期间,设置环境变量的事情也交给 alias 吧:

alias set_consul='export DATACENTER=dc1;export DOMAIN=consul;export CONSUL_DATA_DIR="/Users/sky/work/soft/consul/default/data ";export CONSUL_CONFIG_DIR="/Users/sky/work/soft/consul/default/config";export CONSUL_HTTP_ADDR="https://127.0.0.1:8443" ;exportCONSUL_HTTP_SSL=true;export CONSUL_CACERT="${CONSUL_CONFIG_DIR}/consul-agent-ca.pem";export CONSUL_TLS_SERVER_NAME="server.${DATACENTER}.${DOMAIN}";export CONSUL_FQDN_ADDR="consul";export CONSUL_HTTP_TOKEN=0b8ca5d0-aace-9d33-cf13-62059fd39573'

搭建多个测试用的集群

为了方便测试跨集群访问的功能,需要在当前机器上额外搭建多个consul集群

搭建集群2

准备目录并设置:

export DATACENTER="dc1" \
export DOMAIN="consul" \
export CONSUL_DATA_DIR="/Users/sky/work/soft/consul/cluster2/data" \
export CONSUL_CONFIG_DIR="/Users/sky/work/soft/consul/cluster2/config"

执行:

./scripts/generate_consul_server_config.sh

记得修改 agent-server-tls.hcl 中错误的 ca_file 设置

ca_file   = "/Users/sky/work/soft/consul/cluster2/config/consul-agent-ca.pem"

修改 agent-server-secure.hcl 文件,在所有端口前加2:

ports {
  grpc  = 28502
  http  = 28500
  https = 28443
  dns   = 28600
  server = 28300 # 多加一行
}

启动:

consul agent -node=consul -config-dir=${CONSUL_CONFIG_DIR} -data-dir=${CONSUL_DATA_DIR} -serf-wan-port=28302 -serf-lan-port=28301

备注:serf-wan-port 和 serf-lan-port 不知道怎么在配置文件中设置,只好在命令行中输入。

还是加个 alias 吧:

alias start-consul2='consul agent -node=consul -config-dir="/Users/sky/work/soft/consul/cluster2/config" -data-dir="/Users/sky/work/soft/consul/cluster2/data" -serf-wan-port=28302 -serf-lan-port=28301'

启动客户端前配置环境变量(前四个是之前配置的,这里也要用到),注意修改 CONSUL_HTTP_ADDR :

export DATACENTER="dc1" \
export DOMAIN="consul" \
export CONSUL_DATA_DIR="/Users/sky/work/soft/consul/cluster2/data" \
export CONSUL_CONFIG_DIR="/Users/sky/work/soft/consul/cluster2/config" \
export CONSUL_HTTP_ADDR="https://127.0.0.1:28443" \
export CONSUL_HTTP_SSL=true \
export CONSUL_CACERT="${CONSUL_CONFIG_DIR}/consul-agent-ca.pem" \
export CONSUL_TLS_SERVER_NAME="server.${DATACENTER}.${DOMAIN}" \
export CONSUL_FQDN_ADDR="consul"

生成acl:

consul acl bootstrap --format json > /Users/sky/work/soft/consul/cluster2/acl-token-bootstrap.json

执行下面命令获取一下SecretID:

cat "${CONSUL_CONFIG_DIR}"/../acl-token-bootstrap.json | jq -r ".SecretID"

6c0b9c61-c642-99db-9e58-972d5b838b9b

记下这个 SecretID,后面登录 consul UI 页面时需要用到。

export CONSUL_HTTP_TOKEN=`cat "${CONSUL_CONFIG_DIR}"/../acl-token-bootstrap.json | jq -r ".SecretID"`

方便起见,设置环境变量的事情交给 alias 吧:

alias set_consul2='export DATACENTER=dc1;export DOMAIN=consul;export CONSUL_DATA_DIR="/Users/sky/work/soft/consul/cluster2/data ";export CONSUL_CONFIG_DIR="/Users/sky/work/soft/consul/cluster2/config";export CONSUL_HTTP_ADDR="https://127.0.0.1:28443" ;exportCONSUL_HTTP_SSL=true;export CONSUL_CACERT="${CONSUL_CONFIG_DIR}/consul-agent-ca.pem";export CONSUL_TLS_SERVER_NAME="server.${DATACENTER}.${DOMAIN}";export CONSUL_FQDN_ADDR="consul";export CONSUL_HTTP_TOKEN=6c0b9c61-c642-99db-9e58-972d5b838b9b'

检验:

consul info
consul members

打开UI页面: https://127.0.0.1:28443/,用上面保存的 SecretID 登录。