0%

consul 实践

在本地虚拟机环境搭建 consul cluster 实践。

实验环境

这三台虚拟机,分别跑在三台笔记本电脑上。这样做的好处是,桥接模式不需要端口映射,在我家的局域网里面尽情的玩耍。

Name Operating system CPU RAM Disk IP
ubuntu18-119 Ubuntu 18.04.5 LTS 2 4G 20G 192.168.1.119
ubuntu18-120 Ubuntu 18.04.5 LTS 2 4G 10G 192.168.1.120
ubuntu18-121 Ubuntu 18.04.5 LTS 2 4G 30G 192.168.1.121

Ubuntu18 是公司当前线上环境的系统,桌面版有可视化,桌面比服务器版本的命令行方便一点。

实验环境的搭建,可以参考我的另外一篇博客 https://feiyang233.club/post/vm/

安装过程

参考文档:

分别在三台虚拟机上安装好 consul service

1
2
3
4
5
6
7
8
9
10
11
12
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

sudo apt-get update && sudo apt-get install consul

## 验证安装的结果
root@ubuntu18-119:/etc/consul.d# cd
root@ubuntu18-119:~# consul version
Consul v1.9.5
Revision 3c1c22679
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

配置文件

在开始配置文件之前,先要生成一个秘钥。 随便在三台中的任意一台机器运行以下命令

1
2
3
4
root@ubuntu18-121:~# consul keygen
byCHv8NFR6p770xmNNjxlCGdeH9e0YU7CwZFnsnOW8w=

# 秘钥很重要,不要随便暴露,这里的秘钥是我临时生成的作示范

node-119

1
2
3
4
5
6
7
8
9
datacenter = "feiyang"
data_dir = "/opt/consul"
client_addr = "0.0.0.0"
bind_addr = "192.168.1.119"
ui = true
server = true
bootstrap_expect=3
encrypt = "byCHv8NFR6p770xmNNjxlCGdeH9e0YU7CwZFnsnOW8w="
retry_join = ["192.168.1.119", "192.168.1.120", "192.168.1.121"]

node-120

1
2
3
4
5
6
7
8
9
datacenter = "feiyang"
data_dir = "/opt/consul"
client_addr = "0.0.0.0"
bind_addr = "192.168.1.120"
ui = true
server = true
bootstrap_expect=3
encrypt = "byCHv8NFR6p770xmNNjxlCGdeH9e0YU7CwZFnsnOW8w="
retry_join = ["192.168.1.119", "192.168.1.120", "192.168.1.121"]

node-121

1
2
3
4
5
6
7
8
9
datacenter = "feiyang"
data_dir = "/opt/consul"
client_addr = "0.0.0.0"
bind_addr = "192.168.1.121"
ui = true
server = true
bootstrap_expect=3
encrypt = "byCHv8NFR6p770xmNNjxlCGdeH9e0YU7CwZFnsnOW8w="
retry_join = ["192.168.1.119", "192.168.1.120", "192.168.1.121"]

启动服务

一切配置好以后,我们可以启动服务,并且设置服务开机自启动

1
2
3
4
5
6
7
8
9
10
11
sudo systemctl start consul

sudo systemctl enable consul

## ---------------------------
## Check cluster members
root@ubuntu18-119:~# consul members
Node Address Status Type Build Protocol DC Segment
ubuntu18-119 192.168.1.119:8301 alive server 1.9.5 2 feiyang <all>
ubuntu18-120 192.168.1.120:8301 alive server 1.9.5 2 feiyang <all>
ubuntu18-121 192.168.1.121:8301 alive server 1.9.5 2 feiyang <all>

ui

任意一台节点的 8500 端口加路径 ui http://node_ip:8500/ui/
举例: http://192.168.1.120:8500/ui/

未完待续