這篇文章的目的:搭建帶有ACL控制的consul1.5集羣。
具體概念及配置說明,後面我會再寫文章補充說明。html
我這裏起了四臺虛擬機,三臺用做Server agent,一臺用做Client agent。(說明:固然Client能夠配置多個,這裏因爲開太多虛擬機比較耗費資源,就只設置了一個。)node
機器ip(機器名) | http端口(其餘端口使用默認值) | Agent類型 | 節點名稱 |
---|---|---|---|
10.2111.55.28 (node1) | 8500 | server | consul-server1 |
10.2111.55.25 (node2) | 8500 | server | consul-server2 |
10.2111.55.26 (node3) | 8500 | server | consul-server3 |
10.2111.55.27 (node4) | 8500 | client 帶ui | consul-client1 |
consul-server1.jsonweb
{ "datacenter":"dc1", "primary_datacenter":"dc1", "bootstrap_expect":1, "start_join":[ "10.211.55.25", "10.211.55.26" ], "retry_join":[ "10.211.55.25", "10.211.55.26" ], "advertise_addr": "10.211.55.28", "bind_addr": "10.211.55.28", "server":true, "connect":{ "enabled":true }, "node_name":"consul-server1", "data_dir":"/opt/consul/data/", "enable_script_checks":false, "enable_local_script_checks":true, "log_file":"/opt/consul/log/", "log_level":"info", "log_rotate_bytes":100000000, "log_rotate_duration":"24h", "encrypt":"krCysDJnrQ8dtA7AbJav8g==", "acl":{ "enabled":true, "default_policy":"deny", "enable_token_persistence":true, "tokens":{ "master":"cd76a0f7-5535-40cc-8696-073462acc6c7" } } }
consul-server2.jsonjson
{ "datacenter":"dc1", "primary_datacenter":"dc1", "advertise_addr": "10.211.55.25", "bind_addr": "10.211.55.25", "server":true, "connect":{ "enabled":true }, "node_name":"consul-server2", "data_dir":"/opt/consul/data/", "enable_script_checks":false, "enable_local_script_checks":true, "log_file":"/opt/consul/log/", "log_level":"info", "log_rotate_bytes":100000000, "log_rotate_duration":"24h", "encrypt":"krCysDJnrQ8dtA7AbJav8g==", "acl":{ "enabled":true, "default_policy":"deny", "enable_token_persistence":true, "tokens":{ "master":"cd76a0f7-5535-40cc-8696-073462acc6c7" } } }
consul-server3.jsonbootstrap
{ "datacenter":"dc1", "primary_datacenter":"dc1", "advertise_addr":"10.211.55.26", "bind_addr":"10.211.55.26", "server":true, "connect":{ "enabled":true }, "node_name":"consul-server3", "data_dir":"/opt/consul/data/", "enable_script_checks":false, "enable_local_script_checks":true, "log_file":"/opt/consul/log/", "log_level":"info", "log_rotate_bytes":100000000, "log_rotate_duration":"24h", "encrypt":"krCysDJnrQ8dtA7AbJav8g==", "acl":{ "enabled":true, "default_policy":"deny", "enable_token_persistence":true, "tokens":{ "master":"cd76a0f7-5535-40cc-8696-073462acc6c7" } } }
能夠看到,consul-server2和consul-server3的配置相似,只是換了下ip和端口;另外consul-server1主要是多了開始鏈接和重試鏈接等配置。
接着,啓動集羣:
在機器10.2111.55.25 (node2)上執行,./consul agent -config-file start-conf/consul-server2.json
在機器10.2111.55.26 (node3)上執行,./consul agent -config-file start-conf/consul-server3.json
在機器10.2111.55.28 (node1)上執行,./consul agent -config-file start-conf/consul-server1.jsonvim
當上面的語句執行完以後,會發現協調更新因爲ACL被阻塞。以下圖:
通過查看官方文檔,發現是因爲未生成和配置agent-token致使。瀏覽器
在任意一臺server上執行下面的語句來生成agent-token:app
curl \ --request PUT \ --header "X-Consul-Token: cd76a0f7-5535-40cc-8696-073462acc6c7" \ --data \ '{ "Name": "Agent Token", "Type": "client", "Rules": "node \"\" { policy = \"write\" } service \"\" { policy = \"read\" }" }' http://127.0.0.1:8500/v1/acl/create
此時會返回生成的agent-token
將生成的agent_token設置到每一個server agent的配置文件中。
此時consul-server1.json, consul-server2.json, consul-server3.json中acl部分就變爲:curl
"acl":{ "enabled":true, "default_policy":"deny", "enable_token_persistence":true, "tokens":{ "master":"cd76a0f7-5535-40cc-8696-073462acc6c7", "agent":"deaa315d-98c5-b9f6-6519-4c8f6574a551" } }
也就是多了agent這個配置。ui
接着一次重啓各個server agent(把以前的進程先停掉)
在機器10.2111.55.25 (node2)上執行,./consul agent -config-file start-conf/consul-server2.json
在機器10.2111.55.26 (node3)上執行,./consul agent -config-file start-conf/consul-server3.json
在機器10.2111.55.28 (node1)上執行,./consul agent -config-file start-conf/consul-server1.json
等server agent集羣穩定下來以後,咱們會看到以前的ACL block已經解決。
{ "datacenter":"dc1", "primary_datacenter":"dc1", "advertise_addr": "10.211.55.27", "start_join":[ "10.211.55.25", "10.211.55.26", "10.211.55.28" ], "retry_join":[ "10.211.55.25", "10.211.55.26", "10.211.55.28" ], "bind_addr":"10.211.55.27", "node_name":"consul-client1", "client_addr":"0.0.0.0", "connect":{ "enabled":true }, "data_dir":"/opt/consul/data/", "log_file":"/opt/consul/log/", "log_level":"info", "log_rotate_bytes":100000000, "log_rotate_duration":"24h", "encrypt":"krCysDJnrQ8dtA7AbJav8g==", "ui":true, "enable_script_checks":false, "enable_local_script_checks":true, "disable_remote_exec":true, "ports":{ "http":7110 }, "acl":{ "enabled":true, "default_policy":"deny", "enable_token_persistence":true, "tokens":{ "agent":"deaa315d-98c5-b9f6-6519-4c8f6574a551" } } }
上面的配置主要是多了ui,代表帶web-ui(能夠在瀏覽器中查看)。
另外也是設置了第三步中生成的agent token。
在機器10.2111.55.27 (node4)上執行,./consul agent -config-file start-conf/consul-client1.json
通過前面一番配置,本覺得已經搞定了全部東西,此時只想摸摸本身帥氣的頭髮。
可一執行./consul members, 想看看我這裏都有哪些成員,竟然發現一個都沒有
通過查看官方文檔及搜索,發現是沒有配置環境變量致使。
1.給三個server的環境變量添加CONSUL_HTTP_TOKEN, vim /etc/profile添加下面一句
export CONSUL_HTTP_TOKEN=cd76a0f7-5535-40cc-8696-073462acc6c7
而後,source /etc/profile一下。
爲了簡單方便,我這裏配了最大的權限即master_token
此時發現./consul members已經有數據了
2.給client agent 設置環境變量
因爲client agent 帶web-ui,這裏你的公司不必定對外開放8500端口,因此我這裏把它改爲了7110,方便在外網查看。
不過此時須要添加一個環境變量CONSUL_HTTP_ADDR,來告訴命令行不是使用默認的127.0.0.1:8500
更改client-agent的環境變量,在最後添加下面兩行
#consul http-token export CONSUL_HTTP_TOKEN=cd76a0f7-5535-40cc-8696-073462acc6c7 #only consul-client1 need, because http port has changed to 7110 export CONSUL_HTTP_ADDR=127.0.0.1:7110
此時發如今client agent上執行./consul members也是ok的。
在client-agent上,輸入127.0.0.1:7110, 點擊ACL, 輸入master-token便可。以下圖:
https://www.consul.io/docs/ac...
https://www.consul.io/docs/ag...
https://www.consul.io/docs/co...