轻量级Keepalived实现Nginx与LVS高可用
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 "
}
notify.sh:
#!/bin/bash
case "$1" in
master)
/etc/init.d/nginx start
exit 0
;;
backup)
/etc/init.d/nginx stop
exit 0
;;
fault)
/etc/init.d/nginx stop
exit 0
;;
*)
echo 'Usage: `basename $0` {master|backup|fault}'
exit 1
;;
esac
实验结果操作:主节点nginx挂了 从节点就升级为主节点 之前的主节点nginx重新开启,就又回复为主节点,这里不做演示。
配置lvs:
lvs段配置文件说明:
virtual_server 192.168.200.100 443 { #vip port
delay_loop 6 #对后端Real Server轮询的时间间隔
lb_algo rr #调度方法
lb_kind NAT #lvs模型
nat_mask 255.255.255.0 #子网掩码
persistence_timeout 50 #连接的持久时长
protocol TCP #协议
real_server 192.168.201.100 443 {
weight 1 #权重
SSL_GET { #检查real server时候用的方法 可以是HTTP_GET
url {
path / #检测健康状况时获取的页面(默认页面)
digest ff20ad2481f97b1754ef3e12ecd3a9cc #期望的返回特征码 也可以是status_code 200
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3 #连接超时时长
nb_get_retry 3 #连接超时后的重试次数
delay_before_retry 3 #重试间隔
}
}
}
具体配置lvs段:
virtual_server 192.168.1.199 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 192.168.1.222 80 {
weight 1
HTTP_GET {
url {
path /demo.php
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.223 80 {
weight 1
HTTP_GET {
url {
path /demo.php
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
分别配置两台real-server:
[root@martin ~]# ip addr add 192.168.1.199/32 label lo:0 brd 192.168.1.199 dev lo
[root@martin ~]# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
[root@martin ~]# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
[root@martin ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@martin ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
实验结果
[root@marvin keepalived]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.199:80 rr
-> 192.168.1.222:80 Route 1 0 0
-> 192.168.1.223:80 Route 1 0 0
[root@sherry keepalived]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.199:80 rr
-> 192.168.1.222:80 Route 1 0 0
-> 192.168.1.223:80 Route 1 0 0