Consul 集群部署 + ACL 配置

3,195 阅读3分钟

如何快速部署一个集群/系统?那种只用敲一条命令所有的组件部署完成的绝佳体验,我只从docker-composeansible上体验过。

使用docker-compose快速搭建Consul集群

  1. 编辑docker-compose.yml定义Consul集群。
  2. 执行$ docker-compose up就能将docker-compose.yml定义的Consul集群进行启动。
version: '2'
networks:
  byfn:
 
services:
  consul1:
    image: consul
    container_name: node1
    command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    networks:
      - byfn
 
  consul2:
    image: consul
    container_name: node2
    command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    depends_on:
        - consul1
    networks:
      - byfn
 
  consul3:
    image: consul
    container_name: node3
    command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    depends_on:
        - consul1
    networks:
      - byfn
 
  consul4:
    image: consul
    container_name: node4
    command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 -ui 
    ports:
      - 8500:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - byfn

docker-compose.yml可以看出Consul集群启动了4个节点,其中node1~node3作为Consul Server组成集群。node4作为客户端join到集群中,映射宿主机的8500端口到容器的8500端口ports: - 8500:8500,使得通过command参数-ui提供Consul UI,可以通过访问宿主机的8500访问。

Command-line Options

  • -bootstrap-expect - Consul启动集群期望的数据中心Server的节点数,只适用server模式。-bootstrap-expect=3 表示Consul会等加入到集群中的Server数据达到3才启动。
  • -node - 节点在集群中名称,必须集群中唯一。
  • -bind - 绑定集群间通信的地址。缺省为"0.0.0.0",意味着Consul将绑定本地机器的所有地址并且获取第一个可用的私有IPV4地址广播给集群其余的节点。
  • -retry-join - 顾名思义是加入到集群中并且支持重试。
  • -server - 表示agent为Consul Server模式。
  • -client - 绑定客户端接口地址,包括HTTP和DNS服务器,缺省为"127.0.0.1"。
  • -ui - 开启内建的 web ui。
  • -datacenter - 数据中心名称,缺省为"dc1"。

具体参数可以查看网关文档:Consul Configuration

除了在启动命令中带参数来配置Consul外,还可以通过-config-dir或是-config-file指定配置目录或是配置文件来配置Consul。Consul回去扫描-config-dir指定的目录下的.json或是.hcl文件。

到此一个由3个Sever节点和一个Client组成的Consul集群开始裸奔。

ACLs和加密

Consul使用ACLs提供数据和接口的保护。Consul还可以对集群间通信的RPC数据进行加密。

配置ACLs。按照官方文档将 Bootstrap the ACL Systemacl.hcl放到配置目录中,Consul启动会报文件格式错误。

最后添加如下两个配置:

  • acl.json
{
    "acl_datacenter": "dc1",
    "acl_master_token": "2a825e81-b249-444d-a18e-ab9c8ece6059"
}

需要注意一下Consul的几个Token。

acl_master_token有最高权限,acl_token用于请求资源是通过分配得到的Token,这个Token的只有一些资源的操作权限,例如:某个key的读权限。acl_master_token是启动ACL是提供的Token。acl_agent_token则是通过api进行请求获取,然后给后续加入集群中的agent,用与完成agent的acl认证。

curl \
    --request PUT \
    --header "X-Consul-Token: 2a825e81-b249-444d-a18e-ab9c8ece6059" \
    --data \
'{
  "Name": "Agent Token",
  "Type": "client",
  "Rules": "node \"\" { policy = \"write\" } service \"\" { policy = \"read\" }"
}' http://127.0.0.1:8500/v1/acl/create

{"ID": "your-agent-token"}
  • encrypt.json
{
  "encrypt": "your-encrypt-key"
}

修改`docker-compose.yml

version: '2'
networks:
  byfn:
 
services:
  consul1:
    image: consul
    container_name: node1
    volumes: 
      - /home/consul/conf:/consul/config
    command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    networks:
      - byfn
 
  consul2:
    image: consul
    container_name: node2
    volumes:
      - /home/consul/conf:/consul/config
    command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    ports:
       - 8500:8500
    depends_on:
        - consul1
    networks:
      - byfn
 
  consul3:
    image: consul
    volumes:
      - /home/consul/conf:/consul/config
    container_name: node3
    command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    depends_on:
        - consul1
    networks:
      - byfn
 
  consul4:
    image: consul
    container_name: node4
    volumes:
      - /home/consul/conf:/consul/config
    command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -ui -config-dir=/consul/config
    ports:
      - 8501:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - byfn

  consul5:
    image: consul
    container_name: node5
    volumes:
      - /home/consul/conf_without_acl:/consul/config
    command: agent -retry-join=node1 -node=ndoe5 -bind=0.0.0.0 -client=0.0.0.0  -config-dir=/consul/config
    ports:
      - 8502:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - byfn

修改内容

  1. 通过volumes挂载了配置目录给容器。
  2. 通过修改command指定了配置目录。
  3. 增加了一个不认证的Consul客户端来验证ACL Token的效果。