keepalived 配置 dns 单点恢复


1.vim /etc/keepalived/keepalived.conf


vrrp_script chk_dns {

   script "/usr/local/sbin/check_dns.sh"  #在这里添加脚本链接

   interval 3       #脚本执行间隔

   weight -10     # 修改权重

}

vrrp_instance VI_1 {

    state BACKUP    # 备用用 BACKUP

    interface eth2   #用的是哪个网卡

    virtual_router_id 88  #虚拟路由地址必须一样,才能配置成一个组

    priority 100   # 备用用 90

    advert_int 1  #MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒

    nopreempt

    authentication {

        auth_type PASS  #(密码形式)

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.32.88

    }

    track_script {

        chk_dns     #添加脚本执行

    }

    notify_master "/bin/bash -x /usr/local/sbin/modify_master.sh 2>&1 | tee /tmp/modimaster.txt"

    notify_backup "/bin/bash -x /usr/local/sbin/modify_master.sh 2>&1 | tee /tmp/modibackup.txt"

}


virtual_server 192.168.32.88 53 {

    delay_loop 6    #每隔6s 查询 realserver 状态

    lb_algo rr    #lvs 算法

    lb_kind NAT     #Direct Route

    persistence_timeout 0    #会话保持时间,单位是秒(可以适当延长时间以保持session)

    protocol TCP     # 用 TCP协议检查 realserver 状态

    real_server 192.168.32.85 53 {

        weight 1

        notify_down "/bin/sh -x /usr/local/sbin/keepalived.sh 2>&1 | tee /tmp/tmp.txt"

          TCP_CHECK {

              connect_timeout 10   #10s 无响应超时

               nb_get_retry 3

               delay_before_retry 3

               connect_port 53

          }

    }

}



2.vim /usr/local/sbin/check_dns.sh


#!/bin/bash


ll=`ping -c 3 www.zjtest8.com`


if [ $? -eq 1  ]; then

        service keepalived restart

        /usr/bin/killall -9 named

        /usr/local/named/sbin/named -c /usr/local/named/etc/named.conf

        exit 0

else

        exit 1

fi


vim /usr/local/sbin/keepalived.sh


#!/bin/bash


service keepalived restart


killall -9 named


/usr/local/named/sbin/named -c /usr/local/named/etc/named.conf


vim /usr/local/sbin/modify_master.sh


#!/bin/bash


killall -9 named


/usr/local/named/sbin/named -c /usr/local/named/etc/named.conf