使用keepalived配置支持Linux集羣高可用

Linux集羣概述

  • 根據功能劃分爲兩大類:高可用和負載均衡
  • 高可用集羣一般爲兩臺服務器,一臺工做,另一臺做爲冗餘,當提供服務的機器宕機,冗餘將接替繼續提供服務
  • 實現高可用的開源軟件有:heartbeat、keepalived,推薦使用keepalived
  • 負載均衡集羣,須要有一臺服務器做爲分發器,它負責把用戶的請求分發給後端的服務器處理,在這個集羣裏,除了分發器外,就是給用戶提供服務的服務器了,這些服務器數量至少爲2
  • 實現負載均衡的開源軟件有LVS、keepalived、haproxy、nginx,商業的有F五、Netscaler

keepalived介紹

  • keepalived經過VRRP(Virtual Router Redundancy Protocol,虛擬路由冗餘協議)來實現高可用。
  • 在這個協議裏會將多臺功能相同的路由器組成一個小組,這個小組裏會有1個master角色和N(N>=1)個backup角色。
  • master會經過組播的形式向各個backup發送VRRP協議的數據包,當backup收不到master發來的VRRP數據包時,就會認爲master宕機了。此時就須要根據各個backup的優先級來決定誰成爲新的mater
  • keepalived要有三個模塊,分別是core、check和vrrp。其中core模塊爲keepalived的核心,負責主進程的啓動、維護以及全局配置文件的加載和解析,check模塊負責健康檢查,vrrp模塊是來實現VRRP協議的。

針對Nginx服務進行高可用配置

virtual_ipaddress 綁定的ip, 下面簡寫vipnginx

安裝keepalive
master(test-a:134)與backup(centos0: 129)安裝keepalivedvim

[root@test-a ~]# yum install -y keepalived
[root@centos0 ~]# yum install -y keepalived

# 使用yum安裝Nginx若是提示找不到對應的安裝包,須要安裝epel源
[root@centos0 ~]# yum install -y nginx
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.neusoft.edu.cn
 * updates: mirrors.cqu.edu.cn
沒有可用軟件包 nginx。
錯誤:無須任何處理
[root@centos0 ~]# yum install -y nginx # 再安裝

master配置啓動服務後端

# 先關閉防火牆
[root@test-a ~]# systemctl stop firewalld.service
[root@test-a ~]# setenforce 0

# 編輯配置文件
[root@test-a ~]# vim /etc/keepalived/keepalived.conf
[root@test-a ~]# cat  /etc/keepalived/keepalived.conf
global_defs {
   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 MASTER
    interface eno16777736
    virtual_router_id 1
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass test111
    }
    virtual_ipaddress {
        192.168.77.100
    }

    track_script {
        chk_nginx
    }

}


[root@test-a ~]# vim /usr/local/sbin/check_ng.sh  # 編寫對應的檢測腳本
[root@test-a ~]# cat /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
echo "$d nginx check begin"
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
        echo "$d nginx restart ok" >> /var/log/check_ng.log
fi

[root@test-a ~]# chmod 755 /usr/local/sbin/check_ng.sh
[root@test-a ~]# systemctl start keepalived.service
[root@test-a ~]# ps aux|grep keep
root      2841  0.0  0.1 119248  1396 ?        Ss   15:13   0:00 /usr/sbin/keepa                                          lived -D
root      2842  0.0  0.3 123448  3136 ?        S    15:13   0:00 /usr/sbin/keepa                                          lived -D
root      2843  0.1  0.2 123516  2532 ?        S    15:13   0:00 /usr/sbin/keepa                                          lived -D
root      2861  0.0  0.0 112704   972 pts/0    R+   15:14   0:00 grep --color=au                                          to keep
[root@test-a ~]# ps aux|grep nginx
root      1688  0.0  0.1  46548  1268 ?        Ss   15:00   0:00 nginx: master p                                          rocess /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    1694  0.0  0.3  49036  3900 ?        S    15:00   0:00 nginx: worker p                                          rocess
nobody    1695  0.0  0.3  49036  3900 ?        S    15:00   0:00 nginx: worker p                                          rocess
root      2911  0.0  0.0 112704   976 pts/0    R+   15:14   0:00 grep --color=au                                          to nginx

[root@test-a ~]# ip add  # 查看綁定的vip地址,ifconfig看不見
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stat                                          e UP qlen 1000
    link/ether 00:0c:29:b9:56:99 brd ff:ff:ff:ff:ff:ff
    inet 192.168.77.134/24 brd 192.168.77.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet 192.168.77.100/32 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet 192.168.77.139/24 brd 192.168.77.255 scope global secondary eno16777736                                          :t
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feb9:5699/64 scope link
       valid_lft forever preferred_lft forever

[root@test-a ~]# /etc/init.d/nginx stop # nginx中止後,檢測腳本會從新啓動
Stopping nginx (via systemctl):                            [  OK  ]
[root@test-a ~]# ps aux|grep keep
root      4508  0.0  0.1 119248  1396 ?        Ss   15:21   0:00 /usr/sbin/keepa                                          lived -D
root      4509  0.0  0.3 123448  3120 ?        S    15:21   0:00 /usr/sbin/keepa                                          lived -D
root      4510  0.0  0.2 123516  2652 ?        S    15:21   0:00 /usr/sbin/keepa                                          lived -D
root      5402  0.0  0.0 112704   976 pts/0    R+   15:25   0:00 grep --color=au                                          to keep
[root@test-a ~]# ps aux|grep nginx
root      5388  0.0  0.1  46548  1264 ?        Ss   15:25   0:00 nginx: master p                                          rocess /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    5392  0.0  0.3  49036  3900 ?        S    15:25   0:00 nginx: worker p                                          rocess
nobody    5393  0.0  0.3  49036  3900 ?        S    15:25   0:00 nginx: worker p                                          rocess
root      5428  0.0  0.0 112704   976 pts/0    R+   15:25   0:00 grep --color=au                                          to nginx
[root@test-a ~]#

backup配置啓動服務centos

[root@centos0 ~]# setenforce 0
[root@centos0 ~]# systemctl stop firewalld

[root@centos0 ~]# vim /usr/local/sbin/check_ng.sh
[root@centos0 ~]# cat /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@centos0 ~]# chmod 755 /usr/local/sbin/check_ng.sh 
[root@centos0 ~]# vim /etc/keepalived/keepalived.conf 
[root@centos0 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   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
    interface eno16777736
    virtual_router_id 1
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass test111
    }
    virtual_ipaddress {
        192.168.77.100
    }

    track_script {
        chk_nginx
    }

}

[root@centos0 ~]# ps aux | grep keep
root      4219  0.0  0.0 112656   988 pts/0    R+   15:46   0:00 grep --color=auto keep
[root@centos0 ~]# ps aux | grep nginx
root      4221  0.0  0.0 112656   988 pts/0    R+   15:46   0:00 grep --color=auto nginx

# 能夠看到,keepalived 和 nginx 都沒啓動起來,這時若是啓動keepalived,nginx也啓動起來,說明配置成功
[root@centos0 ~]# systemctl start keepalived
[root@centos0 ~]# ps aux | grep keep
root      4230  0.0  0.1 119100  1376 ?        Ss   15:48   0:00 /usr/sbin/keepalived -D
root      4231  0.0  0.3 123268  3096 ?        S    15:48   0:00 /usr/sbin/keepalived -D
root      4232  0.0  0.2 123268  2608 ?        S    15:48   0:00 /usr/sbin/keepalived -D
root      4262  0.0  0.0 112656   988 pts/0    R+   15:48   0:00 grep --color=auto keep
[root@centos0 ~]# ps aux | grep nginx # nginx也啓動起來啦
root      4256  0.0  0.2 125388  2112 ?        Ss   15:48   0:00 nginx: master process /usr/sbin/nginx
nginx     4257  0.0  0.3 125776  3132 ?        S    15:48   0:00 nginx: worker process
root      4282  0.0  0.0 112656   992 pts/0    R+   15:48   0:00 grep --color=auto nginx

瀏覽器訪問測試瀏覽器

訪問master:
bash

訪問backup:
服務器

訪問綁定的vip:
負載均衡

停掉master上的keepalived,再進行訪問測試,發現已經切到backup了oop

[root@test-a ~]# systemctl stop keepalived.service

相關文章
相關標籤/搜索