主:(主备配置只有两处,优先级&状态)

[root@node7_1 keepalived]# cat keepalived.conf

! Configuration File for keepalived


global_defs {

   notification_email {

     root@localhost

   }

   notification_email_from keepalived@localhost

   smtp_server 172.0.0.1

   smtp_connect_timeout 30

   router_id node1

   vrrp_mcast_group4 224.0.100.19

}


vrrp_instance VI_1 {

    state MASTER

    interface eno16777728

    virtual_router_id 1

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass cb77b8da

    }

    virtual_ipaddress {

        192.168.1.251/32 dev eno16777728  #此处写DR模型中的VIP地址

    }

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

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

    notify_fault "/etc/keepalived/notify.sh fault"

}


virtual_server 192.168.1.251 80 { #此处写DR模型中的VIP地址

    delay_loop 3

    lb_algo rr

    lb_kind DR

    protocol TCP


    real_server 192.168.1.161 80 {

        weight 1

        HTTP_GET {

            url {

              path /

              status_code 200

            }

            connect_timeout 1

            nb_get_retry 3

            delay_before_retry 1

        }

    }


    real_server 192.168.1.162 80 {

        weight 1

        HTTP_GET {

            url {

              path /

              status_code 200

            }

            connect_timeout 1

            nb_get_retry 3

            delay_before_retry 1

        }

    }

}


备:

[root@node7_1 keepalived]# cat keepalived.conf

! Configuration File for keepalived


global_defs {

   notification_email {

     root@localhost

   }

   notification_email_from keepalived@localhost

   smtp_server 172.0.0.1

   smtp_connect_timeout 30

   router_id node1

   vrrp_mcast_group4 224.0.100.19

}


vrrp_instance VI_1 {

    state BACKUP 

    interface eno16777728

    virtual_router_id 1 

    priority 10

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass cb77b8da

    }

    virtual_ipaddress {

        192.168.1.251/32 dev eno16777728  #此处写DR模型中的VIP地址

    }

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

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

    notify_fault "/etc/keepalived/notify.sh fault"

}


virtual_server 192.168.1.251 80 { #此处写DR模型中的VIP地址

    delay_loop 3

    lb_algo rr

    lb_kind DR

    protocol TCP


    real_server 192.168.1.161 80 {

        weight 1

        HTTP_GET {

            url {

              path /

              status_code 200

            }

            connect_timeout 1

            nb_get_retry 3

            delay_before_retry 1

        }

    }


    real_server 192.168.1.162 80 {

        weight 1

        HTTP_GET {

            url {

              path /

              status_code 200

            }

            connect_timeout 1

            nb_get_retry 3

            delay_before_retry 1

        }

    }

}


通知脚本:

[root@node7_1 keepalived]# cat notify.sh 

#!/bin/bash


vip=192.168.1.251

contact='root@localhost'


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

}


case "$1" in

    master)

        notify master

       # /etc/rc.d/init.d/haproxy start

        exit 0

    ;;

    backup)

        notify backup

       # /etc/rc.d/init.d/haproxy stop

        exit 0

    ;;

    fault)

        notify fault

      #  /etc/rc.d/init.d/haproxy stop

        exit 0

    ;;

    *)

        echo 'Usage: `basename $0` {master|backup|fault}'

        exit 1

    ;;

esac