Consul1.5.0 带ACL控制集群搭建

这篇文章的目的:搭建带有ACL控制的consul1.5集群。
具体概念及配置说明,后面我会再写文章补充说明。html

1.机器规划

我这里起了四台虚拟机,三台用做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

2.先配置好三个Server,并启动一遍。

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

3.生成并配置agent-token,解决server agent ACL block问题

当上面的语句执行完以后,会发现协调更新因为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已经解决。
在这里插入图片描述

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/ac...
https://www.consul.io/docs/ag...
https://www.consul.io/docs/co...

相关文章
相关标签/搜索