keepalived中vrrp_script,track_script,notify的使用方法

可以在keepalived.conf文件中定义的脚本,用以实现某个检测功能;

例:检测/etc/keepalived目录下down文件是否存在,如果存在则优先级减20,如果不存在表示正常

vrrp_script chk {

  script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"

  interval 1

  weight -20

注:这个脚本的作用是用于维护MASTER,使MASTER手动下线

如何调用上面定义的脚本呢?

  在vrrp实例中(vrrp_instance VI_1)加上track_script用于追踪脚本

  track_script {

    chk

  }

notify的用法:

  notify_master:当当前节点成为master时,通知脚本执行任务(一般用于启动某服务,比如nginx,haproxy等)

  notify_backup:当当前节点成为backup时,通知脚本执行任务(一般用于关闭某服务,比如nginx,haproxy等)

  notify_fault:当当前节点出现故障,执行的任务; 

  例:当成为master时启动haproxy,当成为backup时关闭haproxy

  notify_master "/etc/keepalived/start_haproxy.sh start"

  notify_backup "/etc/keepalived/start_haproxy.sh stop"

一个完整的实例:

  MASTER:初始priority为100

  BACKUP:初始priority为90

  模拟MASTER产生故障:

  当检测到/etc/keepalived目录下有down文件时,priority减少20,变为80;低于BACKUP的priority;

  此时MASTER变成BACKUP,同时执行notify_backup的脚本文件(关闭haproxy);

  同时BACKUP变成MASTER,同时执行notify_master的脚本文件(启动haproxy);

  模拟MASTER故障恢复:

  当删除/etc/keepalived目录下的down文件时,原MASTER的优先级又变为100,高于原BACKUP的priority;

  此时原MASTER由BACKUP又抢占成了MASTER,同时执行notify_master的脚本文件(启动haproxy);

  同时原BACKUP由MASTER又变了BACKUP,同时执行notify_backup的脚本文件(关闭haproxy);

MASTER的配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

global_defs {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk {

   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"

   interval 1

   weight -20

}

vrrp_instance VI_1 {

    state MASTER

    interface eth1

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        10.0.22.245

    }

    track_script {

      chk

    }

    notify_master "/etc/keepalived/start_haproxy.sh start"

    notify_backup "/etc/keepalived/start_haproxy.sh stop"

BACKUP的配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

global_defs {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

  

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

       10.0.22.245

    }

    notify_master "/etc/keepalived/start_haproxy.sh start"

    notify_backup "/etc/keepalived/start_haproxy.sh stop"

  

}

start_haproxy.sh的脚本内容:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#!/bin/bash

case "$1" in

  start)

    /etc/init.d/haproxy start

  ;;

  stop)

    /etc/init.d/haproxy stop

  ;;

  restart)

    /etc/init.d/haproxy stop

    /etc/init.d/haproxy start

  *)

    echo "Usage:$0 start|stop|restart"

  ;;

esac

keepalived检测nginx,当nginx服务不正常时自动降级,当nginx恢复时自动升级:

check_nginx.sh脚本

1

2

3

4

5

#!/bin/bash

nmap localhost -p 80 | grep "80/tcp open"

if [ $? -ne 0 ];then

        exit 10

fi

notify.sh脚本:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

#!/bin/bash

VIP=$2

sendmail (){

        subject="${VIP}'s server keepalived state is translate"

        content="`date +'%F %T'`: `hostname`'s state change to master"

        echo $content | mail -s "$subject" zhengwei.liu@staples.cn

}

case "$1" in

  master)

        nmap localhost -p 80 | grep "80/tcp open"

        if [ $? -ne 0 ];then

                /etc/init.d/nginx start

        fi

        sendmail

  ;;

  backup)

        nginx_psr=`ps -C nginx --no-header | wc -l`

        if [ $nginx_psr -ne 0 ];then

                /etc/init.d/nginx stop

        fi

  ;;

  *)

        echo "Usage:$0 master|backup VIP"

  ;;

esac

MASTER配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

! Configuration File for keepalived

 

global_defs {

    notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id https

}

vrrp_script chk_nginx {

    script "/etc/keepalived/check_nginx.sh"

    interval 1

    weight -20

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 54

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

    172.16.8.19/25

    }

    track_script {

    chk_nginx

    }

    notify_master "/etc/keepalived/notify.sh master 172.16.8.19"

    notify_backup "/etc/keepalived/notify.sh backup 172.16.8.19"

}

BACKUP配置:

backup无需检测nginx是否正常,默认nginx是未启动的,当升级为MASTER时启动nginx,当降级为BACKUP时关闭

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

! Configuration File for keepalived

 

global_defs {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id https

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 54

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

    172.16.8.19/25

    }

    notify_master "/etc/keepalived/notify.sh master 172.16.8.19"

    notify_backup "/etc/keepalived/notify.sh backup 172.16.8.19"

}

 

出处:http://blog.51cto.com/liuzhengwei521/1929589

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
keepalived ,"vrrp_script" 和 "track_script" 的用法有一些区别。 "vrrp_script" 的用法如下: ``` vrrp_script <script_name> { script <path_to_script> interval <check_interval> weight <weight> fall <fall_threshold> rise <rise_threshold> } ``` 其,各个参数的含义如下: - `<script_name>`: 脚本的名称,可以是任意字符串,用于标识该脚本。 - `<path_to_script>`: 脚本的路径,可以是相对或绝对路径,指定要运行的脚本。 - `<check_interval>`: 检查脚本的时间间隔,单位为秒,默认为 2 秒。 - `<weight>`: 脚本的权重,用于计算服务的权重和优先级,默认为 1。 - `<fall_threshold>`: 连续检测到故障的阈值,达到该阈值后,将认为服务出现故障,默认为 1。 - `<rise_threshold>`: 连续检测到服务恢复的阈值,达到该阈值后,将认为服务已经恢复正常,默认为 1。 "track_script" 的用法如下: ``` track_script { <script_name> } ``` 其, `<script_name>` 是要监控的脚本的名称,必须与 "vrrp_script" 指定的脚本名称相同。在配置文件指定 "track_script" 时,不需要再指定脚本路径、时间间隔、权重、阈值等参数,这些参数已经在 "vrrp_script" 指定过了。 因此,"vrrp_script" 和 "track_script" 的主要区别在于用法,"vrrp_script" 需要指定脚本路径、时间间隔、权重、阈值等参数,"track_script" 只需要指定要监控的脚本名称即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值