keepalived學習

HA集羣node

1 keepalived
2 heartbeat
3 corosync
4 cman

 

功能實現linux

vrrp協議在Linux主機上以守護進程方式,

可以根據配置文件自動生成ipvs規則

對各RS健康狀態檢測

 

組件算法

vrrp stack

checkers

ipvs wrapper ---> ipvs

 

HA Cluster配置準備
vim

1. 本機的主機名與Hosts中定義的主機保持一致,以及hostname、uname -n得到的名稱保持一致

    CentOS6 /etc/sysconfig/network

    CentOS7 hostnamectl set-hostname HOSTNAME | /etc/hostname

    各節點能互相解析主機名編輯/etc/hosts文件

2. 各節點時間同步

3. 確保iptables及selinux規則影響keepalive

4. 爲了防止配置文件修改出錯不可回改首先備份文件

 

配置文件詳解服務器

man keepalived.conf

定義收件人

    notification_email {...}   # To:

定義發件人

    notification_email_from admin@example.com

SMTP服務器

    smtp_server 127.0.0.1 [<PORT>]

郵件超時時間

    smtp_connect_timeout 30

定義route_id

    router_id LVS_DEVEL

定義組播地址

    vrrp_mcast_group4 224.0.0.1

例子:本機郵件

    global_defs {
        notification_email {
                root@localhost
        }
        notification_email_from admin@localhost
        smtp_server 127.0.0.1
        smtp_connect_timeout 30
        router_id node1.example.com
        vrrp_skip_check_adv_addr
        vrrp_strict
        vrrp_garp_interval 0
        vrrp_gna_interval 0
        # vrrp_mcast_group4 224.0.0.1 雙主實例默認組播地址不能更改
    }

VRRP組身份

    state MASTER

通告網卡

    interface eth0

虛擬Route_ID(要惟一)

    virtual_router_id 51

優先級,用於斷定VRRP組身份

    priority 100

心跳發送間隔

    advert_int 1

身份認證

    authentication {
            # PASS||AH
            # PASS - Simple password (suggested)
            # AH - IPSEC (not recommended))
            auth_type PASS
            # Password for accessing vrrpd.
            # should be the same on all machines.
            # Only the first eight (8) characters are used.
            auth_pass 1234
    }

虛擬地址配置

    virtual_ipaddress {
        <IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPE> label <LABEL>
        192.168.200.17/24 dev eth1
        192.168.200.18/24 dev eth2 label eth2:1
    }

例子:vrrp實例

    vrrp_instance VI_1 {
        state MASTER
        interface ens33
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 123456
        }
        virtual_ipaddress {
            192.168.180.100/24 dev ens33 label ens33:0
        }
    }

 

使用日誌功能網絡

vim /etc/sysconfig/keepalived 

    KEEPALIVED_OPTIONS="-D -S 3"


# LOG守護進程配置

vim /etc/rsyslog.conf 

    local3.*                                                /var/log/keepalived.log

systemctl restart rsyslog

 

VRRP腳本自動檢測優先級併發

# 實例外定義,實例內調用

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

vrrp_instance INSTANCE_NAME {
    ...
    track_script {
        chk_schedown
    }
}

 

keepalive基於LVS-NAT配置app

vrrp_sync_group VG_1 {
    VI_1
    VI_2
}

vrrp_instance VI_1 {
    interface ...
    VIP
}

vrrp_instance VI_2 {
    instance ...
    DIP
}

 

keepalived雙主模型配置oop

node1 

    vrrp_instance VI_1 {
        state MASTER
        interface ens33
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 123456
        }
        virtual_ipaddress {
            192.168.180.100/24 dev ens33 label ens33:0
        }
    }

    vrrp_instance VI_2 {
        state BACKUP
        interface ens33
        virtual_router_id 61
        priority 99
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 654321
        }
        virtual_ipaddress {
            192.168.180.110/24 dev ens33 label ens33:1
        }
    }

node2 

    vrrp_instance VI_1 {
        state BACKUP
        interface ens33
        virtual_router_id 51
        priority 99
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 123456
        }
        virtual_ipaddress {
            192.168.180.100/24 dev ens33 label ens33:0
        }
    }

    vrrp_instance VI_2 {
        state MASTER
        interface ens33
        virtual_router_id 61
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 654321
        }
        virtual_ipaddress {
            192.168.180.110/24 dev ens33 label ens33:1
        }
    }

 

 

Virtual Instence 狀態發生改變時,發送通知郵件url

當前節點轉換爲master狀態時,發送通知郵件

    notify_master <STRING>|<QUOTED-STRING> [username [groupname]]

    # 編寫腳本定製發送內容
    notify_master "/etc/keepalived/notify.sh master"

當前節點轉換爲backup狀態時,發送通知郵件

    notify_backup <STRING>|<QUOTED-STRING> [username [groupname]]

當前節點轉換爲backup狀態時(節點故障),發送通知郵件
    notify_fault <STRING>|<QUOTED-STRING> [username [groupname]]

當前節點轉換爲stop狀態時(節點故障),發送通知郵件

    notify_stop <STRING>|<QUOTED-STRING> [username [groupname]]      # executed when stopping vrrp

 

Virtual server

三種類型組

    virtual_server IP port 

    virtual_server fwmark int 
    
    virtual_server group string

向RS發送健康狀態檢查間隔

    delay_loop <INT>

負責均衡所支持的調度算法

    lb_algo rr|wrr|lc|wlc|lblc|sh|dh

對每個報文單獨調度

    ops

lvs支持類型

    lb_kind NAT|DR|TUN

持久時長

    persistence_timeout [<INT>]

協議選擇

    protocol TCP|UDP|SCTP

沒有虛擬地址時掛起監控檢查功能

    ha_suspend

定義對哪些虛擬主機作健康狀態檢測

    virtualhost <STRING>

全部的虛擬主機都失效時,Sorry Server生效

    sorry_server <IPADDR> <PORT>

real_server <IPADDR> <PORT>

    weight <INT>

    notify_up <STRING>|<QUOTED-STRING>

    notify_down <STRING>|<QUOTED-STRING>

    健康狀態檢測協議

        # HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|DNS_CHECK|MISC_CHECK

        HTTP_GET|SSL_GET

            url {
                    #eg path / , or path /mrtg2/
                    path <STRING>
                    # healthcheck needs status_code
                    # or status_code and digest
                    # Digest computed with genhash
                    # eg digest 9b3a0c85a887a256d6939da88aabd8cd
                    digest <STRING>
                    # status code returned in the HTTP header
                    # eg status_code 200. Default is any 2xx value
                    status_code <INT>
                }

            # 若是服務器繁忙或者其餘緣由獲取不到頁面時,應該再從新嘗試幾回

                nb_get_retry <INT>

            # 從新獲取的等待延時

                delay_before_retry <INT>

            connect_ip <IP ADDRESS>

            connect_port <PORT>

            bindto <IP ADDRESS>

            bind_port <PORT>

            connect_timeout <INTEGER>

            # 基於fwmark檢測時使用

                fwmark <INTEGER>

            # 打散各RS的檢測時間,減小同一時間點上的併發數,減小網絡負擔

                warmup <INT>
相關文章
相關標籤/搜索