centos搭建consul高可用集群
一、概念
consul是spring cloud的服务注册组件
二、单点搭建
下载consol安装包,下载地址为https://releases.hashicorp.com/consul/
将.zip(consul_1.7.2_linux_amd64.zip)文件放置到需要安装的目录下
使用unzip命令解压.zip文件
将解压后的文件(consul)拷贝至指定目录下
使用./consul 命令查看是否安装成功
若出现上方图片中的内容,则说明consul安装成功
使用./consul agent -dev -ui -node=consul-dev -client=0.0.0.0
参数说明:
-dev 表示以开发模式启动,如在生产环境启动,则使用-server
-ui 表示webui的路径,用web来管理consul
-node=consul-dev 表示该服务器节点名
-net=host docker参数, 使得docker容器越过了net namespace的隔离,免去手动指定端口映射的步骤
-advertise 将本机私有IP传递到consul
-bootstrap-expect 指定consul将等待几个节点连通,成为一个完整的集群
-retry-join 指定要加入的consul节点地址,失败会重试, 可多次指定不同的地址
-client consul绑定在哪个client地址上,这个地址提供HTTP、DNS、RPC等服务,默认是127.0.0.1
-bind 该地址用来在集群内部的通讯,集群内的所有节点到地址都必须是可达的,默认是0.0.0.0
allow_stale 设置为true, 表明可以从consul集群的任一server节点获取dns信息, false则表明每次请求都会经过consul server leader
若出现上方图片中的内容,则说明consul启动成功
三、集群搭建
1、集群搭建
IP地址规划
192.168.47.3 consul_server_1
192.168.47.4 consul_server_2
192.168.47.5 consul_server_3
192.168.47.6 consul_client_1
在四台服务器上分别在/opt/spring-cloud-consul目录创建名为consul_config.json的文件,内容如下
192.168.47.3
{
"bootstrap_expect": 1,
"datacenter": "cangzhou_consul",
"data_dir": "/opt/consul-data",
"node_name": "consul_server_1",
"server": true,
"client_addr": "0.0.0.0",
"ui": true,
"bind_addr": "192.168.47.3",
"primary_datacenter": "cangzhou_consul",
"acl": {
"enabled": true,
"default_policy": "allow",
"enable_token_persistence": true,
"tokens": {
"master": "8dc1eb67-1f5f-4e10-ad9d-5e58b047647c"
}
}
}
192.168.47.4
{
"datacenter": "cangzhou_consul",
"data_dir": "/opt/consul-data",
"node_name": "consul_server_2",
"server": true,
"client_addr": "0.0.0.0",
"ui": true,
"bind_addr": "192.168.47.4",
"start_join":["192.168.47.3","192.168.47.4","192.168.47.5"],
"retry_join":["192.168.47.3","192.168.47.4","192.168.47.5"],
"primary_datacenter": "cangzhou_consul",
"acl": {
"enabled": true,
"default_policy": "allow",
"enable_token_persistence": true,
"tokens": {
"master": "8dc1eb67-1f5f-4e10-ad9d-5e58b047647c"
}
}
}
192.168.47.5
{
"datacenter": "cangzhou_consul",
"data_dir": "/opt/consul-data",
"node_name": "consul_server_3",
"server": true,
"client_addr": "0.0.0.0",
"ui": true,
"bind_addr": "192.168.47.5",
"start_join":["192.168.47.3","192.168.47.4","192.168.47.5"],
"retry_join":["192.168.47.3","192.168.47.4","192.168.47.5"],
"primary_datacenter": "cangzhou_consul",
"acl": {
"enabled": true,
"default_policy": "allow",
"enable_token_persistence": true,
"tokens": {
"master": "8dc1eb67-1f5f-4e10-ad9d-5e58b047647c"
}
}
}
192.168.47.6
{
"datacenter": "cangzhou_consul",
"data_dir": "/opt/consul-data",
"node_name": "consul_client_1",
"server": false,
"client_addr": "0.0.0.0",
"ui": true,
"bind_addr": "192.168.47.6",
"start_join":["192.168.47.3","192.168.47.4","192.168.47.5"],
"retry_join":["192.168.47.3","192.168.47.4","192.168.47.5"],
"primary_datacenter": "cangzhou_consul"
}
启动consul
在192.168.47.3、192.168.47.4、192.168.47.5,分别运行指令:consul agent -config-file /opt/spring-cloud-consul/consul_config.json
待这三台都上线后,再在192.168.47.6上运行指令:consul agent -config-file /opt/spring-cloud-consul/consul_config.json
可以看到已有4个nodes
2、配置acl
使用192.168.47.3、192.168.47.4、192.168.47.5的/opt/spring-cloud-consul/consul.config.json文件中的acl.token.master中的内容,登录acl
创建policy(在ui上创建)
用上一步创建的policy创建一个token(在ui上创建)
查看token值(在ui上查看)
拿到token值:2a06e1da-1ead-d783-33c5-845e011a96d4
修改服务端配置文件中的acl配置项,将"tokens"节点新增"agent"值
"acl": {
"enabled": true,
"default_policy": "allow",
"enable_token_persistence": true,
"tokens": {
"master": "8dc1eb67-1f5f-4e10-ad9d-5e58b047647c",
"agent":"2a06e1da-1ead-d783-33c5-845e011a96d4"
}
}
修改客户端配置文件,新增acl配置
"acl":{
"tokens":{
"agent":"2a06e1da-1ead-d783-33c5-845e011a96d4"
}
}
重新启动consul,加载新的配置文件
3、添加服务注册token
没配置ACL之前默认策略为allow,可以任意进行服务注册,配置acl后,可以添加一个用于服务注册的token,某个服务要注册到consul,必须带上这个token
先添加一个policy(在ui上添加)
用上一步创建的policy创建一个token(在ui上创建)
查看token值(在ui上查看)
4、服务注册
在微服务的笔记中讲到