Nginx+Keepalived主從模式-負(fù)載均衡高可用(CentOS7.5)

簡(jiǎn)介

這種方案,使用一個(gè)虛擬VIP地址,前端使用2臺(tái)機(jī)器,一臺(tái)做主,一臺(tái)做備,但同時(shí)只有一臺(tái)機(jī)器工作,另一臺(tái)備機(jī)在主機(jī)器不出現(xiàn)故障的時(shí)候,永遠(yuǎn)處于浪費(fèi)狀態(tài),對(duì)于服務(wù)器不多的網(wǎng)站,該方案并不經(jīng)濟(jì)實(shí)惠,但能快速切換。

關(guān)于Nginx版本

Mainline version:開(kāi)發(fā)版

Stable version:穩(wěn)定版

Legacy versions:遺留的老版本

官方地址:http://nginx.org/,找到“news”中,最新的一個(gè)stable version

下載地址:http://nginx.org/download/,找到這個(gè)包的下載鏈接,右鍵復(fù)制鏈接地址

規(guī)劃:

LB-01:16.155.199.223? nginx+keepalived-master

LB-02:16.155.197.42? nginx+keepalived-backup

VIP:16.155.197.100

OS:CentOS 7.5?

(tips:master,backup機(jī)器一定要在同一個(gè)網(wǎng)段內(nèi),vip也要設(shè)置在同一個(gè)網(wǎng)段內(nèi),不知道怎么在同一個(gè)網(wǎng)段內(nèi)的小伙伴請(qǐng)自行百度一下)

Nginx+Keepalived主從架構(gòu)

Tips:

在下面的部署過(guò)程中,為了節(jié)省篇幅,只顯示了LB-01:16.155.199.223上的部署過(guò)程。新手請(qǐng)注意,按照部署過(guò)程,凡是在LB-01:16.155.199.223上做的所有配置(準(zhǔn)備工作、部署Nginx、部署Keepalived),都需要在LB-02:16.155.197.42上,再部署一次,并保持兩邊的配置過(guò)程一樣!

1.準(zhǔn)備工作

查看centos版本命令:

cat /etc/centos-release

1.1 關(guān)閉SELinux

[root@example01 ~]# vim /etc/sysconfig/selinux

SELINUX=disabled

1.2 關(guān)閉IPTABLES防火墻

[root@example01 ~]#?systemctl unmask firewalld

[root@example01 ~]#?systemctl start firewalld

[root@example01 ~]#?firewall-cmd --zone=public --add-port=80/tcp --permanent 開(kāi)放80端口

[root@example01 ~]#?firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface em1 --destination 224.0.0.18 --protocol vrrp -j ACCEPT

'[root@example01 ~]#?firewall-cmd --reload


1.3 安裝wget

[root@example01 ~]# yum -y install wget

準(zhǔn)備工作到此為止,reboot命令,重啟兩臺(tái)服務(wù)器,使得SELinux配置生效

2.部署nginx

2.1 安裝編譯工具及庫(kù)文件

[root@example01 ~]# yum -y install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.2 安裝nginx

[root@example01 ~]# cd /usr/local/src/

[root@example01 src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz

[root@example01 src]# tar -zxvf nginx-1.6.2.tar.gz

?[root@example01 src]# cd nginx-1.6.2

[root@example01 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx \--with-http_stub_status_module \? --with-http_ssl_module?

?[root@example01 nginx-1.6.2]# make && make install

配置報(bào)錯(cuò):

./configure: error: the HTTP rewrite module requires the PCRE library.

You can either disable the module by using --without-http_rewrite_module

option, or install the PCRE library into the system, or build the PCRE library

statically from the source with nginx by using --with-pcre= option.

解決辦法:

[root@example01 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx \> --without-http_rewrite_module

2.3 配置nginx

2.3.1 創(chuàng)建nginx運(yùn)行使用的用戶(hù)www

[root@example01 nginx-1.6.2]# /usr/sbin/groupadd www

[root@example01 nginx-1.6.2]# /usr/sbin/useradd -g www www

2.3.2 啟動(dòng)nginx

啟動(dòng)服務(wù)

[root@example01 nginx-1.6.2]# /usr/local/nginx/sbin/nginx

重載nginx配置

[root@example01 nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s reload

開(kāi)機(jī)啟動(dòng)

[root@example01 src]# vim /etc/rc.local # Nginx/usr/local/nginx/sbin/nginx

2.3.3 編輯index.html文件

編輯LB-01:16.155.199.223

[root@example01 nginx-1.6.2]# vim /usr/local/nginx/html/index.html? ? 14

Welcome to nginx!Server01

編輯LB-02:16.155.197.42

[root@example02 nginx-1.6.2]# vim /usr/local/nginx/html/index.html? ? 14

Welcome to nginx!Server02

2.3.4 配置nginx.conf

? ? 2 user? www www;

? ? 3 worker_processes? 1;

? 35? ? upstream my Server {

? 36? ? ? ? ip_hash;

? 37? ? ? ? server 16.155.199.223;

? 38? ? ? ? server 16.155.197.42;

? 39? ? }

Tips:

負(fù)載均衡模塊用于從”upstream”指令定義的后端主機(jī)列表中選取一臺(tái)主機(jī)。nginx先使用負(fù)載均衡模塊找到一臺(tái)主機(jī),再使用upstream模塊實(shí)現(xiàn)與這臺(tái)主機(jī)的交互。

從配置我們可以看出負(fù)載均衡模塊的使用場(chǎng)景:

1.核心指令”ip_hash”只能在upstream {}中使用。這條指令用于通知nginx使用ip hash負(fù)載均衡算法。如果沒(méi)加這條指令,nginx會(huì)使用默認(rèn)的round robin負(fù)載均衡模塊。

2.upstream {}中的指令可能出現(xiàn)在”server”指令前,可能出現(xiàn)在”server”指令后,也可能出現(xiàn)在兩條”server”指令之間。

2.3.5 瀏覽器訪(fǎng)問(wèn):

http://16.155.199.223/

LB-01:16.155.199.223

http://16.155.197.42

LB-02:16.155.197.42

nginx其它命令:

/usr/local/nginx/sbin/nginx -s reload# 重新載入配置文件

/usr/local/nginx/sbin/nginx -s reopen# 重啟 Nginx

/usr/local/nginx/sbin/nginx -s stop# 停止 Nginx

3.部署keepalived

3.1 安裝keepalived

[root@example01 src]# yum -y install keepalived

查看keepalived版本

[root@example01 src]# keepalived -vKeepalived v1.2.13(03/19,2015)

3.2 修改keepalived的配置文件

LB-01:16.155.199.223/的配置

[root@example01 src]# vim /etc/keepalived/keepalived.conf

? vrrp_script chk_nginx {

? ? script "/etc/keepalived/nginx_check.sh"? ? # 檢測(cè)nginx狀態(tài)的腳本路徑

? ? interval 2? ? ? ? ? ? ? ? # 檢測(cè)時(shí)間間隔2s

? ? weight -20? ? ? ? ? ? ? ? # 如果腳本的條件成立,權(quán)重-20

? }

? vrrp_instance VI_1 {

? ? ? state MASTER? ? ? ? ? ? ? # 服務(wù)狀態(tài);MASTER(工作狀態(tài))BACKUP(備用狀態(tài))

? ? ? interface ens192? ? ? ? ? ? ? # VIP綁定網(wǎng)卡,大家默認(rèn)的一般是eth0,我這里個(gè)性化設(shè)置是ens192

? ? ? virtual_router_id 51? ? ? # 虛擬路由ID,主、備節(jié)點(diǎn)必須一致

? ? ? mcast_src_ip 16.155.199.223 # 本機(jī)IP

? ? ? nopreempt? ? ? ? ? ? ? ? # 優(yōu)先級(jí)高的設(shè)置,解決異常回復(fù)后再次搶占的問(wèn)題

? ? ? priority 100? ? ? ? ? ? ? # 優(yōu)先級(jí);取值范圍:0~254;MASTER > BACKUP

? ? ? advert_int 1? ? ? ? ? ? ? # 組播信息發(fā)送間隔,主、備節(jié)點(diǎn)必須一致,默認(rèn)1s

? ? ? authentication {? ? ? ? ? # 驗(yàn)證信息;主、備節(jié)點(diǎn)必須一致

? ? ? ? ? auth_type PASS? ? ? ? ? # VRRP驗(yàn)證類(lèi)型,PASS、AH兩種

? ? ? ? ? auth_pass 1111? ? ? ? ? # VRRP驗(yàn)證密碼,在同一個(gè)vrrp_instance下,主、從必須使用相同的密碼才能正常通信

? ? ? }

? ? track_script {? ? ? ? ? # 將track_script塊加入instance配置塊

? ? ? ? ? chk_nginx? ? ? ? # 執(zhí)行Nginx監(jiān)控的服務(wù)

? ? ? }

? ? ? virtual_ipaddress {? ? ? ? # 虛擬IP池,主、備節(jié)點(diǎn)必須一致,可以定義多個(gè)VIP

? ? ? ? ? 16.155.197.100? ? ? ? ? # 虛擬IP

? ? ? }

? }


LB-02:16.155.197.42的配置

[root@example02 src]# vim /etc/keepalived/keepalived.conf

? vrrp_script chk_nginx {

? ? script "/etc/keepalived/nginx_check.sh"

? ? interval 2

? ? weight -20

? }

? vrrp_instance VI_1 {

? ? ? state BACKUP

? ? ? interface ens192

? ? ? virtual_router_id 51

? ? ? mcast_src_ip 16.155.197.42

? ? ? priority 90

? ? ? advert_int 1

? ? ? authentication {

? ? ? ? ? auth_type PASS

? ? ? ? ? auth_pass 1111

? ? ? }

? ? ? track_script {

? ? ? ? ? chk_nginx

? ? ? }

? ? ? virtual_ipaddress {

? ? ? ? ? 16.155.197.100

? ? ? }?

? }

3.3 編寫(xiě)nginx狀態(tài)監(jiān)測(cè)腳本

[root@example01 keepalived]# vim /etc/keepalived/nginx_check.sh

? #!/bin/bash

? A=`ps -C nginx? --no-header |wc -l`

? if [ $A -eq 0 ];then

? ? ? ? ? /usr/local/nginx/sbin/nginx

? ? ? ? ? sleep 2

? ? ? ? ? if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

? ? ? ? ? ? ? ? ? killall keepalived

? ? ? ? ? fi

? fi

腳本要求:如果 nginx 停止運(yùn)行,嘗試啟動(dòng),如果無(wú)法啟動(dòng)則殺死本機(jī)的 keepalived 進(jìn)程, keepalied將虛擬 ip 綁定到 BACKUP 機(jī)器上。

3.4 保存腳本,賦予執(zhí)行權(quán)限

[root@example01 keepalived]# chmod +x /etc/keepalived/nginx_check.sh

? [root@example01 keepalived]# ll

? total 8

? -rw-r--r--. 1 root root 3602 Mar 27 23:46 keepalived.conf

? -rwxr-xr-x. 1 root root? 191 Mar 27 23:53 nginx_check.sh

3.5 啟動(dòng)keepalived

開(kāi)機(jī)啟動(dòng)

[root@example02 src]# chkconfig keepalived on

啟動(dòng)服務(wù)

[root@example01 keepalived]# service keepalived startStartingkeepalived:[? OK? ]

4.keepalived+nginx的高可用測(cè)試

4.1 查看服務(wù)器上的地址

查看MASTER的地址:

[root@example01 keepalived]# ip add?

1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000

? ? link/ether 00:50:56:b0:2e:17 brd ff:ff:ff:ff:ff:ff

? ? inet 16.155.199.223/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192

? ? ? valid_lft 113604sec preferred_lft 113604sec

? ? inet 16.155.197.100/32 scope global ens192? ? ??# 注意,此時(shí)MASTER上存在一個(gè)VIP

? ? ? valid_lft forever preferred_lft forever

? ? inet6 fe80::c453:9e3c:8efd:1d73/64 scope link noprefixroute

? ? ? valid_lft forever preferred_lft forever


查看BACKUP的地址:

[root@example02 src]# ip add?

1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000

? ? link/ether 00:50:56:b0:3b:c4 brd ff:ff:ff:ff:ff:ff

? ? inet 16.155.197.42/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192

? ? ? valid_lft 110123sec preferred_lft 110123sec

? ? inet6 fe80::c5d2:fa65:bf3c:e8a8/64 scope link tentative dadfailed

? ? ? valid_lft forever preferred_lft forever

? ? inet6 fe80::4440:cea8:b176:daa2/64 scope link tentative dadfailed

? ? ? valid_lft forever preferred_lft forever

? ? inet6 fe80::c865:c307:7968:5daf/64 scope link tentative dadfailed

? ? ? valid_lft forever preferred_lft forever

瀏覽器訪(fǎng)問(wèn):http://16.155.197.100。



4.2 關(guān)閉MASTER上的nginx,keepalived會(huì)將它重新啟動(dòng)

[root@example01 keepalived]# /usr/local/nginx/sbin/nginx -s stop

4.3 關(guān)閉MASTER上的keepalived,VIP會(huì)切換到BACKUP上

[root@example01 keepalived]# service keepalived stopStoppingkeepalived:[? OK? ]

4.4 驗(yàn)證VIP的漂移

驗(yàn)證方法1:通過(guò)ip add查看VIP的漂移

[root@example01 keepalived]# ip add

1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000

? ? link/ether 00:50:56:b0:2e:17 brd ff:ff:ff:ff:ff:ff

? ? inet 16.155.199.223/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192

? ? ? valid_lft 113216sec preferred_lft 113216sec

? ? inet6 fe80::c453:9e3c:8efd:1d73/64 scope link noprefixroute

? ? ? valid_lft forever preferred_lft forever

?[root@example02 src]# ip add?

1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 forever2: ens192: mtu 1500 qdisc mq state UP group default qlen 1000

? ? link/ether 00:50:56:b0:3b:c4 brd ff:ff:ff:ff:ff:ff

? ? inet 16.155.197.42/21 brd 16.155.199.255 scope global noprefixroute dynamic ens192

? ? ? valid_lft 110002sec preferred_lft 110002sec

? ? inet 16.155.197.100/32 scope global ens192? ? #注意此時(shí)vip漂浮到backup機(jī)器上了

? ? ? valid_lft forever preferred_lft forever

? ? inet6 fe80::c5d2:fa65:bf3c:e8a8/64 scope link tentative dadfailed

? ? ? valid_lft forever preferred_lft forever

? ? inet6 fe80::4440:cea8:b176:daa2/64 scope link tentative dadfailed

? ? ? valid_lft forever preferred_lft forever

? ? inet6 fe80::c865:c307:7968:5daf/64 scope link tentative dadfailed

? ? ? valid_lft forever preferred_lft forever


驗(yàn)證方法2:瀏覽器訪(fǎng)問(wèn):http://16.155.197.100。

刷新頁(yè)面,顯示“Welcome to nginx!Server02”,表示已經(jīng)VIP已經(jīng)漂移到了BACKUP服務(wù)器上

高可用

到這里,整個(gè)部署就已經(jīng)完成了!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容