KEEPALIVED 의 구체 적 인 실현

7350 단어
구체 적 인 과정 은 Nginx 로 부하 균형 을 맞 추 는 것 입 니 다. Nginx 와 호스트 를 같은 기기 에 놓 을 수도 있 고 따로 놓 을 수도 있 습 니 다. 다만 분리 하면 RS 가 Nginx 의 호스트 주 소 를 가 리 켜 야 합 니 다.Nginx 호스트 에 가상 주 소 를 직접 설정 하 는 것 은 아직 성공 하지 못 했 습 니 다.
아래 에 제 더 블 메 인 모델 의 메 인 keepalived 서버 설정 을 첨부 합 니 다.
! Configuration File for keepalived

global_defs {
   notification_email {
    root@localhost
    vrrp_mcast_group4  224.0.11.100
   }
   notification_email_from kaadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id sjf1
}

vrrp_script  chk_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.sh  master"
   notify_backup   "/etc/keepalived/inotify.sh  backup"
   notify_fault    "/etc/keepalived/inotify.sh  fault"
}
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.201  80

    real_server 11.1.0.203  80 {
        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.204  80 {
         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.sh  master"
   notify_backup   "/etc/keepalived/inotify.sh  backup"
   notify_fault    "/etc/keepalived/inotify.sh  fault"
}
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.201  80

    real_server 11.1.0.205  80 {
        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.206  80 {
         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_group4  224.0.11.100
   }
   notification_email_from kaadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id sjf2
}

vrrp_script  chk_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.sh  master"
   notify_backup   "/etc/keepalived/inotify.sh  backup"
   notify_fault    "/etc/keepalived/inotify.sh  fault"
}
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.201  80

    real_server 11.1.0.203  80 {
        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.204  80 {
         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.sh  master"
   notify_backup   "/etc/keepalived/inotify.sh  backup"
   notify_fault    "/etc/keepalived/inotify.sh  fault"
}
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.201  80

    real_server 11.1.0.205  80 {
        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.206  80 {
         weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

가상 네트워크 를 설정 한 스 크 립 트 를 첨부 합 니 다.
#!/bin/bash
vip='11.1.0.11'
vport='80'
netmask='255.255.255.255'
iface='lo:0'

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 $iface  $vip  netmask $netmask  broadcast $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.1
vip=192.16.11.99
contact='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" in
master)
        notify master
        exit 0
        ;;
backup)
        notify  backup
        exit 0
        ;;
fault)
        notify fault
        exit 0
        ;;
*)
        echo 'Usage: `basename $0` {maser|backup|fault} '
        exit 1
esac
        

좋은 웹페이지 즐겨찾기