狼狼 发表于 2018-12-31 11:16:04

keepalived通过vrr_script实现高可用性案例分析

  

  ps -C nginx --no-heading|wc -l
  ps -C java --no-heading|wc -l
  先确认一下服务器上上面两个数字
  

  

  cd/etc/keepalived
https://s1.运维网.com/oss/201711/07/92dde6eeef400da9c58bc7b0b2f8ee5e.gif-wh_500x0-wm_3-wmp_4-s_791742904.gif
  vi/etc/keepalived/check_nginx.sh
  #!/bin/bash
  counter=$(ps -C nginx --no-heading|wc -l)
  if [ "${counter}" = "0" ]; then
     sleep 2
      counter=$(ps -C nginx --no-heading|wc -l)
      if [ "${counter}" = "0" ]; then
        /etc/init.d/keepalived stop
      fi
  fi
https://s1.运维网.com/oss/201711/07/19e09c2a8e1ab56ffd350b139397505c.gif-wh_500x0-wm_3-wmp_4-s_110697487.gif
  vi/etc/keepalived/check_tomcat.sh
  #!/bin/bash
  counter=$(ps -C java --no-heading|wc -l)
  if [ "${counter}" -lt "3" ]; then
     sleep 2
      counter=$(ps -C java --no-heading|wc -l)
      if [ "${counter}" -lt "3" ]; then
        /etc/init.d/keepalived stop
      fi
  fi
https://s1.运维网.com/oss/201711/07/c60ffc41ae61531d9a8ba4d0a1c5f912.gif-wh_500x0-wm_3-wmp_4-s_1226031864.gif
  chmod u+x check_nginx.sh
  chmod u+x check_tomcat.sh
  主 192.168.3.214
  cat /etc/keepalived/keepalived.conf
   vi/etc/keepalived/keepalived.conf
  global_defs {
  notification_email {
  752119102@qq.com
     }
  notification_email_from keepalived@localhost
  smtp_server 127.0.0.1                        
  smtp_connect_timeout 30                     
  router_id LVS_MASTER                        
  }
  vrrp_script chk_nginx {
     script "/etc/keepalived/check_nginx.sh"
      interval 2
      fall 3
  }
  vrrp_script chk_tomcat {
     script "/etc/keepalived/check_tomcat.sh"
      interval 2
      fall 3
  }
  vrrp_instance VI_1 {                     
  #state MASTER
  state BACKUP      
     interface eth1
     track_interface
      {
        eth1
      }      
     virtual_router_id 50   
     priority 150
     nopreempt      
     advert_int 1      
     authentication {   
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {   
         192.168.3.197/24 dev eth1 label eth1:1
     }
  track_script {
         chk_nginx
         chk_tomcat
      }
  }
  从1服务器 192.168.3.215
  global_defs {
  notification_email {
  752119102@qq.com
     }
  notification_email_from keepalive@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_BACKUP
  }
  vrrp_script chk_nginx {
     script "/etc/keepalived/check_nginx.sh"
      interval 2
      fall 3
  }
  vrrp_script chk_tomcat {
     script "/etc/keepalived/check_tomcat.sh"
      interval 2
      fall 3
  }
  vrrp_instance VI_1 {
  state BACKUP
     interface eth0
     track_interface
      {
        eth0
      }   
     virtual_router_id 50
     priority 100
   nopreempt
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {
         192.168.3.197/24 dev eth0 label eth0:1
     }
  track_script {
         chk_nginx
         chk_tomcat
      }
  }
  从2192.168.3.207
  cat   /etc/keepalived/keepalived.conf
  global_defs {
  notification_email {
  752119102@qq.com
     }
  notification_email_from keepalived@localhost
  smtp_server 127.0.0.1                        
  smtp_connect_timeout 30                     
  router_id LVS_MASTER                        
  }
  vrrp_script chk_nginx {
     script "/etc/keepalived/check_nginx.sh"
      interval 2
      fall 3
  }
  vrrp_script chk_tomcat {
     script "/etc/keepalived/check_tomcat.sh"
      interval 2
      fall 3
  }
  vrrp_instance VI_1 {                     
  #state MASTER
  state BACKUP      
     interface eth0
     track_interface
      {
        eth0
      }      
     virtual_router_id 50   
     priority 50
     nopreempt      
     advert_int 1      
     authentication {   
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {   
         192.168.3.197/24 dev eth0 label eth0:1
     }
  track_script {
         chk_nginx
         chk_tomcat
      }
  }
  

  

  




页: [1]
查看完整版本: keepalived通过vrr_script实现高可用性案例分析