Consul 集群部署 + ACL 配置

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

使用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访问。node

Command-line Optionsweb

  • -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 Configurationdocker

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

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

ACLs和加密

Consul使用ACLs提供数据和接口的保护。Consul还能够对集群间通讯的RPC数据进行加密。api

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

最后添加以下两个配置:服务器

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

须要注意一下Consul的几个Token。curl

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的效果。
相关文章
相关标签/搜索