apache+tomcat+keepalived jsp网站高可用(主备切换)

 1、 测试环境

服务器:
master:centos 5.5 x86_64 10.10.0.38
backup: centos 5.5 x86_64 10.10.0.39
vip: 10.10.0.40
 
软件:
httpd-2.2.6.tar.gz 
jdk-6u4-linux-x64.bin
apache-tomcat-6.0.36.tar.gz
tomcat-connectors-1.2.37-src
keepalived-1.1.15.tar.gz
 
2、整合apche tomcat(省略、见jsp网站部署)
echo "10.10.0.38" > apache主目录/ROOT/index.jsp
echo "10.10.0.39" > apache主目录/ROOT/index.jsp
以上测试主备切换时用到
 
3、安装keepalived(主、备机上)
#tar zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure --prefix=/usr/local/keepalived
#make 
#make install
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
 
4、配置keepalived
1)主服务器:master
#mkdir /etc/keepalived
#vim /etc/keepalived/keepalived.conf
 
! Configuration File for keepalived
 
global_defs {
   notification_email {
     [email protected] #邮件通知地址
   }
    notification_email_from [email protected]
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}
 
vrrp_script chk_http_port { 
                script "/etc/keepalived/http.sh"         #监控脚本 
             #   script "/etc/keepalived/http1.sh" 
                interval 2                      #监控时间 
                weight 2                               
 
vrrp_instance VI_1 {
    state MASTER    #设置为主服务器
    interface eth0  #监测网络接口
    virtual_router_id 51  #主、备必须一样
    mcast_src_ip 10.10.0.38   #(多播的源IP,设置为本机外网IP,与VIP同一网卡)此项可不设置
    priority 100   #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)
    advert_int 1   #VRRP Multicast广播周期秒数
    authentication {
        auth_type PASS  #VRRP认证方式,主备必须一致
        auth_pass 1111   #(密码)
    }
 
 track_script { 
                chk_http_port   #调用http进程监控脚本
        } 
 
    virtual_ipaddress {
        10.10.0.40  #VRRP HA虚拟地址
    }
 
2)备份服务器:BACKUP
只需要将主服务器配置做如下修改即可:
state  BACKUP
priority 90
 
5、编写监控脚本(主备机)
#vim /etc/keepalived/http.sh (监控apache)
 
#!/bin/sh
 
A=`ps -C httpd --no-header |wc -l`               
if [ $A -eq 0 ];then                                        
                /usr/local/apache/bin/apachectl  start
                sleep 3
                if [ `ps -C httpd --no-header |wc -l` -eq 0 ];then
                       killall keepalived                       
                fi
fi
 
 
B=`ps aux | grep tomcat |wc -l`                
if [ $B -eq 0 ];then                                       
               /usr/local/tomcat/bin/startup.sh
               sleep 3
                if [ `ps aux | grep tomcat |wc -l` -eq 0 ];then
                       killall keepalived                       
                fi
fi
 
编写监控脚本的原因:
当apache或tomcat服务不能正常运行,而keepalived或机器正常,则主备不会自动切换。
 
6、启动服务
分别在主备机上启动apache tomcat nginx
#/usr/local/apache/bin/apachectl start
#/usr/locat/tomcat/bin/startup.sh
#service keepalived start
 
主服务器上keepalived日志:
#tail -f /var/log/message
Dec 21 14:49:09 monthplan Keepalived: Starting Keepalived v1.1.15 (12/21,2012) 
Dec 21 14:49:09 monthplan Keepalived_vrrp: Using MII-BMSR NIC polling thread...
Dec 21 14:49:09 monthplan Keepalived_vrrp: Registering Kernel netlink reflector
Dec 21 14:49:09 monthplan Keepalived_vrrp: Registering Kernel netlink command channel
Dec 21 14:49:09 monthplan Keepalived_vrrp: Registering gratutious ARP shared channel
Dec 21 14:49:09 monthplan Keepalived: Starting VRRP child process, pid=1301
Dec 21 14:49:09 monthplan Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'. 
Dec 21 14:49:09 monthplan Keepalived_vrrp: Configuration is using : 65382 Bytes
Dec 21 14:49:09 monthplan Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]
Dec 21 14:49:09 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 21 14:49:10 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 21 14:49:10 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Dec 21 14:49:10 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.0.40
Dec 21 14:49:10 monthplan avahi-daemon[1829]: Registering new address record for 10.10.0.40 on eth0.
Dec 21 14:49:11 monthplan Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded
Dec 21 14:49:15 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.0.40
 
备服务器上keepalived日志:
#tail -f /var/log/message
Dec 21 14:50:20 monthplan Keepalived: Starting Keepalived v1.1.15 (12/21,2012) 
Dec 21 14:50:20 monthplan Keepalived_vrrp: Using MII-BMSR NIC polling thread...
Dec 21 14:50:20 monthplan Keepalived_vrrp: Registering Kernel netlink reflector
Dec 21 14:50:20 monthplan Keepalived_vrrp: Registering Kernel netlink command channel
Dec 21 14:50:20 monthplan Keepalived_vrrp: Registering gratutious ARP shared channel
Dec 21 14:50:20 monthplan Keepalived: Starting VRRP child process, pid=22788
Dec 21 14:50:20 monthplan Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'. 
Dec 21 14:50:20 monthplan Keepalived_vrrp: Configuration is using : 65256 Bytes
Dec 21 14:50:20 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Dec 21 14:50:20 monthplan Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]
Dec 21 14:50:22 monthplan Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded
 
从日志中可以看出主备服务正常启动
 
7、测试
 1)停掉主服务器keepalived 
主日志:
Dec 21 14:53:42 monthplan Keepalived: Stopping Keepalived v1.1.15 (12/21,2012) 
Dec 21 14:53:42 monthplan Keepalived_vrrp: Terminating VRRP child process on signal
Dec 21 14:53:42 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Dec 21 14:53:42 monthplan avahi-daemon[1829]: Withdrawing address record for 10.10.0.40 on eth0.
 
备日志:
Dec 21 14:53:46 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 21 14:53:47 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 21 14:53:47 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Dec 21 14:53:47 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.0.40
Dec 21 14:53:47 monthplan avahi-daemon[1812]: Registering new address record for 10.10.0.40 on eth0.
Dec 21 14:53:52 monthplan Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.0.40
 
备份服务器已成功接管vip:10.10.0.40,同时访问网页也可以看出。
 
 2)关闭任何服务器apache进程
日志:
Dec 21 14:59:41 monthplan Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded
apache进程自动启动
 
 3)掉主服务器,备份服务器成功接管vip
 
 
 
 
 
 
 
 
 

你可能感兴趣的:(apache,tomcat,jsp,高可用,KeepAlive)