今天给伙伴们分享一下Keepalived 实战示例(上),希望看了有所收获。
我是公众号「想吃西红柿」「云原生运维实战派」作者,对云原生运维感兴趣,也保持时刻学习,后续会分享工作中用到的运维技术,在运维的路上得到支持和共同进步!
如果伙伴们看了文档觉得有用,欢迎大家关注我的公众号,获取相关文档。爱运维,爱生活。
一、Keepalived 实现 HA
- 注:主/备调度器均能够实现正常调度
1、主/备调度器安装软件
[root@edenluo-lb1 ~]# yum -y install keepalived
2、Keepalived 配置
1、BACKUP1 配置
[root@edenluo-lb1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id director1
}
vrrp_instance VI_1 {
state MASTER
nopreempt
interface eth0
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.152.100
}
}
2、BACKUP2 配置
[root@edenluo-lb2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id director2
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 80
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.152.100
}
}
3、启动KeepAlived(主备均启动)
[root@edenluo-lb1 ~]# systemctl enable keeplived
[root@edenluo-lb1 ~]# systemctl start keeplived
[root@edenluo-lb1 ~]# ip addr
- 到此:可以解决心跳故障 keepalived,不能解决Nginx服务故障
4、扩展对调度器Nginx健康检查(可选)
- 思路:让Keepalived以一定时间间隔执行一个外部脚本,脚本的功能是当Nginx失败,则关闭本机的Keepalived
1、nginx 检测脚本
[root@edenluo-lb1 ~]# cat /etc/keepalived/check_nginx_status.sh
#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then
systemctl stop keepalive
fi
[root@edenluo-lb1 ~]# chmod a+x /etc/keepalived/check_nginx_status.sh
2、keepalived 使用检测脚本
! Configuration File for keepalived
global_defs {
router_id director1
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx_status.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
nopreempt
virtual_router_id 90
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.152.100
}
}
- 注:必须先启动nginx,再启动keepalived
二、Keepalived+LVS_Dr 高可用集群
1、KeepAlived 在该项目中的功能
- 管理IPVS的路由表(包括对RealServer做健康检查)
- 实现调度器的HA
2、获取组播信息
[root@mysql1 ~]# tcpd