手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux 高可用(HA)集群之Keepalived详解

[日期:2013-08-22] 来源:Linux社区  作者:freeloda [字体: ]

(4).测试
master:
[root@master keepalived]# touch down #新建一下down文件
[root@master keepalived]# ll 
总用量 4   
-rw-r--r-- 1 root root    0 8月  22 13:39 down   
-rw-r--r-- 1 root root 1317 8月  22 13:35 keepalived.conf
[root@master keepalived]# tail -f /var/log/messages #查看一下日志 
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Entering MASTER STATE   
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) setting protocol VIPs.   
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.18.200   
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Received higher prio advert   
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Entering BACKUP STATE   
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) removing protocol VIPs.   
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: Netlink reflector reports IP 192.168.18.200 added   
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: Netlink reflector reports IP 192.168.18.200 removed   
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: SMTP alert successfully sent.   
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: SMTP alert successfully sent.   
^C   
[root@master keepalived]# ip add show #查看VIP   
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN   
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00   
    inet 127.0.0.1/8 scope host lo   
    inet6 ::1/128 scope host   
      valid_lft forever preferred_lft forever   
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000   
    link/ether 00:0c:29:4b:a1:85 brd ff:ff:ff:ff:ff:ff   
    inet 192.168.18.208/24 brd 192.168.18.255 scope global eth0   
    inet6 fe80::20c:29ff:fe4b:a185/64 scope link   
      valid_lft forever preferred_lft forever

slave:
123456789101112 [root@slave ~]# ip addr show #查看一下VIP已转移到slave上 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN   
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00   
    inet 127.0.0.1/8 scope host lo   
    inet6 ::1/128 scope host   
      valid_lft forever preferred_lft forever   
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000   
    link/ether 00:0c:29:f9:e6:26 brd ff:ff:ff:ff:ff:ff   
    inet 192.168.18.207/24 brd 192.168.18.255 scope global eth0   
    inet 192.168.18.200/32 scope global eth0   
    inet6 fe80::20c:29ff:fef9:e626/64 scope link   
      valid_lft forever preferred_lft forever

好了,自写监测脚本,完成维护模式切换,到这里就演示成功,下面我们来解决最后一个问题,就是keepalived主从切换的邮件通告。
11.如何在keepalived故障时(或主备切换时),发送警告邮件给指定的管理员?
(1).keepalived通知脚本进阶示例
下面的脚本可以接受选项,其中
-s, --service SERVICE,...:指定服务脚本名称,当状态切换时可自动启动、重启或关闭此服务;

-a, --address VIP: 指定相关虚拟路由器的VIP地址;

-m, --mode {mm|mb}:指定虚拟路由的模型,mm表示主主,mb表示主备;它们表示相对于同一种服务而方,其VIP的工作类型;

-n, --notify {master|backup|fault}:指定通知的类型,即vrrp角色切换的目标角色;

-h, --help:获取脚本的使用帮助;

#!/bin/bash 
# Author: freeloda   
# description: An example of notify script   
# Usage: notify.sh -m|--mode {mm|mb} -s|--service SERVICE1,... -a|--address VIP  -n|--notify {master|backup|falut} -h|--help
contact='1521076067@163.com'
helpflag=0   
serviceflag=0   
modeflag=0   
addressflag=0   
notifyflag=0
Usage() { 
  echo "Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP>  <-n|--notify {master|backup|falut}>"   
  echo "Usage: notify.sh -h|--help" 
}
ParseOptions() { 
  local I=1;   
  if [ $# -gt 0 ]; then   
    while [ $I -le $# ]; do   
      case $1 in 
      -s|--service)   
        [ $# -lt 2 ] && return 3   
        serviceflag=1   
        services=(`echo $2|awk -F"," '{for(i=1;i<=NF;i++) print $i}'`)   
        shift 2 ;;   
      -h|--help)   
        helpflag=1   
        return 0   
        shift 
        ;;   
      -a|--address)   
        [ $# -lt 2 ] && return 3   
        addressflag=1   
        vip=$2   
        shift 2   
        ;;   
      -m|--mode)   
        [ $# -lt 2 ] && return 3   
        mode=$2   
        shift 2   
        ;;   
      -n|--notify)   
        [ $# -lt 2 ] && return 3   
        notifyflag=1   
        notify=$2   
        shift 2   
        ;;   
      *)   
        echo "Wrong options..." 
        Usage   
        return 7   
        ;;   
      esac 
    done 
    return 0   
  fi 
}
#workspace=$(dirname $0)
RestartService() { 
  if [ ${#@} -gt 0 ]; then   
    for I in $@; do 
      if [ -x /etc/rc.d/init.d/$I ]; then 
        /etc/rc.d/init.d/$I restart   
      else 
        echo "$I is not a valid service..." 
      fi 
    done 
  fi 
}
StopService() { 
  if [ ${#@} -gt 0 ]; then   
    for I in $@; do 
      if [ -x /etc/rc.d/init.d/$I ]; then 
        /etc/rc.d/init.d/$I stop   
      else 
        echo "$I is not a valid service..." 
      fi 
    done 
  fi 
}
Notify() {   
    mailsubject="`hostname` to be $1: $vip floating" 
    mailbody="`date '+%F %H:%M:%S'`, vrrp transition, `hostname` changed to be $1." 
    echo $mailbody | mail -s "$mailsubject" $contact   
}
# Main Function   
ParseOptions $@   
[ $? -ne 0 ] && Usage && exit 5
[ $helpflag -eq 1 ] && Usage && exit 0
if [ $addressflag -ne 1 -o $notifyflag -ne 1 ]; then 
  Usage   
  exit 2   
fi
mode=${mode:-mb}
case $notify in 
'master')   
  if [ $serviceflag -eq 1 ]; then 
      RestartService ${services[*]}   
  fi 
  Notify master   
  ;;   
'backup')   
  if [ $serviceflag -eq 1 ]; then 
    if [ "$mode" == 'mb' ]; then 
      StopService ${services[*]}   
    else 
      RestartService ${services[*]}   
    fi 
  fi 
  Notify backup   
  ;;   
'fault')   
  Notify fault   
  ;;   
*)   
  Usage   
  exit 4   
  ;;   
esac

(2).在keepalived.conf配置文件中,其调用方法如下所示:
notify_master "/etc/keepalived/notify.sh -n master -a 192.168.18.200" 

notify_backup "/etc/keepalived/notify.sh -n backup -a 192.168.18.200" 

notify_fault "/etc/keepalived/notify.sh -n fault -a 192.168.18.200" 

(3).修改配置文件
master:
[root@master keepalived]# cat keepalived.conf   
! Configuration File for keepalived
global_defs { 
  notification_email {   
    15251076067@163.com   
  }   
  notification_email_from root   
  smtp_server 127.0.0.1   
  smtp_connect_timeout 30   
  router_id LVS_DEVEL   
}
vrrp_script chk_schedown { 
  script "[ -e /etc/keepalived/down ] && exit 1 || exit 0" 
  interval 1   
  weight -5   
  fall 2   
  rise 1   
}
vrrp_instance VI_1 { 
    state MASTER   
    interface eth0   
    virtual_router_id 51   
    priority 101   
    advert_int 1   
    authentication {   
        auth_type PASS   
        auth_pass 1111   
    }   
    virtual_ipaddress {   
        192.168.18.200   
    }   
    track_script {   
        chk_schedown   
    }
    #增加以下三行 
    notify_master "/etc/keepalived/notify.sh -n master -a 192.168.18.200" 
    notify_backup "/etc/keepalived/notify.sh -n backup -a 192.168.18.200"
    notify_fault "/etc/keepalived/notify.sh -n fault -a 192.168.18.200"
}
virtual_server 192.168.18.200 80 { 
    delay_loop 6   
    lb_algo rr   
    lb_kind DR   
    nat_mask 255.255.255.0   
    protocol TCP
    real_server 192.168.18.201 80 { 
        weight 1   
        HTTP_GET {   
            url {   
              path /   
          status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
    real_server 192.168.18.202 80 {   
        weight 1   
        HTTP_GET {   
            url {   
              path /   
              status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
  sorry_server 127.0.0.1 80   
}

slave:
[root@slave keepalived]# cat keepalived.conf   
! Configuration File for keepalived
global_defs { 
  notification_email {   
    15251076067@163.com   
  }   
  notification_email_from root   
  smtp_server 127.0.0.1   
  smtp_connect_timeout 30   
  router_id LVS_DEVEL   
}
vrrp_script chk_schedown { 
  script "[ -e /etc/keepalived/down ] && exit 1 || exit 0" 
  interval 1   
  weight -5   
  fall 2   
  rise 1   
}
vrrp_instance VI_1 { 
    state BACKUP   
    interface eth0   
    virtual_router_id 51   
    priority 100   
    advert_int 1   
    authentication {   
        auth_type PASS   
        auth_pass 1111   
    }   
    virtual_ipaddress {   
        192.168.18.200   
    }   
    track_script {   
    chk_schedown   
    }
    #增加以下三行 
    notify_master "/etc/keepalived/notify.sh -n master -a 192.168.18.200"
    notify_backup "/etc/keepalived/notify.sh -n backup -a 192.168.18.200" 
    notify_fault "/etc/keepalived/notify.sh -n fault -a 192.168.18.200" 
}
virtual_server 192.168.18.200 80 { 
    delay_loop 6   
    lb_algo rr   
    lb_kind DR   
    nat_mask 255.255.255.0   
    protocol TCP
    real_server 192.168.18.201 80 { 
        weight 1   
        HTTP_GET {   
            url {   
              path /   
          status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
    real_server 192.168.18.202 80 {   
        weight 1   
        HTTP_GET {   
            url {   
              path /   
              status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
    sorry_server 127.0.0.1 80   
}

linux
相关资讯       keepalived  HA集群  Linux高可用 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款