天天看點

通過keepalived 建立雙機浮動ip實作熱備高可用

Keepalived的作用是檢測web伺服器的狀态,如果有一台web伺服器當機,或工作出現故障,Keepalived将檢測到,并将有故障的web伺服器從系統中剔除,當web伺服器工作正常後Keepalived自動将web伺服器加入到伺服器群中,這些工作全部自動完成,不需要人工幹涉,需要人工做的隻是修複故障的web伺服器。

Keepalived是一個基于VRRP協定來實作的WEB 服務高可用方案,可以利用其來避免單點故障。一個WEB服務至少會有2台伺服器運作Keepalived,一台為主伺服器(MASTER),一台為備份伺服器(BACKUP),但是對外表現為一個虛拟IP,主伺服器會發送特定的消息給備份伺服器,當備份伺服器收不到這個消息的時候,即主伺服器當機的時候,備份伺服器就會接管虛拟IP,繼續提供服務,進而保證了高可用性。

下面講一下keepalived的搭建過程:

兩台機器ip分别為172.16.21.23和172.16.21.24,建立一個公共虛拟ip 172.16.21.79.

一、準備

yum install gcc
yum install -y openssl openssl-devel
           

下載下傳keepalived安裝包keepalived-1.4.2.tar.gz

wget http://www.keepalived.org/software/keepalived-1.4.2.tar.gz
           

解壓安裝包

tar -zxvf keepalived-1.4.2.tar.gz 得到檔案keepalived-1.4.2
           

進入目錄keepalived-1.4.2

cd keepalived-1.4.2
           

輸入以下指令:

make
make install
           

拷貝keepalived執行檔案

cp /data/keepalived-1.4.2/sbin/keepalived  /usr/sbin/
cp /data/keepalived-1.4.2/etc/sysconfig/keepalived /etc/sysconfig/
cp /data/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/

           

建立keepalived配置檔案目錄(預設/etc/keepalived)

mkdir /etc/keepalived
           

啟動keepalived服務

chkconfig keepalived on
           

二、配置/etc/keepalived/keepalived.conf( 兩台機器隻有這部分不同,其他步驟相同)

建立檔案keepalived.conf

touch keepalived.conf
           

編輯keepalived.conf,添加如下内容

(1)172.16.21.23(主)

vrrp_script chk_mariadb {
    script "/data/script/chk_mariadb.sh"
    interval 2
    weight 2
}
###########################################################
# define  mariadb_01                                           #
###########################################################
vrrp_instance V_mariadb_01 {
    interface eth0
    state MASTER
    priority 180
    virtual_router_id 53
    garp_master_delay 1


    authentication {
        auth_type PASS
        auth_pass 111
    }
    track_interface {
       eth0
    }
    virtual_ipaddress {
        172.16.21.79   dev eth0 label eth0:1
    }
    track_script {
        chk_mariadb
    }
}
           

(2)172.16.21.24(從):

vrrp_script chk_mariadb {
    script "/data/script/chk_mariadb.sh"
    interval 2
    weight 2
}


###########################################################
# define  mariadb_02                                           #
###########################################################
vrrp_instance V_mariadb_02 {
    interface eth0
    state BACKUP
    priority 150
    virtual_router_id 53
    garp_master_delay 1


    authentication {
        auth_type PASS
        auth_pass 222
    }
    track_interface {
       eth0
    }
    virtual_ipaddress {
        172.16.21.79   dev eth0 label eth0:2
    }
    track_script {
        chk_mariadb
    }
}
           

注:上面配置中提到的/data/script/chk_azkaban.sh需要獨立建立(每台機器),代碼如下:

#!/bin/bash 
STATUS=`netstat -nptl | grep mysql | wc -l`  
if [ "$STATUS" -eq "0" ]; then
   killall keepalived 
fi   
           

三、給腳本授權:

chmod +x chk_mariadb.sh
           

四、啟動keepailved:

進入keepalived安裝目錄後:bin/keepailved -D
           

五、關閉:

killall keepalived
           

啟動後使用ifconfig檢視master ip狀況如下

[[email protected] keepalived-1.4.2]# bin/keepalived -D
[[email protected] keepalived-1.4.2]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.21.24  netmask 255.255.255.0  broadcast 172.16.21.255
        inet6 fe80::cc43:e4ff:fea5:a889  prefixlen 64  scopeid 0x20<link>
        ether ce:43:e4:a5:a8:89  txqueuelen 1000  (Ethernet)
        RX packets 2497478  bytes 718499801 (685.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1334522  bytes 558798512 (532.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.21.79  netmask 255.255.255.255  broadcast 0.0.0.0
        ether ce:43:e4:a5:a8:89  txqueuelen 1000  (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 3254556  bytes 1764906803 (1.6 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3254556  bytes 1764906803 (1.6 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0eeth0:1
           

eth0:1為啟動的虛拟ip

注意,此時backup的節點上并沒有eth0:1的網卡資訊.隻有腳本中監測的主節點上mariadb的程序宕掉以後,從節點才會有172.16.21.79的ip.

如果搭建完畢不能顯示eth0:1網卡的資訊又找不到問題在哪,更快的解決辦法是建議重新搭建

繼續閱讀