1. 程式人生 > >keepalived介紹、高可用配置;linux集群介紹;

keepalived介紹、高可用配置;linux集群介紹;

keepalived 高可用配置 keepalived 冗余 Linux集群 VRRP

Linux集群

分類(按功能)

1. 高可用:通常為兩臺服務器,一臺工作,另一臺做冗余;當主機宕機,冗余接替提供服務;實現軟件有:heartbeat、keepalived;
2. 負載均衡:一臺做分發器,將任務分發給其他機器;可以當成一臺做調度,1+N服務;軟件有:LVS、keepalived、haproxy、nginx;

keepalived介紹

keepalived通過VRRP協議來實現高可用,在這個協議裏面會將多臺功能相同的路由器組成一個組,其中一個Master,N個backup角色;
master會通過組播的形式向各個backup發送VRRP協議的數據包,當backup收不到master發來的VRRP數據包時,就會認為master宕機了。此時就需要根據各個backup的優先級來決定誰成為新的master。

三個模塊

1. core:核心模塊,主要負責主進程的啟動、維護以及全局配置文件的加載與解析;
2. check:負責健康體檢;
3. vrrp:實現VRRP協議;

keepalived配置

master機器:192.168.188.2
backup機器:192.168.188.3
vip公共ip:192.168.188.188
組id:51

安裝keepalived

yum install -y keepalived

master機器設置

vim /etc/keepalived/keepalived.conf            //編輯配置

global_defs {                                                    //定義錯誤發送到指定郵件
   notification_email {
[email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_nginx {                                    //定義健康檢查shell,這裏指定的shell要手動創建
    script "/usr/local/sbin/check_ng.sh"
    interval 3
}
vrrp_instance VI_1 {
    state MASTER                                            //設置為master
    interface ens33                                        //指定網卡,這裏試ens33,有的是eth0(根據自身情況改變)
    virtual_router_id 51                                //定義組id,必須一致
    priority 100                                            //優先級100,優先級越大越優先
    advert_int 1
    authentication {                                                //定義認證形式
        auth_type PASS                                               //認證形式為pass密碼認證
        auth_pass testlinux123                            //定義密碼為testlinux123
    }
    virtual_ipaddress {
        192.168.188.188                                //定義vip的ip,也就是所有服務器共用的一個ip
    }
    track_script {                                                //加載健康檢查腳本
        chk_nginx
    }
}
vim /usr/local/sbin/check_ng.sh            //創建健康檢查的shell

#!/bin/bash
#時間變量,用於記錄日誌
d=`date --date today +%Y%m%d_%H:%M:%S`
#計算nginx進程數量
n=`ps -C nginx --no-heading|wc -l`
#如果進程為0,則啟動nginx,並且再次檢測nginx進程數量,
#如果還為0,說明nginx無法啟動,此時需要關閉keepalived
if [ $n -eq "0" ]; then
/etc/init.d/nginx start
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
chmod 755 /usr/local/sbin/check_ng.sh        //給shell文件755權限
systemctl start keepalived                //啟動keepalived服務

backup設置

vim /etc/keepalived/keepalived.conf

global_defs {
   notification_email {
[email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_nginx {
    script "/usr/local/sbin/check_ng.sh"
    interval 3
}
vrrp_instance VI_1 {
    state BACKUP                                                //定義backup
    interface ens33                                            //定義網卡
    virtual_router_id 51                                    //和master一樣的id
    priority 90                                                    //定義優先級,要比master的小,越小越不優先
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass testlinux123
    }
    virtual_ipaddress {
        192.168.188.188
    }
    track_script {
        chk_nginx
    }
}

vim /usr/local/sbin/check_ng.sh            //創建健康檢查的shell

#時間變量,用於記錄日誌
d=`date --date today +%Y%m%d_%H:%M:%S`
#計算nginx進程數量
n=`ps -C nginx --no-heading|wc -l`
#如果進程為0,則啟動nginx,並且再次檢測nginx進程數量,
#如果還為0,說明nginx無法啟動,此時需要關閉keepalived
if [ $n -eq "0" ]; then
systemctl start nginx
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
chmod 755 /usr/local/sbin/check_ng.sh        //給shell文件755權限
systemctl start keepalived                //啟動keepalived服務

日誌查看

less /var/log/messages

vip的ip查看

ip add

測試
master關閉keepalived,訪問vip的ip192.168.188.188為buckup的內容

systemctl stop keepalived
systemctl start keepalived

keepalived介紹、高可用配置;linux集群介紹;