//安装所需环境
yum -y install openssl-devel gcc libnl libnl-devel
//下载安装包
wget https://www.keepalived.org/software/keepalived-2.2.8.tar.gz
//解压
tar -zxvf keepalived-2.2.8.tar.gz
//编译
./configure --prefix=/usr/local/keepalived --sysconf=/etc
make &&make install
//查看网卡名,我的网卡是ens33
ip addr
//添加配置
vi /etc/keepalived/keepalived.conf
global_defs {
#路由id,全局唯一
router_id keep_130
}
vrrp_instance VI_1 {
#MASTER或者BACKUP
state MASTER
#绑定的网卡
interface ens33
#保证主备节点一致
virtual_router_id 51
#权重,优先级越高,master挂掉后就能优先成为新的master
priority 100
#主备之间同步检查间隔时间s
advert_int 1
#认证时的密码
authentication {
auth_type PASS
auth_pass SGs2pGV3VKTk
}
#虚拟VIP
virtual_ipaddress {
192.168.159.250
}
}
//启动keepalived
/usr/local/keepalived/sbin/keepalived
systemctl enable keepalived.service
//查看进程
ps -ef | grep keepalived
测试:三台机器(一主两备) 都安装好了nginx,index.html都添加上机器序号
不断访问虚拟ip,只会请求到当前的master节点
手动关闭130号机器后再次访问,备用机器成为处理节点