day63:keepalived介紹及高可用集羣應用

一、linux集羣:linux根據功能可分爲兩大類:高可用負載均衡html

高可用集羣:一般是兩臺服務器,一臺工做,另外一臺提供冗餘,當提供服務器的機器down機,冗餘將接替繼續提供服務,提供系統可用效率,高可用的衡量標準----4個九:99.99,表示一個比例,在一年當中,99.99%都是在線的,不容許down機,不容許服務不可用,也有5個9、6個九:mysql

  實現高可用的開源軟件:keepalived      heartbeatlinux

heartbeat:在Centos中有許多bug,而且已經不在更新,不建議使用:nginx

keepalived:不止有高可用,還支持負載均衡,配置也簡單:git

負載均衡集羣:須要一臺服務器做爲調度分發器,它負責把用戶的請求分發給後端的服務器處理,一個請求過來,分發到後端的多個服務上,在這個集羣裏,除了調度分發器外,就是給用戶提供服務的服務器器了,這些服務器的數量至少是2:sql

 實現負載均衡的軟件有:LVS     keepalived     haproxy    nginx   硬件有:   F5     netscaler(更高的併發量,很好的穩定性)vim

keepalived介紹後端

   在這裏使用keepalived來實現高可用集羣,由於heartbeat在Centos上有一些問題,影響實驗效果,好比:heartbeat在切換時並非很及時,主以及down機,而備起不來等:瀏覽器

 keepalivedVRRP(Virtual Router Redundancy Protocl 中文爲:虛擬路由冗餘協議)來實現高可用。bash

  在這個協議裏,將多臺功能相同的路由器組成一個小組,在這個小組裏有一個master角色和N(N>=1)臺backup角色:

  master會經過組播的形式向各個backup發送VRRP協議的數據包,當backup收到master的數據包時,就會認爲master宕機了,此時就須要根據各個backup的優先級來決定誰成爲新的master:

 keepalived有三個模塊,分別是core   check   vrrp

  core:是keepalived的核心,負責主進程的啓動,維護以及全局配置文件的加載和解析:

  check:負責健康檢查:

  vrrp模塊:負責實習VRRP協議的:

keepalived高可用的操做配置

操做環境:兩臺Centos 7.3機器A和B:

master(A):192.168.149.131:安裝nginx,並安裝keepalived:

backup(B):192.168.149.132:安裝nginx,並安裝keepalived:

VIP:1921.68.149.254:   註釋:(virtual IP)虛擬IP,由keepalived設置,用於服務器對外提供服務:

註釋:安裝nginx:yum     install   -y    nginx

註釋:安裝keepalived:yum    install  -y   keepalived

master機器操做

 編輯keepalived配置文件---->>  master配置文件

 編輯keepalived監控腳本---->>   master監控腳本

 分別給腳本755權限: 並啓動服務:    systemctl         start      keepalived

backup機器操做

  編輯keepalived配置文件---->>  backup配置文件

 編輯keepalived監控腳本---->>   backup配置腳本

 分別給腳本755權限: 並啓動服務:    systemctl         start      keepalived

詳細操做步驟:  master===131        backup====132

masterA==131操做:
[root@localhost_03 ~]# yum install -y epel-release        #安裝擴展源:
[root@localhost_03 ~]# yum install -y keeplived           #安裝keepalived:
[root@localhost_03 ~]# yum install -y nginx               #安裝nginx:
backupB==132操做:
[root@localhost_04 ~]# yum install -y epel-release        #安裝擴展源:
[root@localhost_04 ~]# yum install -y keeplived           #安裝keepalived:
[root@localhost_04 ~]# yum install -y nginx               #安裝nginx:

1:更改masterA配置文件:   /etc/keepalived/keepalived.conf     master配置文件  

masterA機器修改配置文件:
[root@localhost_03 ~]# ls /etc/keepalived/keepalived.conf 
/etc/keepalived/keepalived.conf
[root@localhost_03 ~]# > !$                            #清空配置文件:
> /etc/keepalived/keepalived.conf
[root@localhost_03 ~]# vim /etc/keepalived/keepalived.conf 
############全局配置:
global_defs {                                           #global_defs全局配置標識:
   notification_email {                             #notification_email用於設置報警郵件地址:
     yuanhh@forebix.com                             #可設置多個,每行一個:
   }
   notification_email_from root@aminglinux.com      #設置郵件發送地址:   
   smtp_server 127.0.0.1                            #設置郵件的smtp_server地址:
   smtp_connect_timeout 30                          #設置郵件的smtp_server超時時間地址:
   router_id LVS_DEVEL
}

###########VRRP的配置:
vrrp_script chk_nginx {
    script "/usr/local/sbin/check_ng.sh"            #檢測服務器是否正常,經過寫腳本實現,腳本檢測服務器狀態:
    interval 3                                      #檢查時間間隔是3s:
}

vrrp_instance VI_1 {                                #VRRP配置標識,VI_1是實例名稱:
    state MASTER                                    #定義master:
    interface eth0                                  #指定經過這個網卡去發vrrp的廣播通訊,此處要注意:
    virtual_router_id 50                            #定義路由器ID,配置和備機器一致:
    priority 100                                    #定義優先級,權重,主和備的權重是不同的:
    advert_int 1                                #設定master和backup同步時間間隔:
    authentication {                            #認證相關信息:
        auth_type PASS                          #認證相關信息:
        auth_pass   nihao123!                   #密碼的形式:字符串
    }
    virtual_ipaddress {                         #定義VIP地址:  
        192.168.149.254                         #VIP地址:                         
    }

    track_script {                              #加載腳本:
        chk_nginx
    }

}

註釋:virtual_ip,簡稱虛擬VIP地址,虛擬IP是主備IP的集合,請求過來的時候首先訪問的是這個IP地址,再有這個IP分發給下面的IP地址,如:有兩臺機器,一主一備,正常狀況下是主在服務,主宕機了,備起來nginx服務,備起來後再訪問的就是這個虛擬VIP地址,能夠再keepalived中使用:而且解析域名會解析到這個IP上來:

二、定義監控腳本:keepalived監控腳本             /usr/local/sbin/check_ng.sh

[root@localhost_03 ~]# vim /usr/local/sbin/check_ng.sh
#!/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
        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
#更改腳本權限:
[root@localhost_03 ~]# chmod 755 /usr/local/sbin/check_ng.sh

註釋:腦裂:在HA高可用中,當鏈接主備兩臺服務器的心跳線斷開時,原本爲總體的HA系統就會分裂成兩個獨立的個體,因爲相互失去了聯繫,並都認爲是對方出現了故障,兩個節點的HA像裂腦人同樣,爭搶共享資源和應用服務,當master宕機後仍然未釋放VIP,同時backup接替master提供服務要使用同一VIP,於是致使兩臺機器爭佔同一VIP致使服務紊亂會致使兩邊服務都起不來,或者兩邊服務都起來了,當同時讀寫共享存儲,致使數據損壞:

 如何判斷腦裂:分別在兩臺機查看當前服務器是否擁有虛擬IP,若是兩臺服務器都擁有,則說明發生了腦裂,證實目前雙機通訊出現問題,產生此問題的原有在於 兩臺服務器都探測不到組內其餘服務器的狀態(心跳請求沒法正常響應),私自斷定另外一臺服務器掛起,則搶佔虛擬IP,腦裂的出現是不被容許的,解決此問題的方法爲檢查防火牆設置(關閉防火牆)或者使用串口通訊。

註釋:keepalived的日記:  /var/log/message

三、啓動keepalived服務,並查看是否啓動成功:(若是啓動不成功,多是防火牆或者selinux形成的):

[root@localhost_03 ~]# systemctl start keepalived
[root@localhost_03 ~]# !ps
ps aux |grep keep
root       1245  0.0  0.1 118608  1384 ?        Ss   11:12   0:00 /usr/sbin/keepalived -D
root       1246  0.0  0.2 122804  2368 ?        S    11:12   0:00 /usr/sbin/keepalived -D
root       1247  0.0  0.2 122804  2412 ?        S    11:12   0:00 /usr/sbin/keepalived -D
root       1249  0.0  0.0 112676   980 pts/0    R+   11:12   0:00 grep --color=auto keep

四、啓動並查看nginx服務:          systemctl      start    nginx

[root@localhost_03 ~]# systemctl start nginx
[root@localhost_03 ~]# ps aux |grep nginx
root       1283  0.0  0.2 120752  2096 ?        Ss   11:21   0:00 nginx: master process /usr/sbin/nginx
nginx      1284  0.0  0.3 121136  3124 ?        S    11:21   0:00 nginx: worker process
root       1286  0.0  0.0 112676   980 pts/0    R+   11:21   0:00 grep --color=auto nginx

五、而後關閉nginx服務,並再次查看,發現會自動加載nginx:

[root@localhost_03 ~]# systemctl stop nginx
[root@localhost_03 ~]# !ps 
ps aux |grep nginx 
root       1936  0.0  0.2 120752  2092 ?        Ss   11:33   0:00 nginx: master process /usr/sbin/nginx
nginx      1937  0.0  0.3 121136  3120 ?        S    11:33   0:00 nginx: worker process
root       1942  0.0  0.0 112676   980 pts/0    R+   11:33   0:00 grep --color=auto nginx
[root@localhost_03 ~]# ps -C nginx --no-heading|wc -l
2

六、查看虛擬VIP地址:使用ip addr這個命令:(ifconfig沒法查看看)

[root@localhost_03 ~]# ip addr
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:66:d6:8e brd ff:ff:ff:ff:ff:ff
    inet 192.168.149.131/24 brd 192.168.149.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.149.254/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe66:d68e/64 scope link 
       valid_lft forever preferred_lft forever

註釋:以上是master主機器配置:

二、backup備機器配置:

backup機器操做

  編輯keepalived配置文件---->>  backup配置文件

 編輯keepalived監控腳本---->>   backup配置腳本

 分別給腳本755權限: 並啓動服務:    systemctl         start      keepalived

一、更改backup的配置文件:      /etc/keepalived/keepalived.conf

[root@localhost_04 ~]# > /etc/keepalived/keepalived.conf 
[root@localhost_04 ~]# vim /etc/keepalived/keepalived.conf 
global_defs {
   notification_email {
     aming@aminglinux.com
   }
   notification_email_from root@aminglinux.com
   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                   #這裏和master是不同:  
    interface eth0                 #這裏是當前的網卡名稱:
    virtual_router_id 50           #路由器標識,和master的要相同:
    priority 90                    #優先級:權重值,須要比master的小:
    advert_int 1                   
    authentication {
        auth_type PASS
        auth_pass nihao123!
    }
    virtual_ipaddress {
        192.168.149.254           #虛擬VIP地址:
    }

註釋:若是是多臺服務器,權重值決定了master宕機後,backup接管master角色的順序問題:

二、定義監控腳本:       /usr/local/sbin/check_ng.sh

#時間變量,用於記錄日誌
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
[root@localhost_04 ~]# chmod 755 /usr/local/sbin/check_ng.sh

三、backup備機器啓動keepalived和nginx:並查看:

[root@localhost_04 ~]# systemctl start keepalived
[root@localhost_04 ~]# ps aux |grep keep
root       1066  0.0  0.1 118608  1388 ?        Ss   11:51   0:00 /usr/sbin/keepalived -D
root       1067  0.0  0.3 127468  3288 ?        S    11:51   0:00 /usr/sbin/keepalived -D
root       1068  0.0  0.2 127340  2572 ?        S    11:51   0:00 /usr/sbin/keepalived -D
root       1075  0.0  0.0 112676   984 pts/0    R+   11:51   0:00 grep --color=auto keep
[root@localhost_04 ~]# systemctl start nginx
[root@localhost_04 ~]# ps aux |grep nginx
root       1089  0.0  0.2 120752  2092 ?        Ss   11:52   0:00 nginx: master process /usr/sbin/nginx
nginx      1090  0.0  0.3 121136  3120 ?        S    11:52   0:00 nginx: worker process
root       1092  0.0  0.0 112676   984 pts/0    R+   11:52   0:00 grep --color=auto nginx

註釋:由於此時VIP在主master上面,全部在備backup上是查找不到的:

[root@localhost_04 ~]# ip addr
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:eb:02:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.149.132/24 brd 192.168.149.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feeb:22d/64 scope link 
       valid_lft forever preferred_lft forever

四、如何區分是主master仍是備backup

master主訪問:    瀏覽器輸入:192.168.149.131         配置文件目錄:  /usr/share/nginx/html/index.html

[root@localhost_03 ~]# cat /usr/share/nginx/html/index.html 
master  Ths is  master site!!!

backup備訪問:   瀏覽器輸入:192.168.149.132         配置文件目錄:  /usr/share/nginx/html/index.html

[root@localhost_04 ~]# cat /usr/share/nginx/html/index.html 
backup  backup

五、此時能夠訪問虛擬VIP地址:瀏覽器輸入: 192.168.149.254    此時發現查看到的是master主的地址:以下:

五、測試高可用:模擬master主宕機,即master主關閉keepalived的情景:以下:

[root@localhost_03 ~]# systemctl stop  keepalived

六、查看master主的虛擬VIP地址,發現已經被釋放了:

[root@localhost_03 ~]# ip addr
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:66:d6:8e brd ff:ff:ff:ff:ff:ff
    inet 192.168.149.131/24 brd 192.168.149.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe66:d68e/64 scope link 
       valid_lft forever preferred_lft forever

說明:發現虛擬VIP地址:192.168.149.254已經不存在了,被釋放了:

七、查看backup備的IP地址,發現虛擬VIP地址已經在這裏:   ip   addr

[root@localhost_04 ~]# ip addr
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:eb:02:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.149.132/24 brd 192.168.149.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.149.254/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feeb:22d/64 scope link 
       valid_lft forever preferred_lft forever

註釋:虛擬VIP地址轉移到backup備的機器上了:

8:瀏覽器訪問: 192.168.149.254      #切換到backup的網頁:     

#backup備接替了master主的角色,成爲了新的master:

九、查看backup備(新master角色)的日記:           /var/log/message:

[root@localhost_04 ~]# tail -f /var/log/messages
Sep  6 15:16:29 localhost_04 Keepalived_vrrp[1406]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:16:29 localhost_04 Keepalived_vrrp[1406]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:16:29 localhost_04 Keepalived_vrrp[1406]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:16:29 localhost_04 Keepalived_vrrp[1406]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:16:34 localhost_04 Keepalived_vrrp[1406]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:16:34 localhost_04 Keepalived_vrrp[1406]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.149.254
Sep  6 15:16:34 localhost_04 Keepalived_vrrp[1406]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:16:34 localhost_04 Keepalived_vrrp[1406]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:16:34 localhost_04 Keepalived_vrrp[1406]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:16:34 localhost_04 Keepalived_vrrp[1406]: Sending gratuitous ARP on eth0 for 192.168.149.254

10:在master(原A機器)重啓keepalived服務後,則這個虛擬VIP會當即會當即切換回來:

[root@localhost_03 ~]# systemctl start keepalived 
[root@localhost_03 ~]# ip addr
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:66:d6:8e brd ff:ff:ff:ff:ff:ff
    inet 192.168.149.131/24 brd 192.168.149.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.149.254/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe66:d68e/64 scope link 
       valid_lft forever preferred_lft forever

11:在瀏覽器再次訪問,並查看日記:           /var/log/message

[root@localhost_03 ~]# tail -f /var/log/messages
Sep  6 15:23:14 localhost_03 Keepalived_vrrp[8553]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:23:14 localhost_03 Keepalived_vrrp[8553]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:23:14 localhost_03 Keepalived_vrrp[8553]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:23:14 localhost_03 Keepalived_vrrp[8553]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:23:19 localhost_03 Keepalived_vrrp[8553]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:23:19 localhost_03 Keepalived_vrrp[8553]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.149.254
Sep  6 15:23:19 localhost_03 Keepalived_vrrp[8553]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:23:19 localhost_03 Keepalived_vrrp[8553]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:23:19 localhost_03 Keepalived_vrrp[8553]: Sending gratuitous ARP on eth0 for 192.168.149.254
Sep  6 15:23:19 localhost_03 Keepalived_vrrp[8553]: Sending gratuitous ARP on eth0 for 192.168.149.254

彙總:在生產環境中,可能會用到2-3臺的backup角色的設備,則須要在/etc/keepalived/keepalived.conf這裏設置權重值,權重值的越大優先級越高,除了nginx服務,還能夠作mysql的高可用集羣(mysql集羣必定要保證兩邊的數據要一致):

相關文章
相關標籤/搜索