使用Keepalived+shell脚本线上系统热备一例

因为线上业务比较特殊,因为针对系统进行扩展时,需要从全局的角度来进行规划与部署高可用。以最小的改动,让线上系统使用上高可用。

具体环境介绍:

服务器A : 线上系统JAVA应用。

服务器B : 线上系统服务器A的冷备. 因为业务逻辑相对复杂,有些东西在代码层面写死了,因此成了现在这个情况。在服务器A出现故障时,运维人员需要手工进行启动服务器B,有些参数有可能需要调整,原计划是使用负载均衡,奈何两台服务器不能同时使用。

要让服务器A、B实现自动热备。在服务器A故障时,服务器B自动接管VIP,实现自动切换。我认为这个过程非常的简单.

软件:keepalved + shell

原理:keepalived 实现系统层面VIP的飘移,shell用于实现在VIP飘移后的操作。

面临问题:

1、因为两台服务器,连接两个网络,一个是用于接入,一个是用于连接数据库;再者它本机需要开放端口让其他应相库。所以,在一台服务器的java进程在跑的时候,另外一台java进程最好关掉。

> 这就是使用脚本的原因

2、关于keepalived的心跳,使用用于接入的网卡进行心跳检查。因为如果前面不能接入,后端能连数据库,意义不大了。

> 现实就是这样的

3、java应用层面的监控,如果页面无法打开的话,立即停止keepalived,触发业务切换. 独立脚本实现>

> 通过脚本判断不是问题。

4、在服务器A出现故障时,B接管了业务系统;当A恢复时,不需要重新接管业务系统.

> 已经实现了.

系统维护        A      -----shell------    B 

系统检查        A    -----keepalived-----  B

shell:<以Python启动一个web服务器为例> 

  1. # cat /opt/tomcat.sh
  2. #!/bin/bash 
  3. A=`ps -C python --no-header |wc -l`               
  4. ip addr | grep "10.254.41.200"  > /dev/null 
  5.  
  6. if [ "$?" != "0" ];then 
  7.      if [ "$A" = "1" ];then 
  8.         pkill  python   //直接kill好像不行,最后使用pkil强行关 
  9.      fi 
  10. else 
  11.      if [ "$A" = "0" ];then 
  12.              python -m SimpleHTTPServer 80 
  13.          fi 
  14. fi 

具看我如何实现?
看keepalived配置:
A服务器配置:

  1. ! Configuration File for keepalived 
  2.  
  3. global_defs { 
  4.    notification_email { 
  5.      acassen@firewall.loc 
  6.      failover@firewall.loc 
  7.      sysadmin@firewall.loc 
  8.    } 
  9.    notification_email_from Alexandre.Cassen@firewall.loc 
  10.    smtp_server 192.168.200.1 
  11.    smtp_connect_timeout 30 
  12.    router_id LVS_DEVEL 
  13.  
  14. vrrp_script chk_port { 
  15.     script "/opt/tomcat.sh" 
  16.     interval 2 
  17.     weight 2 
  18.  
  19. vrrp_instance VI_1 { 
  20.     state BACKUP  //使用BACKUP状态 
  21.     interface eth0 
  22.     virtual_router_id 51 
  23.     priority 100 
  24.     mcast_src_ip 10.254.41.173 
  25.     advert_int 1 
  26.     authentication { 
  27.         auth_type PASS 
  28.         auth_pass 1234 
  29.     } 
  30.     track_script { 
  31.     chk_port 
  32.     }     
  33.  
  34.     virtual_ipaddress { 
  35.         10.254.41.200 
  36.     } 

B服务器配置:

  1. ! Configuration File for keepalived 
  2.  
  3. global_defs { 
  4.    notification_email { 
  5.      acassen@firewall.loc 
  6.      failover@firewall.loc 
  7.      sysadmin@firewall.loc 
  8.    } 
  9.    notification_email_from Alexandre.Cassen@firewall.loc 
  10.    smtp_server 192.168.200.1 
  11.    smtp_connect_timeout 30 
  12.    router_id LVS_DEVEL 
  13.  
  14. vrrp_script chk_port {        //自己定义的脚本 
  15.         script "/opt/tomcat.sh"   
  16.         interval 2 
  17.         weight 2 
  18.  
  19. vrrp_instance VI_1 { 
  20.     state BACKUP //使用BACKUP状态 
  21.     interface eth0 
  22.     virtual_router_id 51   
  23.     priority 100 
  24.     mcast_src_ip 10.254.41.110  //使用eth0作为广播源地址 
  25.     advert_int 1 
  26.     authentication { 
  27.         auth_type PASS 
  28.         auth_pass 1234   
  29.     } 
  30.  
  31.     track_script { 
  32.         chk_port 
  33.         } 
  34.  
  35.     virtual_ipaddress { 
  36.         10.254.41.200 
  37.     } 

相关推荐