keepalived 高可用方案


Keepalived 高可用lvs

服务器准备:

  node1:  192.168.99.61     centos6.7   (keepalived+httpd)
  node2:  192.168.99.62     centos7.2    (keepalived+httpd)
  node3:  192.168.99.63     centos7.3        httpd服务
  node4:  192.168.99.64     centos7.4        httpd服务
    vip  :  192.168.99.88

node1

    yum -y install keepalived httpd

node2

    yum -y install keepalived httpd

node3

    yum -y install httpd

node4 

    yum -y install httpd

node1 配置keepalived

[[email protected] keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
        [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_mcast_group4 224.0.0.8
}
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 57
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 111222
    }
    virtual_ipaddress {
        192.168.99.88/32 dev eth1 brd 192.168.99.88 label eth1:0
    }
}
virtual_server 192.168.99.88 80 {
    delay_loop 3
    lb_algo rr 
    lb_kind DR
    nat_mask 255.255.255.0
    protocol TCP
    real_server 192.168.99.63 80 {
        weight 1
        HTTP_GET {
            url { 
              path /index.html
              #digest 640205b7b0fc66c1ea91c463fac6334d
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.99.64 80 {
        weight 1
        HTTP_GET {
            url { 
              path /index.html
              #digest 640205b7b0fc66c1ea91c463fac6334d
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

node2 keepalived配置

[[email protected] keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
        [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node2
   vrrp_mcast_group4 224.0.0.8
}
vrrp_instance VI_1 {
    state BACKUP
    interface enp0s8
    virtual_router_id 57
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 111222
    }
    virtual_ipaddress {
        192.168.99.88/32 dev enp0s8 brd 192.168.99.88 label enp0s8:0
    }
}
virtual_server 192.168.99.88 80 {
    delay_loop 3
    lb_algo rr 
    lb_kind DR
    nat_mask 255.255.255.0
    protocol TCP
    real_server 192.168.99.63 80 {
        weight 1
        HTTP_GET {
            url { 
              path /index.html
              #digest 640205b7b0fc66c1ea91c463fac6334d
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.99.64 80 {
        weight 1
        HTTP_GET {
            url { 
              path /index.html
              #digest 640205b7b0fc66c1ea91c463fac6334d
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

node3-node4配置虚拟vip脚本

[[email protected] html]# cat vip.sh 
#!/bin/bash
#
vip=192.168.99.88
mask=255.255.255.255
case $1 in
start)
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ifconfig lo:0 $vip netmask $mask broadcast $vip up
        route add -host $vip dev lo:0
        ;;
stop)
        ifconfig lo:0 down
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ;;
*)
        echo "Usage: $(basename $0) start | stop"
        ;;
esac



Keepalived 高可用httpd服务

node1配置

[[email protected] keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
        [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_mcast_group4 224.0.0.8
}
vrrp_script chk_down {
        script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0 "
        interval 1
        weight -5
}
vrrp_script chk_httpd {
        script "killall -0 httpd && exit 0 || exit 1"
        interval 1
        weight -5
}
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 57
    priority 100
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 111222
    }
    virtual_ipaddress {
        192.168.99.88/32 dev eth1 
    }
    track_script {
        chk_down
        chk_httpd
   } 
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}

node2配置

[[email protected] keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
        [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node2
   vrrp_mcast_group4 224.0.0.8
}
vrrp_script chk_down {
        script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0 "
        interval 1
        weight -5
}
vrrp_script chk_httpd {
        script "killall -0 httpd && exit 0 || exit 1"
        interval 1
        weight -5
}
vrrp_instance VI_1 {
    state BACKUP
    interface enp0s8
    virtual_router_id 57
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 111222
    }
    virtual_ipaddress {
        192.168.99.88/32 dev enp0s8 
    }
    track_script {
        chk_down
        chk_httpd
   }
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}

notify脚本

[[email protected] keepalived]# cat notify.sh 
#!/bin/bash
#
contact="[email protected]"
notify() {
        mailsubject="$(hostname) to be $1, vip floating."
        mailbody="$(date + '%F %T'): vrrp transition, $(hostname) changed to be $1"
        echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
        notify master
        ;;
backup)
        notify backup
        systemctl restart httpd.service
        ;;
fault)
        notify fault
        systemctl restart httpd.service
        ;;
*)
        echo "Usage: $(basename $0) {master|backup|fault}"
        exit 1
        ;;
esac