我这里只说配置tomcat的高可用的步骤,不再讲解原理以及执行流程方面的知识, 关于keepalived的原理以及执行流程,请参考我的其他文章。
机器:
ip | 操作系统版本 | 备注 |
10.253.12.10 | CentOS Linux release 7.0 (Core) | 将被配置为MASTER |
10.253.12.30 | CentOS Linux release 7.0 (Core) | 将被配置为BACKUP |
约定:
为了简便,下文10.253.12.10我会简称10,10.253.12.30我会简称为30。
一、10上的keepalived的配置文件 vi /etc/keepalived/keepalived.conf:
! Configuration File for keepalived global_defs { router_id zaspark02 #当前机器标识 } vrrp_script monitor_tomcat { script "/etc/keepalived/script/tomcat_monitor.sh" interval 3 } vrrp_instance VI_1 { state MASTER #主MASTER 从BACKUP interface eth0 #ifconfig 得到,机器不同可能不同 virtual_router_id 51 priority 100 #1-254 ,从需要小于主 #如果机器不支持组播,需要使用单播模式开始 unicast_src_ip 10.253.12.10 unicast_peer { 10.253.12.30 } #如果机器不支持组播,需要使用单播模式结束 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.253.12.22 } track_script { monitor_tomcat } notify_backup "/etc/keepalived/script/tomcat_backup.sh" notify_master "/etc/keepalived/script/tomcat_master.sh" }
二、30上的/etc/keepalived/keepalived.conf:
! Configuration File for keepalived global_defs { router_id zaspark01 #当前机器标识 } vrrp_script monitor_tomcat { script "/etc/keepalived/script/tomcat_monitor.sh" interval 3 } vrrp_instance VI_1 { state BACKUP #主MASTER 从BACKUP interface eth0 #ifconfig 得到,机器不同可能不同 virtual_router_id 51 priority 90 #1-254 ,从需要小于主 #如果机器不支持组播,需要使用单播模式开始 unicast_src_ip 10.253.12.30 unicast_peer { 10.253.12.10 } #如果机器不支持组播,需要使用单播模式结束 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.253.12.22 } track_script { monitor_tomcat } notify_backup "/etc/keepalived/script/tomcat_backup.sh" notify_master "/etc/keepalived/script/tomcat_master.sh" }
三、tomcat_monitor.sh:
#!/bin/bash . /etc/profile . ~/.bashrc logdate=`date "+%Y-%m-%d %H:%M:%S"` logfile="/etc/keepalived/script/tomcat_monitor.log" #根据安装情况修改 curl http://localhost:8080 if [ "$?" = "0" ];then echo "$logdate tomcat running">>$logfile exit 0 else echo "$logdate tomcat not running,stop keepalived start...">>$logfile service keepalived stop echo "stop keepalived end">>$logfile echo "make sure kill tomcat start...">>$logfile ps -ef|grep -v grep|grep tomcat|awk '{print $2}'|xargs kill -9 echo "kill -9 tomcat end">>$logfile exit 1 fi
四、tomcat_master.sh:
#!/bin/bash . /etc/profile . ~/.bashrc logdate=`date "+%Y-%m-%d %H:%M:%S"` logfile="/etc/keepalived/script/tomcat_master.log" #根据安装情况修改 curl http://localhost:8080 if [ "$?" = "0" ];then echo "$logdate tomcat running">>$logfile exit 0 else echo "$logdate tomcat not running">>$logfile echo "start tomcat...">>$logfile #根据安装情况修改 sh /usr/local/tomcat/apache-tomcat-7.0.77/bin/startup.sh echo "started tomcat">>$logfile exit 1 fi
五、tomcat_backup.sh:
#!/bin/bash . /etc/profile . ~/.bashrc logdate=`date "+%Y-%m-%d %H:%M:%S"` logfile="/etc/keepalived/script/tomcat_backup.log" #根据安装情况修改 curl http://localhost:8080 if [ "$?" = "0" ];then echo "$logdate tomcat running">>$logfile exit 0 else echo "$logdate tomcat not running">>$logfile echo "start tomcat...">>$logfile #根据安装情况修改 sh /usr/local/tomcat/apache-tomcat-7.0.77/bin/startup.sh echo "started tomcat">>$logfile exit 1 fi
六、创建目录:
mkdir -p /etc/keepalived/script
七、把这3个脚本放到/etc/keepalived/script目录下以供调用
八、进入到脚本所在目录:
cd /etc/keepalived/script
九、赋予可执行的权限:
chmod 777 tomcat_*.sh
十、分别启动和停止10和30上的tomcat与keepalived服务,查看tomcat_monitor.log等日志的变化以及keepalived的日志/var/log/messages是否符合预期,并使用提供的虚拟ip(这里是10.253.12.22)访问tomcat验证是否可以访问。