基於Keepalive實現LVS高可用集羣

1、keepalived的基本介紹node

一、keepalivd的核心功能就是在linux系統上經過vrrp協議實現LVS的高可用。linux

二、vrrp協議(虛擬冗餘路由協議)能夠將多個網關虛擬成一個網關,同時一組IP虛擬成VIP,及其MAC地址能夠同時虛擬化。算法

三、keepalived經過vrrp協議可以很好實現故障轉移,避免單點故障發生,主節點服務故障時,備節點可以取代主節點繼續提供服務。當故障節點恢復正常後,能自動將此節點加入到服務中。shell

四、vrrp協議狀態機制vim

170412799.jpg

五、keepalived服務的安裝,基於Centos6.4的實驗環境,直接使用1.2.7版本的rpm包安裝keepalived。bash

六、keepalived的主配置文件 /etc/keepalived/keepalived.conf
服務器

keepalived的服務腳本 /etc/rc.d/init.d/keepalivedssh

2、keepalived的配置文件ide

一、全局配置段oop

GLOBAL CONFIGURATION


global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

定義郵件收發,靜態路由

二、keepalived的vrpp實例配置段

VRRPD CONFIGURATION


vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.16
        192.168.200.17
        192.168.200.18
    }
}

虛擬路由的配置實例爲核心配置段

三、keepalived的LVS虛擬服務器配置段

LVS CONFIGURATION


virtual_server 192.168.200.100 443 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    real_server 192.168.201.100 443 {
        weight 1
        SSL_GET {
            url {
              path /
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }
            url {
              path /mrtg/
              digest 9b3a0c85a887a256d6939da88aabd8cd
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

3、keepalived實現LVS的高可用的準備環境

一、準備三個節點 ms/node1/node2。

二、在節點ms安裝ansible服務,實現節點node1/node2的互信。

[root@ms ~]# yum -y install ansible
[root@ms ~]# ssh-keygen -t rsa -P ''
[root@ms ~]# ssh-copy-id -i .ssh/id.rsa.pub root@node1.xiaozheng.com
[root@ms ~]# ssh-copy-id -i .ssh/id.rsa.pub root@node2.xiaozheng.com

三、在節點node1/node2上安裝keepalived服務。

[root@ms ~]# ansible all -m shell -a "yum -y install keepalived"

四、去節點node1/node2查看keepalived的配置。

[root@node1 ~]# cd /etc/keepalived
[root@node1 keepalived]# vim keepalived.conf
[root@node2 ~]# cd /etc/keepalived
[root@node2 keepalivd]# vim keepalived.conf

五、另啓動node1/node2的終端開啓日誌通知,隨時檢測。

[root@node1 ~]# tail -f /var/log/message
[root@node2 ~]# tail -f /var/log/message

4、keepalived如何實如今狀態轉換時的通知

一、通知位置

vrrp_sync_group {

}

最經常使用的位置

vrrp_instance {

}

1)先定義一下全局配置段

global_defs {
      notification_email {
           root@localhost
      }
      notification_email_from msadmin@localhost
      smtp_server 127.0.0.1
      smtp_connect_timeout 30
      router_id LVS_DEVEL

2)定義相關控制機制

vrrp_script chk_main {
          script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
          interval 1
          weight -2
    }

3)接着定義vrrp實例段

節點node1的vrrp實例段配置

[root@node1 keepalived]# vim keepalived.conf
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 63
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    } 
    virtual_ipaddress {
        172.16.200.100
    } 
    track_script {
        chk_main
    }

節點node2vrrp實例段配置

[root@node2 keepalived]# vim keepalived.conf
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 63
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    } 
    virtual_ipaddress {
        172.16.200.100
    } 
    track_script {
        chk_main
    }

二、通知方式

notify_master 主節點通知

notify_backup 備節點通知

notify_fault 故障點通知

4)在實例中能夠定義使用notify.sh腳本控制通知方式

notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"

* notify.sh實例腳本

#!/bin/bash
# Author: MageEdu <linuxedu@foxmail.com>
# description: An example of notify script
vip=172.16.200.100
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
        exit 0
    ;;
    backup)
        notify backup
        exit 0
    ;;
    fault)
        notify fault
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
    ;;
esac

5)在節點ms上重啓node1/node2節點的keepalived服務並查看virtual_ipaddress所在節點

[root@ms ~]# ansible all -a "service keepalived restart"
[root@ms ~]# ansible alol -m shell -a "ip addr show | grep eth0"

6)在主節點node1上編譯down文件,實現單點故障使virtual_ipaddress從主節點node1轉移到node2上去,並在節點ms查看節點之間VIP轉移情況

[root@node1 keepalived]# touch down
[root@ms ~]# ansible all -m shell -a "ip addr show | grep eth0"

7) 恢復主節點node2,再次查看VIP的轉移情況

[root@node1 keepalived]# rm -rf down
[root@ms ~]# ansible all -m shell -a "ip addr show | grep eth0"

5、如何配置ipvs

核心配置段爲 virtual server 定義虛擬主機

一、virtual_server IP port 定義虛擬主機IP地址及其端口

二、virtual_server fwmark int ipvs的防火牆打標,實現基於防火牆的LVS

三、virtual_server group string

四、lb_algo {rr|wrr|lc|wlc|lblc|lblcr} 定義LVS的調度算法

五、lb_kind {NAT|DR|TUN} 定義LVS的模型

六、presitence_timeout <INT> 定義支持持久鏈接的時長

七、protocol ipvs規則所能支持的協議

1)在vrrp_server段配置ipvs的實例

主節點node1 vrrp_server中的ipvs配置

[root@node1 keepalived]# vim keepalived.conf
virtual_server 172.16.200.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    persistence_timeout 0
    protocol TCP
    real_server 172.16.200.8 80{
        weight 1
        HTTP_GET {
            url {
              path /
            status_code 200
            } 
           connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        } 
    }
}

備節點node2 vrrp_server中的ipvs配置

[root@node2 keepalived]# vim keepalived.conf
virtual_server 172.16.200.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    persistence_timeout 0
    protocol TCP
    real_server 172.16.200.9 80{
        weight 1
        HTTP_GET {
            url {
              path /
            status_code 200
            } 
           connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        } 
    }
}

2)在節點ms上爲node1/node2節點安裝ipvsadm服務,並啓動主備節點的httpd服務

[root@ms ~]# ansible all -m shell -a "yum -y install ipvsadm"
[root@ms ~]# ansible all -a "service httpd start"

3) 去節點node1/node2上查看相關的ipvs規則

[root@node1 keepalived]# ipvsadm -L -n
[root@node2 keepalived]# ipvsadm -L -n

6、對特定的服務作高可用

一、監控服務

vrrp_script {

}

二、在vrrp實例中追蹤服務

track_script {

}

7、實現基於多虛擬路由的雙master模型

要實現基於多虛擬路由的master/master模型,則須要定義多個 vrrp_intance段的配置。

一、配置節點node1上的vrrp_intance段,定義兩個


[root@node1 keepalived]# vim keepalived.conf
                                                                                                                                      
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 63
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    } 
    virtual_ipaddress {
        172.16.200.100
    } 
    track_script {
        chk_main
    } 
vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 65
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 21112
    }
    virtual_ipaddress {
       172.16.200.200
    }
    track_srcipt {
       chk_main
    }

二、配置node2節點上vrrp_intance段,定義兩個

[root@node2 keepalived]# vim keepalived.conf
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 63
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.200.100
    }
    track_script {
        chk_main
    }
vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 65
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 21112
    }
    virtual_ipaddress {
       172.16.200.200
    }
    track_srcipt {
       chk_main
    }

三、使主節點node1的keepalived的服務停掉,在節點ms查看主備節點之間的VIP的轉,同理使備節點node2的keepalived的服務停掉並使node1的keepalived的服務啓動,於節點ms上查看主備節點之間VIP的轉移。

[root@node1 keepalived]# servive keepalived stop
[root@ms ~]# ansible all -m shell -a "ip addr show | grep eth0"
[root@node2 keepalived]# servive keepalived stop
[root@node1 keepalived]# servive keepalived start
[root@ms ~]# ansible all -m shell -a "ip addr show | grep eth0"
相關文章
相關標籤/搜索