consul v1.9.1 集群搭建

496 阅读1分钟

1 环境准备

Centos7/CPU:2核+/内存:8G+/磁盘:50G+

1. 1 机器环境

节点hostname作用IP
cmastermaster192.168.8.124
cnode1node1192.168.8.125
cnode2node2192.168.8.126

1.2 hostname

[root@base1 ~]# hostnamectl set-hostname cmaster --static
[root@base2 ~]# hostnamectl set-hostname cnode1 --static
[root@base3 ~]# hostnamectl set-hostname cnode2 --static

1.3 网络设置

[root@base1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="static" #dhcp改为static 
ONBOOT="yes" #开机启用本配置
IPADDR=192.168.8.124 #静态IP 192.168.8.125/192.168.8.126
GATEWAY=192.168.8.2 #默认网关
NETMASK=255.255.255.0 #子网掩码
DNS1=114.114.114.114 #DNS 配置
DNS2=8.8.8.8 #DNS 配置

$# reboot

1.4 查看主机名

hostname

1.5 配置IP host映射关系

echo "192.168.8.124 cmaster.local.com" >> /etc/hosts
echo "192.168.8.125 cnode1.local.com" >> /etc/hosts
echo "192.168.8.126 cnode2.local.com" >> /etc/hosts

1.6 安装依赖

$ yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstatlibseccomp wget vim net-tools git iproute lrzsz bash-completion tree bridge-utils unzip bind-utils gcc

2 安装Consul

2.1 下载二进制包

$ wget https://releases.hashicorp.com/consul/1.9.1/consul_1.9.1_linux_amd64.zip
$ unzip consul_1.9.1_linux_amd64.zip -d /usr/local/bin

2.2 环境配置

$ vi /etc/profile
$ export CONSUL_HOME=/usr/local/bin/consul
$ export PATH=$PATH:CONSUL_HOME

// 使用环境变量配置生效
$ source /etc/profile

2.3 验证安装结果

$ consul --version
Consul v1.9.1
Revision ca5c38943
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

2.4 防火墙

$ systemctl stop firewalld && systemctl disable firewalld
# 置空iptables
$ yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save

至此为止,可以关闭cmaster,然后进行克隆后设置下1.2,1.3

3 搭建Consul集群

3.1 建立Master

$ consul agent -server -ui -bootstrap-expect=3 -data-dir=/data/consul -node=agent-1 -client=0.0.0.0 -log-file=/usr/service/log/consul_log-$(date +%Y-%m-%d--%H-%M) -bind=192.168.8.124 -datacenter=dc1

3.2 Node1

$ consul agent -server -ui -bootstrap-expect=3 -data-dir=/data/consul -node=agent-2 -client=0.0.0.0 -log-file=/usr/service/log/consul_log-$(date +%Y-%m-%d--%H-%M) -bind=192.168.8.125 -datacenter=dc1 -join 192.168.8.124

3.3 Node2

$ consul agent -server -ui -bootstrap-expect=3 -data-dir=/data/consul -node=agent-3 -client=0.0.0.0 -log-file=/usr/service/log/consul_log-$(date +%Y-%m-%d--%H-%M) -bind=192.168.8.126 -datacenter=dc1 -join 192.168.8.124

3.4 查看启动结果

$ consul members
Node     Address             Status  Type    Build  Protocol  DC   Segment
agent-1  192.168.8.124:8301  alive   server  1.9.1  2         dc1  <all>
agent-2  192.168.8.125:8301  alive   server  1.9.1  2         dc1  <all>
agent-3  192.168.8.126:8301  alive   server  1.9.1  2         dc1  <all>

3.5 停止agent服务

[root@cnode2 ~]# consul leave
Graceful leave complete
$ consul members
Node     Address             Status  Type    Build  Protocol  DC   Segment
agent-1  192.168.8.124:8301  alive   server  1.9.1  2         dc1  <all>
agent-2  192.168.8.125:8301  alive   server  1.9.1  2         dc1  <all>
agent-3  192.168.8.126:8301  left    server  1.9.1  2         dc1  <all>

4 访问Consul的WebUI控制台

如果想和Kubernetes集成,官方建议采用Helm方式