keepalive主从搭建
准备两台主机
10.1.1.127
10.1.1.128
两台主机都配置
rocky主机ip配置

# 1. 关闭防火墙并禁用开机自启
systemctl stop firewalld && systemctl disable firewalld
# 2. 临时关闭SELINUX(重启失效),永久关闭(重启生效)
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 3. 关闭NetworkManager(避免网络配置冲突,CentOS7必做)
systemctl stop NetworkManager && systemctl disable NetworkManager
# 4. 安装基础依赖(后续安装需要)
yum install -y wget net-tools gcc pcre-devel zlib-devel openssl-devel
# 5. 重启网络(确保配置生效)
systemctl restart network
安装nginx
yum install -y nginx
10.1.1.127主机nginx页面修改

10.1.1.128主机nginx页面修改

安装keepalive
Master节点安装
yum install -y keepalived
验证

# 备份原有配置文件(避免出错)
添加配置
[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
# router id, global unique, host uuid
router_id keep_17 #唯一标识,主备不一致
}
# vrrp conf, keepalived + nginx
vrrp_instance VI_1 {
# host -> master
state MASTER #主备不一致,主是master
# host interface name
interface ens160 #根据主机的网卡实际名称填写
# ensure master and backup are same
virtual_router_id 51 #主备一致
# weight, the larger, more chance to be master after master down
priority 100 #优先级,主的大
# between m & b time interval, default 1s
advert_int 1
# authenticate pwd, default
authentication {
auth_type PASS
auth_pass 1111 #密码,主备一致
}
# vip conf
virtual_ipaddress {
10.1.1.129 #虚拟ip,主备一致
}
}
Back节点配置
[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
# router id, global unique, host uuid
router_id keep_18
}
# vrrp conf, keepalived + nginx
vrrp_instance VI_1 {
# host -> master
state BACKUP
# host interface name
interface ens160
# ensure master and backup are same
virtual_router_id 51
# weight, the larger, more chance to be master after master down
priority 80
# between m & b time interval, default 1s
advert_int 1
# authenticate pwd, default
authentication {
auth_type PASS
auth_pass 1111
}
# vip conf
virtual_ipaddress {
10.1.1.129
}
}
验证虚拟ip

停掉10.1.1.127的keepalive

访问虚拟ip10.1.1129,跳转到backup


浙公网安备 33010602011771号