wxin 发表于 2018-12-29 13:36:43

KEEPALIVED的具体实现

具体过程是用Nginx做负载均衡,可以将Nginx和主机放在同一台机子上,也可以分开放置,只不过分开的话要指明RS是Nginx的主机地址。至于直接将虚拟地址配置在Nginx主机上我暂时还未成功
下面附上我的双主模型的主keepalived服务器的配置
! Configuration File for keepalived
global_defs {
   notification_email {
    root@localhost
    vrrp_mcast_group4224.0.11.100
   }
   notification_email_from kaadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id sjf1
}
vrrp_scriptchk_mantaince{
    script "[[-e /etc/keepalived/down]]&& exit 1 || exit 0"
    interval 1
    weight-4      }
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 15
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 11111234
    }
    virtual_ipaddress {      11.1.0.11/16 dev eth1   
    }
    track_script {
    chk_mantaince
    }
   notify_master   "/etc/keepalived/inotify.shmaster"
   notify_backup   "/etc/keepalived/inotify.shbackup"
   notify_fault    "/etc/keepalived/inotify.shfault"}
virtual_server 11.1.0.11 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR#persistence_timeout 50
    protocol TCP
    sorry_server 11.1.0.20180
    real_server 11.1.0.20380 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
    real_server 11.1.0.20480 {
         weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 16
    priority 97
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {      11.1.0.22/16 dev eth1   
    }
    track_script {
    chk_mantaince
    }
   notify_master   "/etc/keepalived/inotify.shmaster"
   notify_backup   "/etc/keepalived/inotify.shbackup"
   notify_fault    "/etc/keepalived/inotify.shfault"}
virtual_server 11.1.0.22 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR#persistence_timeout 50
    protocol TCP
    sorry_server 11.1.0.20180
    real_server 11.1.0.20580 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
    real_server 11.1.0.20680 {
         weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
}下面附上我的从keepalived的配置
! Configuration File for keepalived
global_defs {
   notification_email {
    root@localhost
    vrrp_mcast_group4224.0.11.100
   }
   notification_email_from kaadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id sjf2
}
vrrp_scriptchk_mantaince{
    script "[[-e /etc/keepalived/down]]&& exit 1 || exit 0"
    interval 1
    weight-4      }
vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 15
    priority 98
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 11111234
    }
    virtual_ipaddress {      11.1.0.11/16 dev eth1   
    }
    track_script {
    chk_mantaince
    }
   notify_master   "/etc/keepalived/inotify.shmaster"
   notify_backup   "/etc/keepalived/inotify.shbackup"
   notify_fault    "/etc/keepalived/inotify.shfault"}
virtual_server 11.1.0.11 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR#persistence_timeout 50
    protocol TCP
    sorry_server 11.1.0.20180
    real_server 11.1.0.20380 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
    real_server 11.1.0.20480 {
         weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth1
    virtual_router_id 16
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {      11.1.0.22/16 dev eth1   
    }
    track_script {
    chk_mantaince
    }
   notify_master   "/etc/keepalived/inotify.shmaster"
   notify_backup   "/etc/keepalived/inotify.shbackup"
   notify_fault    "/etc/keepalived/inotify.shfault"}
virtual_server 11.1.0.22 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR#persistence_timeout 50
    protocol TCP
    sorry_server 11.1.0.20180
    real_server 11.1.0.20580 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
    real_server 11.1.0.20680 {
         weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
}下面在附上我的设置虚拟网络的脚本
#!/bin/bashvip='11.1.0.11'vport='80'netmask='255.255.255.255'iface='lo:0'case $1 instart)      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 $iface$vipnetmask $netmaskbroadcast $vip up
      route add -host $vip dev $iface
      ;;
stop)
      ifconfig $iface 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
esac下面附上我的跟踪脚本
#!/bin/bash#auth sjf#date 2016.11.1vip=192.16.11.99contact='root@localhost'notify() {
      mailsubject="`hostname` to be $1 ;@vip floating "
      mailbody="`date   +'%F %H:%M%S'`;vrrp transited , `hostname ` changed to be $1    "
      echo $mailbody | mail -s "$mailsubject"$contact}case "$1" inmaster)
      notify master      exit 0
      ;;
backup)
      notifybackup      exit 0
      ;;
fault)
      notify fault      exit 0
      ;;
*)      echo 'Usage: `basename $0` {maser|backup|fault} '
      exit 1esac  




页: [1]
查看完整版本: KEEPALIVED的具体实现