环境准备:
ssh shell工具: 远程连接
三个CentOS示例: 部署集群
配置好各个实例之间的网络访问,以及ssh免密登录。
下载&上传:
1、下载 Consul:
Download Consul - Consul by HashiCorp
2、上传至虚拟机:
➜ Desktop scp consul_1.1.0_linux_amd64.zip root@192.168.1.11:/opt
➜ Desktop scp consul_1.1.0_linux_amd64.zip root@192.168.1.12:/opt
➜ Desktop scp consul_1.1.0_linux_amd64.zip root@192.168.1.13:/opt
解压&拷贝
在其他几台机器上重复此命步骤:
[root@centos-1 ~]# yum install unzip -y
[root@centos-1 ~]# cd /opt
[root@centos-1 opt]# ls
consul_1.1.0_linux_amd64.zip
[root@centos-1 opt]# unzip consul_1.1.0_linux_amd64.zip
Archive: consul_1.1.0_linux_amd64.zip
inflating: consul
[root@centos-1 opt]# ls
consul consul_1.1.0_linux_amd64.zip
[root@centos-1 opt]# mv consul /usr/bin/
[root@centos-1 bin]# cd /usr/bin/ && ls consul
consul
启动
[root@centos-1 ~]# consul agent -server -data-dir=/tmp/consul -node=server-1 \
-bind=192.168.1.11 -bootstrap-expect 3 -client=0.0.0.0 -ui &
[root@centos-2 ~]# consul agent -server -data-dir=/tmp/consul -node=server-2 \
-bind=192.168.1.12 -join=192.168.1.11 -client=0.0.0.0 -ui &
[root@centos-3 ~]# consul agent -server -data-dir=/tmp/consul -node=server-3 \
-bind=192.168.1.13 -join=192.168.1.11 -client=0.0.0.0 -ui &
选项详解:
选项 | 说明 |
---|---|
consul anent: |
该命令会启动一个consulAnent |
-server |
表示该 agent是一个 serverAgent,不添加这个选项则表示是一个 clientAgent |
-data-dir |
表示相关数据存储的目录位置,在 serverAgent上该命令所指示的目录下会存储一些集群的状态信息 |
-node |
指定该 agent 节点的名称,该名称在集群众必须是唯一的 |
-bind |
指定 agent 的 Ip |
-bootstrap-expect |
该命令通知 consul 准备加入集群的节点个数 |
-client 0.0.0.0 -ui |
启动 consulUI,如不添加该指令,则 UI 只能在当前机器上访问 |
-dc |
指定该 agent 加入到哪一个数据中心,默认为 dc1 |
join |
将该节点加入集群中 |
验证
验证集群是否搭建成功,执行consul members
,在任意的一台机器上:
[root@centos-1 ~]# consul members
Node Address Status Type Build Protocol DC Segment
server-1 192.168.1.11:8301 alive server 1.1.0 2 dc1 <all>
server-2 192.168.1.12:8301 alive server 1.1.0 2 dc1 <all>
server-3 192.168.1.13:8301 alive server 1.1.0 2 dc1 <all>