高可用集群搭建,避免单点故障
1. 安装
# 更新软件源中的所有软件列表
sudo apt update
#安装Keepalived
sudo apt install keepalived -y
2. 配置
2.1 Master主机配置
# 复制原本的keepalived.conf
cp /usr/share/doc/keepalived/samples/keepalived.conf.sample /etc/keepalived/keepalived.conf
#修改配置文件为:
global_defs {
# 当前网络中唯一
router_id lb01
}
vrrp_instance VI_1 {
# 指定角色 当前MASTER
state MASTER
# 绑定网卡 需要改动到当前服务器的虚拟网卡
interface enp0s3
# 虚拟路由编号 主从一致
virtual_router_id 51
nopreempt
# 优先级 数值越大优先级越高
priority 100
# 检查间隔 1秒
advert_int 1
virtual_ipaddress {
# 虚拟ip (VIP)
192.168.1.116 dev enp0s3 lable enp0s3:3
}
}
2.2 BackUP主机配置
# 复制原本的keepalived.conf
cp /usr/share/doc/keepalived/samples/keepalived.conf.sample /etc/keepalived/keepalived.conf
#修改配置文件为:
global_defs {
# 当前网络中唯一
router_id lb02
}
vrrp_instance VI_1 {
# 指定角色 当前BACKUP
state BACKUP
# 绑定网卡 需要改动到当前服务器的虚拟网卡
interface enp0s3
# 虚拟路由编号 主从一致
virtual_router_id 51
nopreempt
# 优先级 从比主少50
priority 50
# 检查间隔 1秒
advert_int 1
virtual_ipaddress {
# 虚拟ip (VIP)
192.168.1.116 dev enp0s3 lable enp0s3:3
}
}
3. 主从服务启动
systemctl start keepalived
# 关闭防火墙
sudo ufw disable
4. 测试
4.1 内网ping 虚拟ip
4.2 查看Master ip 配置 并关闭Master 服务器再试
root@ubuntu20-04:/etc/keepalived# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:3a:6b:07 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.111/24 brd 192.168.1.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.1.116/32 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe3a:6b07/64 scope link
valid_lft forever preferred_lft forever
# 发现虚拟ip 在当前Master服务器上 然后关机
root@ubuntu20-04:/etc/keepalived# sudo shutdown now
root@ubuntu20-04:/etc/keepalived# Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(192.168.1.111) at 20:24:44.
Type `help' to learn how to use Xshell prompt.
4.3 尝试 ping 192.168.1.116 虚拟ip
4.4 查看BackUP服务器 ip 配置
root@ubuntu20-04BACKUP:/etc/keepalived# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:fc:a6:28 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.112/24 brd 192.168.1.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.1.116/32 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fefc:a628/64 scope link
valid_lft forever preferred_lft forever
虚拟 ip 成功漂移到BackUP服务器