keepalived实现nginx HA的做法,首先参考博文http://quenlang.blog.51cto.com/4813803/1570352,搭建好nginx反向代理tomcat并利用自身模块proxy_cache实现web缓存的架构,该博文中的nginx安 装在hadoop1节点上,按照博文中的nginx的安装配置,配置好hadoop0,这样就有两个节点安装了nginx并实现了web缓存和反向代理。 这里hadoop0作为nginx的master,而hadoop1作为nginx的backup,以此来配置keepalived。
在hadoop0、hadoop1上安装keepalived
yum install keepalived -y
hadoop0上keepalived的配置如下
[root@hadoop0 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script check_nginx { script "/opt/check_nginx.sh" interval 2 } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.207 } track_script { check_nginx } }
hadoop1上keepalived的配置如下
[root@hadoop1 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 52 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.207 } }
从上面的配置看到,keepalived代理nginx实现HA之后的虚拟IP为192.168.0.207,监控nginx的脚本内容如下
[root@hadoop0 ~]# cat /opt/check_nginx.sh #!/bin/bash #author: kora #date: 2014-10-21 #description: check service such as nginx # #set -x CHECK_TIME=3 NGINX_OK=1 NOW=`date` STATUS='OK!' procnum=`ps -ef |grep "/usr/local/nginx/sbin/nginx"|grep -v "grep"|wc -l` while [ "$CHECK_TIME" -ne 0 ] do let "CHECK_TIME-=1" if [ "$procnum" -eq 1 -a -f /usr/local/nginx/logs/nginx.pid ] then NGINX_OK=1 CHECK_TIME=0 else NGINX_OK=0 fi done if [ "$NGINX_OK" -eq 0 ] then status='fail!' /etc/init.d/keepalived stop fi echo "$NOW:$STATUS" >> /usr/local/nginx/logs/keepalived.log exit 0
当master上的nginx挂掉之后,脚本会停调keepalived服务,从而将虚拟IP浮动到hadoop1上,实现高可用,而用户需要使用虚拟IP192.168.0.207来访问web应用。