标签:keepalived
keepalive路由软件是由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
服务器环境:
主机 IP1:172.16.178.138
主机 IP2:172.16.178.136
vip: 172.16.178.137
安装keepalived:
yum install keepalived -y
安装nginx
yum install nginx -y
配置/etc/keepalived/keepalived.conf
ip1:
! Configuration File for keepalived global_defs { notification_email { root@localhost //收件人地址 } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 //服务器地址 smtp_connect_timeout 30 //链接延迟时间 router_id LVS_DEVEL } vrrp_script chk_nginx { script " pidof nginx " //查看ngixn是否开启 interval 1 //每隔一秒查看一次 weight -3 //权重 与优先级相加做比较 要注意默认值,否则降级时候会出错 } 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 { 172.16.178.137 //vip } 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" }
ip2:
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from Alexandre.Cassen@firewall.loc 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 } 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 { 172.16.178.137 } 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
测试:
[root@marco1 keepalived]# /etc/init.d/keepalived start Starting keepalived: [ OK ] [root@marco1 keepalived]# curl http://172.16.178.137 from IP1 [root@marco1 keepalived]# killall nginx [root@marco1 keepalived]# curl http://172.16.178.137 from IP2 [root@marco1 keepalived]# /etc/init.d/nginx start Starting nginx: [ OK ] [root@marco1 keepalived]# curl http://172.16.178.137 from IP1
本文出自 “marco” 博客,请务必保留此出处http://9173436.blog.51cto.com/9163436/1770598
标签:keepalived
原文地址:http://9173436.blog.51cto.com/9163436/1770598