1. 程式人生 > 實用技巧 >Consul 叢集帶ACL控制搭建

Consul 叢集帶ACL控制搭建

1.機器規劃

我這裡起了四臺虛擬機器,三臺用作Server agent,一臺用作Client agent。(說明:當然Client可以配置多個,這裡由於開太多虛擬機器比較耗費資源,就只設置了一個。)

機器ip(機器名) http埠(其他埠使用預設值) Agent型別 節點名稱
10.211.55.28 node1 8500 server consul-server1
10.211.55.25 node2 8500 server consul-server2
10.211.55.26 node3 8500 server consul-server3
10.211.55.27 node4 7110 client 帶ui consul-client1

2.先配置好三個Server,並啟動一遍。

consul-server1.json
{
    "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.json

{
    "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.json

{
    "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.json

3.生成並配置agent-token,解決server agent ACL block問題

當上面的語句執行完之後,會發現協調更新由於ACL被阻塞。如下圖:

經過檢視官方文件,發現是由於未生成和配置agent-token導致。

在任意一臺server上執行下面的語句來生成agent-token:

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部分就變為:

"acl":{
        "enabled":true,
        "default_policy":"deny",
        "enable_token_persistence":true,
        "tokens":{
            "master":"cd76a0f7-5535-40cc-8696-073462acc6c7",
             "agent":"deaa315d-98c5-b9f6-6519-4c8f6574a551"
        }
    }

也就是多了agent這個配置。

接著一次重啟各個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已經解決。

4.啟動一個帶ui的client agent

{
    "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

5.配置環境變數。

經過前面一番配置,本以為已經搞定了所有東西,此時只想摸摸自己帥氣的頭髮。 可一執行./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的。

6.給web-ui 設定master_token

在client-agent上,輸入127.0.0.1:7110, 點選ACL, 輸入master-token即可。如下圖:

7.參考文章

https://www.consul.io/docs/acl/acl-legacy.html#bootstrapping-acls https://www.consul.io/docs/agent/options.html https://www.consul.io/docs/commands/index.html#environment-variables