Consul1.5.0 带ACL控制集群搭建

Consul Cluster with ACL
1.机器规划
2.先配置好三个Server,并启动一遍。
3.生成并配置agent-token,解决server agent ACL block问题
4.启动一个带ui的client agent
5.配置环境变量。
6.给web-ui 设置master_token
7.参考文章

这篇文章的目的:搭建带有ACL控制的consul1.5集群。
具体概念及配置说明,后面我会再写文章补充说明。
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"
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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"
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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"
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
可以看到,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
1
2
3
4
5
6
7
8
9
此时会返回生成的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"
}
}
1
2
3
4
5
6
7
8
9
也就是多了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"
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
上面的配置主要是多了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
1
然后,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
1
2
3
4
此时发现在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

posted on 2019-11-21 14:38  ExplorerMan  阅读(245)  评论(0编辑  收藏  举报

导航