keepalived路由软件是由C语言编写,为loadbalance Linux系统提供简单、健壮的高可用性,keepalive根据loadbalance服务器池的健康实现动态检查,自适应维护和管理。另一方面,高可 用性是通过VRRP协议。VRRP是路由器的基本砖故障转移。此外,keepalive实现一组钩子VRRP有限状态机提供低级和高速协议交互。 keepalive框架都可以单独使用或一起提供弹性的基础设施。
VRRP是一个“选举”协议,它能够动态地将一个虚拟路由器的责任指定至同一个VRRP组中的其它路由器上,从而消除了静态路由配置的单点故障,被指向的路由器即为主路由器(MASTER),其余的为备用路由器(BACKUP)。
VRRP的优势:
冗余:可以使用多个路由器设备作为LAN客户端的默认网关,大大降低了默认网关成为单点故障的可能性;
负载共享:允许来自LAN客户端的流量由多个路由器设备所共享;
多VRRP组:在一个路由器物理接口上可配置多达255个VRRP组;
多IP地址:基于接口别名在同一个物理接口上配置多个IP地址,从而支持在同一个物理接口上接入多个子网;
抢占:在master故障时允许优先级更高的backup成为master;
通告协议:使用IANA所指定的组播地址224.0.0.18进行VRRP通告;
VRRP追踪:基于接口状态来改变其VRRP优先级来确定最佳的VRRP路由器成为master;
上图大概意思就是通过watchdog定时器 检测checkers module和vrrp module有没有定时在写入数据,如果监测到一方停止写入,那么将这台服务器从master切到backup
Netlink:网络
ipvs:ipvs的规则,以及指向的real-server
checkers:通过tcp http ssl等protocol 检查各服务器运行的进程
VRRP Stack:vrrp protocol
安装
[root@marvin ~]# yum install keepalived
配置:
[root@marvin keepalived]# man keepalived.conf
配置文件说明
[root@marvin ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost #发件人
}
notification_email_from keepalived@localhost #收件人
smtp_server 127.0.0.1 #发件服务器
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 { #实例名称
state MASTER #通过startup 成为master 其他节点只能是backup
interface eth0 #通告进行所需 接口
virtual_router_id 51 #虚拟路由id ,也是决定vmac的指数 同一个实例,路由标识相同, 一般不大于255
priority 100 #初始优先级
advert_int 1 #一个VRRP实例中各节点之间同步的时间间隔
authentication {
auth_type PASS #明文认证
auth_pass 1111 #随机密码
}
virtual_ipaddress {
192.168.1.199 #vip
}
}
配置nginx:
master.conf
----------------------------------------
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "pidof nginx "
interval 1
weight -3
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.199
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master "
notify_backup "/etc/keepalived/notify.sh backup "
notify_fault "/etc/keepalived/notify.sh fault "
}
backup.conf
----------------------------------------
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "pidof nginx "
interval 1
weight -1
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.199
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master "
notify_backup "/etc/keepalived/notify.sh backup "
notify_fault "/etc/keepalived/notify.sh fault "
}