如何快速部署一个集群/系统?那种只用敲一条命令全部的组件部署完成的绝佳体验,我只从docker-compose
和ansible
上体验过。html
docker-compose.yml
定义Consul集群。$ 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=3
表示Consul会等加入到集群中的Server数据达到3才启动。具体参数能够查看网关文档:Consul Configurationdocker
除了在启动命令中带参数来配置Consul外,还能够经过-config-dir
或是-config-file
指定配置目录或是配置文件来配置Consul。Consul回去扫描-config-dir
指定的目录下的.json
或是.hcl
文件。json
到此一个由3个Sever节点和一个Client组成的Consul集群开始裸奔。bootstrap
Consul使用ACLs提供数据和接口的保护。Consul还能够对集群间通讯的RPC数据进行加密。api
配置ACLs。按照官方文档将 Bootstrap the ACL System 将acl.hcl
放到配置目录中,Consul启动会报文件格式错误。bash
最后添加以下两个配置:服务器
{
"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": "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
复制代码
修改内容
volumes
挂载了配置目录给容器。