Consul1.5.0 带ACL控制集群搭建

栏目: 后端 · 发布时间: 4年前

内容简介:这篇文章的目的:搭建带有ACL控制的consul1.5集群。 具体概念及配置说明,后面我会再写文章补充说明。我这里起了四台虚拟机,三台用作Server agent,一台用作Client agent。(说明:当然Client可以配置多个,这里由于开太多虚拟机比较耗费资源,就只设置了一个。)consul-server1.json

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

1.机器规划

我这里起了四台虚拟机,三台用作Server agent,一台用作Client agent。(说明:当然Client可以配置多个,这里由于开太多虚拟机比较耗费资源,就只设置了一个。)

机器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.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被阻塞。如下图:

Consul1.5.0 带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

Consul1.5.0 带ACL控制集群搭建

将生成的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已经解决。

Consul1.5.0 带ACL控制集群搭建

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, 想看看我这里都有哪些成员,居然发现一个都没有

Consul1.5.0 带ACL控制集群搭建
经过查看官方文档及搜索,发现是没有配置环境变量导致。 1.给三个server的环境变量添加CONSUL_HTTP_TOKEN, vim /etc/profile添加下面一句
export CONSUL_HTTP_TOKEN=cd76a0f7-5535-40cc-8696-073462acc6c7
复制代码

然后,source /etc/profile一下。 为了简单方便,我这里配了最大的权限即master_token 此时发现./consul members已经有数据了

Consul1.5.0 带ACL控制集群搭建
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即可。如下图:

Consul1.5.0 带ACL控制集群搭建

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C Primer Plus(第6版)中文版

C Primer Plus(第6版)中文版

普拉达 (Stephen Prata) / 姜佑 / 人民邮电出版社 / 2016-4-1 / CNY 89.00

《C Primer Plus(第6版)中文版》详细讲解了C语言的基本概念和编程技巧。 《C Primer Plus(第6版)中文版》共17章。第1、2章介绍了C语言编程的预备知识。第3~15章详细讲解了C语言的相关知识,包括数据类型、格式化输入/输出、运算符、表达式、语句、循环、字符输入和输出、函数、数组和指针、字符和字符串函数、内存管理、文件输入输出、结构、位操作等。第16章、17章介绍C......一起来看看 《C Primer Plus(第6版)中文版》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具