如何快速部署一個集羣/系統?那種只用敲一條命令全部的組件部署完成的絕佳體驗,我只從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
掛載了配置目錄給容器。