Keepalived.conf解析
三大部分: 全局定义块、VRRP 实例定义块及虚拟服务器定义块
实例剖析
! Configuration File for keepalived global_defs { notification_email { lisongtao@ihuilian.com #acassen@firewall.loc #failover@firewall.loc #sysadmin@firewall.loc } notification_email_from sa@ihuilian.com smtp_server smtp.exmail.qq.com smtp_connect_timeout 30 router_id LVS_DEVEL ##自定义的字符串 } ###在哪里找到自定义配置规范 #vrrp_script chk_mantaince_down { #定义可以手动控制状态的脚本 # script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" vrrp_script chk_nginx_down { #定义可以手动控制状态的脚本 #script "killall -0 nginx" #script "killall -0 nginx && exit 0 || exit 1" script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" interval 1 #检查时间间隔 weight - 13 #如果检测失败,优先级-13 } vrrp_instance VI_1 { state MASTER ###MASTER/BACKUP必须大写,且当MASTER恢复时,会自动恢复为MASTER状态 interface eth0 virtual_router_id 51 ###同一个vrrp使用相同的vrrp,整个vrrp内唯一 priority 100 ###数字越大优先级越高,且要MASTER要高于SLAVE,和sentinel恰好相反...>O< advert_int 1 ###Timeout时长秒为单位 authentication { ###MASTER和SLAVE密码相同方可正常通信 auth_type pass auth_pass huilian } virtual_ipaddress { ###每个地址占一行,不能指定子网掩码,与lvs客户端设定的VIP一致 192.168.65.200 #192.168.200.17 #192.168.200.18 } ###跟global_defs中定义的vrrp_script chk_mantaince_down对应 track_script { #引用自定义的脚本 chk_nginx_down } #notify_master "/etc/keepalived/notify.sh master" #notify_backup "/etc/keepalived/notify.sh backup" #notify_fault "/etc/keepalived/notify.sh fault" } #virtual_server 192.168.65.200 36379{ ###IP和vrrp_instance中定义的vip需一致,IP PORT virtual_server 192.168.65.200 80{ ###IP和vrrp_instance中定义的vip需一致,IP PORT delay_loop 6 ###健康检查时间/秒 #lb_algo wlc ###负载均衡调度算法,常用rr/wlc lb_algo rr ###负载均衡调度算法,常用rr/wlc lb_kind DR ###负载均衡转发规则, DR,NAT,TUN3 nat_mask 255.255.255.0 #persistence_timeout 50 ###会话保持时长 protocol TCP ###协议类型转发 #virtualhost www.a.com real_server 192.168.65.128 80{ ###real server IP PORT weight 33 ###数值越大,权重越大 TCP_CHECK { connect_timeout 1 #表示3秒无响应,则超时 nb_get_retry 3 #表示重试次数 delay_before_retry 3 #表示重试间隔 } } real_server 192.168.65.129 80{ weight 22 } real_server 192.168.65.130 80{ weight 11 } }
自定义脚本功能测试
配置keepalived.conf
###分发keepalived.conf到slave
# ansible keepalived -m copy -a 'src=/etc/keepalived/keepalived.conf dest=/etc/keepalived'
修改优化级和状态
! Configuration File for keepalived global_defs { notification_email { lisongtao@ihuilian.com #acassen@firewall.loc #failover@firewall.loc #sysadmin@firewall.loc } notification_email_from sa@ihuilian.com smtp_server smtp.exmail.qq.com smtp_connect_timeout 30 router_id LVS_DEVEL ##自定义的字符串 } ###在哪里找到自定义配置规范 #vrrp_script chk_mantaince_down { #定义可以手动控制状态的脚本 # script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" vrrp_script chk_nginx_down { #定义可以手动控制状态的脚本 #script "killall -0 nginx" #script "killall -0 nginx && exit 0 || exit 1" script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" interval 1 #检查时间间隔 weight - 13 #如果检测失败,优先级-13 } vrrp_instance VI_1 { state SLAVE ###MASTER/BACKUP必须大写,且当MASTER恢复时,会自动恢复为MASTER状态 interface eth1 virtual_router_id 51 ###同一个vrrp使用相同的vrrp,整个vrrp内唯一 priority 99 ###数字越大优先级越高,且要MASTER要高于SLAVE,和sentinel恰好相反...>O< advert_int 1 ###Timeout时长秒为单位 authentication { ###MASTER和SLAVE密码相同方可正常通信 auth_type pass auth_pass huilian } virtual_ipaddress { ###每个地址占一行,不能指定子网掩码,与lvs客户端设定的VIP一致 192.168.65.200 #192.168.200.17 #192.168.200.18 } ###跟global_defs中定义的vrrp_script chk_mantaince_down对应 track_script { #引用定义的脚本 chk_nginx_down } #notify_master "/etc/keepalived/notify.sh master" #notify_backup "/etc/keepalived/notify.sh backup" #notify_fault "/etc/keepalived/notify.sh fault" } #virtual_server 192.168.65.200 36379{ ###IP和vrrp_instance中定义的vip需一致,IP PORT virtual_server 192.168.65.200 80{ ###IP和vrrp_instance中定义的vip需一致,IP PORT delay_loop 6 ###健康检查时间/秒 #lb_algo wlc ###负载均衡调度算法,常用rr/wlc lb_algo rr ###负载均衡调度算法,常用rr/wlc lb_kind DR ###负载均衡转发规则, DR,NAT,TUN3 nat_mask 255.255.255.0 #persistence_timeout 50 ###会话保持时长 protocol TCP ###协议类型转发 #virtualhost www.a.com real_server 192.168.65.128 80{ ###real server IP PORT weight 33 ###数值越大,权重越大 TCP_CHECK { connect_timeout 1 #表示3秒无响应,则超时 nb_get_retry 3 #表示重试次数 delay_before_retry 3 #表示重试间隔 } } real_server 192.168.65.129 80{ weight 22 } real_server 192.168.65.130 80{ weight 11 } }
重启slave和master的keepalived
# ansible keepalived -m command -a "service keepalived restart"
Touch down文件测试
/var/log/message日志信息
Jan 27 19:40:03 Mrds Keepalived_vrrp[6580]: VRRP_Script(chk_nginx_down) failed Jan 27 19:40:03 Mrds Keepalived_vrrp[6580]: VRRP_Instance(VI_1) Entering FAULT STATE Jan 27 19:40:03 Mrds Keepalived_vrrp[6580]: VRRP_Instance(VI_1) Now in FAULT state
VIP信息
Rm down文件测试
/var/log/message日志信息
Jan 27 19:52:29 Mrds Keepalived_vrrp[6580]: VRRP_Script(chk_nginx_down) succeeded Jan 27 19:52:29 Mrds Keepalived_vrrp[6580]: VRRP_Instance(VI_1) prio is higher than received advert Jan 27 19:52:29 Mrds Keepalived_vrrp[6580]: VRRP_Instance(VI_1) Transition to MASTER STATE Jan 27 19:52:29 Mrds Keepalived_vrrp[6580]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election Jan 27 19:52:30 Mrds Keepalived_vrrp[6580]: VRRP_Instance(VI_1) Entering MASTER STATE
VIP信息
改造为检测nginx进程
修改master配置
Stop nginx进程
/var/log/message日志信息 serJan 27 20:06:05 Mrds Keepalived_healthcheckers[9992]: TCP connection to [192.168.65.128]:80 failed !!! Jan 27 20:06:05 Mrds Keepalived_healthcheckers[9992]: Removing service [192.168.65.128]:80 from VS [192.168.65.200]:80 Jan 27 20:06:05 Mrds Keepalived_healthcheckers[9992]: Remote SMTP server [0.0.0.0]:25 connected. Jan 27 20:06:05 Mrds Keepalived_vrrp[9993]: VRRP_Script(chk_nginx_down) failed Jan 27 20:06:06 Mrds Keepalived_healthcheckers[9992]: SMTP alert successfully sent. Jan 27 20:06:06 Mrds Keepalived_vrrp[9993]: VRRP_Instance(VI_1) Entering FAULT STATE Jan 27 20:06:06 Mrds Keepalived_vrrp[9993]: VRRP_Instance(VI_1) Now in FAULT state VIP信息 VIP资源被抢占 |
Start nginx进程
/var/log/message日志信息 Jan 27 20:08:55 Mrds Keepalived_vrrp[9993]: VRRP_Script(chk_nginx_down) succeeded Jan 27 20:08:56 Mrds Keepalived_vrrp[9993]: Kernel is reporting: interface eth0 UP Jan 27 20:08:56 Mrds Keepalived_vrrp[9993]: VRRP_Instance(VI_1) Transition to MASTER STATE Jan 27 20:08:57 Mrds Keepalived_vrrp[9993]: VRRP_Instance(VI_1) Entering MASTER STATE Jan 27 20:08:59 Mrds Keepalived_healthcheckers[9992]: TCP connection to [192.168.65.128]:80 success. Jan 27 20:08:59 Mrds Keepalived_healthcheckers[9992]: Adding service [192.168.65.128]:80 to VS [192.168.65.200]:80 Jan 27 20:08:59 Mrds Keepalived_healthcheckers[9992]: Remote SMTP server [0.0.0.0]:25 connected. Jan 27 20:09:00 Mrds Keepalived_healthcheckers[9992]: SMTP alert successfully sent. VIP信息 因MASTER优化级最高,VIP资源被抢占回来 |
MASTER和1台slave异常,最后一台slave服务器是否还正常
但会发现lvs池是异常的~~~ 129的nginx应该是异常的.但lvs池没有被剔除
关闭slave的keepalived尝试
关闭Srds1上的keepalived后,master被正常切换到Srds2上,看来slave和master的功能还是有一定的区别的。
Master可以自动发现自己后端服务的状态,并时刻保持lvs池的最新,但slave是没有这个功能或者没有这个权限的!!。
看来要想完全实现后端服务的高可用要其它方式来实现这个功能的。
原创文章,作者:stanley,如若转载,请注明出处:http://www.178linux.com/3206