keepalived 安装
准备两台虚拟机 server1=192.168.11.134,server2=192.168.11.135 ,安装contos7 ,关闭防火墙,关闭selinux,配置阿里yum源
vip=192.168.11.100
selver1
安装软件
[root@server1 ~]# yum -y install keepalived
打开配置文件
[root@server1 ~]# vim /etc/keepalived/keepalived.conf
!Configuration File for keepalived
global_defs {
router_id 1 #设备在租中的标识,设备不一样即可
}
#vrrp_script chk_nginx { #健康检查
#scrip "/etc/keepalived/ch_ng.sh" #检查脚本
#interval 2 #检查频率.秒
#weight -5 #priority减5
#fall 3 #失败三次
#}
vrrp_instance VI_1 { #VI_1.实例名两台路由器相同。
state MASTER #注或从状态
interface ens32 #监控网卡
mcast_src_ip 192.168.11.134 #心跳源ip
virtual_router_id 55 #虚拟路由编号,主备要一致
priority 100 #优先级
advert_int 1 #心跳间隔
authentication { #密钥认证(1-8位)
auth_type PASS
auth_pass 123456
}
virtual_ipaddress { #VIP
192.168.11.100/24
}
#track_script { #引用脚本
#chk_nginx
#}
}
启动服务
[root@server1 ~]# systemctl start keepalived
安装Nginx
[root@server1 ~]# yum -y install nginx
[root@server1 ~]# systemctl start nginx
[root@server1 ~]# vim /usr/share/nginx/html/index.html
133 <h1>Welcome to 111111 CentOS</h1>
测试
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wio8Cg7r-1655380851424)(C:\Users\yue’yue\AppData\Roaming\Typora\typora-user-images\image-20220616190118519.png)]
selver2
!Configuration File for keepalived
global_defs {
router_id 2
}
!Configuration File for keepalived
global_defs{
router_id 2
}
#vrrp_script chk_nginx{
#scrip "/etc/keepalived/ch_ng.sh"
#interval 2
#weight -5
#fall 3
#}
vrrp_instance VI_1{
state BACKUP
interface ens32
mcast_src_ip 192.168.11.135
virtual_router_id 55
priority 99
advert_int 1
authentication{
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.11.100/24
}
#track_script {
#chk_nginx
#}
}
[root@server2 ~]# yum -y install nginx
[root@server2 ~]# systemctl start nginx
[root@server2 ~]# vim /usr/share/nginx/html/index.html
133 <h1>Welcome to 222222222 CentOS</h1>
[root@server2 ~]# systemctl start keepalived.service
测试
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Mx4HQaLS-1655380851425)(C:\Users\yue’yue\AppData\Roaming\Typora\typora-user-images\image-20220616191143498.png)]
最后检查启动systemctl start keepalived
程序运行日志tail -f /var/log/messages
使用vip地址访问网页
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UGuPw27b-1655380851425)(C:\Users\yue’yue\AppData\Roaming\Typora\typora-user-images\image-20220616195632900.png)]
模拟selver1出现故障
再次访问vip网页
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KJT0RvRC-1655380851426)(C:\Users\yue’yue\AppData\Roaming\Typora\typora-user-images\image-20220616195950500.png)]
这里为了看到效果,两个网页故意写成不同。