Nginx Keepalived 負載均衡

2021-09-21 20:54:37 字數 4828 閱讀 1105

喜歡搞nginx,這兩天研究了一下nginx的負載配置

nginx平台搭建:nginx+mysql+php-fpm搭建高效能nginx平台

單nginx負載均衡:nginx 負載均衡 配置全過程

熟悉上面的配置之後,今天我再來搞nginx + keepalived 的配置,具體拓撲如下:

一、安裝keepalived

ln -s /usr/src/kernels/2.6.18-194.el5-i686/ /usr/src/linux

tar zxvf keepalived-1.1.15.tar.gz 

cd keepalived-1.1.15

./configure --prefix=/usr/local/keepalived

make

make install

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/   #主配置檔案

cp -v /usr/local/keepalived/sbin/keepalived /usr/sbin/

service keepalived start|stop           #啟動、停止服務

二、配置keepalived

/etc/keepalived/keepalived.conf

! configuration file for keepalived

global_defs

notification_email_from 

[email protected]

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id lvs_devel

} vrrp_script chk_nginx

vrrp_instance vi_1

track_script

virtual_ipaddress

} vrrp_instance vi_2

virtual_ipaddress }

global_defs 是全域性配置,定義郵件報警,我無視它了。

vrrp_script 定義乙個監控指令碼,作用:發現nginx掛了,馬上關掉keepalived

vrrp_instance 定義乙個虛擬ip,對外服務的,其中還有個track_script ,這個就是呼叫剛才定義的監控指令碼

另外一台前端nginx的keepalived配置如下:

! configuration file for keepalived

global_defs

notification_email_from [email protected]

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id lvs_devel

} vrrp_script chk_nginx

vrrp_instance vi_1

virtual_ipaddress

} vrrp_instance vi_2

track_script

virtual_ipaddress }

另外監控指令碼check_http.sh可以自己發揮,原則就是發現nginx掛了就殺掉keepalived就行,寫法多種

pid=`ps -c nginx --no-header | wc -l`

if [ $pid -eq 0 ]

then

/usr/local/nginx/sbin/nginx

sleep 5

newpid=`ps -c nginx --no-header | wc -l`

if [ $newpid -eq 0 ]

then

killall keepalived

fifi

nginx的配置就不多說

啟動服務

/usr/local/nginx/sbin/nginx

service keepalived start

ps -aef | grep keepalived

root     28742  3288  0 03:21 pts/1    00:00:00 grep keepalived

root     31674     1  0 oct31 ?        00:00:00 keepalived -d

root     31676 31674  0 oct31 ?        00:00:00 keepalived -d

root     31677 31674  0 oct31 ?        00:00:02 keepalived -d

三、測試

主nginx

ip add

2: eth0:

mtu 1500 qdisc pfifo_fast qlen 1000

link/ether 00:0c:29:e0:a3:bf brd ff:ff:ff:ff:ff:ff

inet 192.168.93.137/24 brd 192.168.93.255 scope global eth0

inet 192.168.93.144/32 scope global eth0

inet6 fe80::20c:29ff:fee0:a3bf/64 scope link 

valid_lft forever preferred_lft forever

備nginx

ip add

2: eth0:

mtu 1500 qdisc pfifo_fast qlen 1000

link/ether 00:0c:29:ee:a6:22 brd ff:ff:ff:ff:ff:ff

inet 192.168.93.138/24 brd 192.168.93.255 scope global eth0

inet 192.168.93.145/32 scope global eth0

inet6 fe80::20c:29ff:feee:a622/64 scope link 

valid_lft forever preferred_lft foreve

我把主nginx服務停掉之後:

主nginx

ip add

2: eth0:

mtu 1500 qdisc pfifo_fast qlen 1000

link/ether 00:0c:29:e0:a3:bf brd ff:ff:ff:ff:ff:ff

inet 192.168.93.137/24 brd 192.168.93.255 scope global eth0

inet6 fe80::20c:29ff:fee0:a3bf/64 scope link 

valid_lft forever preferred_lft forever

備nginx

ip add

2: eth0:

mtu 1500 qdisc pfifo_fast qlen 1000

link/ether 00:0c:29:ee:a6:22 brd ff:ff:ff:ff:ff:ff

inet 192.168.93.138/24 brd 192.168.93.255 scope global eth0

inet 192.168.93.145/32 scope global eth0

inet 192.168.93.144/32 scope global eth0

inet6 fe80::20c:29ff:feee:a622/64 scope link 

valid_lft forever preferred_lft forever

備機已經接管了主的ip,然後再把主機的服務恢復:

主nginx

ip add

2: eth0:

mtu 1500 qdisc pfifo_fast qlen 1000

link/ether 00:0c:29:e0:a3:bf brd ff:ff:ff:ff:ff:ff

inet 192.168.93.137/24 brd 192.168.93.255 scope global eth0

inet 192.168.93.144/32 scope global eth0

inet6 fe80::20c:29ff:fee0:a3bf/64 scope link 

valid_lft forever preferred_lft forever

備nginx

ip add

2: eth0:

mtu 1500 qdisc pfifo_fast qlen 1000

link/ether 00:0c:29:ee:a6:22 brd ff:ff:ff:ff:ff:ff

inet 192.168.93.138/24 brd 192.168.93.255 scope global eth0

inet 192.168.93.145/32 scope global eth0

inet6 fe80::20c:29ff:feee:a622/64 scope link 

valid_lft forever preferred_lft forever

配置完成!

lihuipeng

nginx keepalived學習目錄

keepalived權威指南 深入理解nginx nginx模組開發與架構解析第二版 nginx開發從入門到精通 keepalived工作原理和配置說明 1 nginx安裝和介紹 2 nginx的啟動和停止 3 nginx程序模型 4 nginx處理請求過程 5 nginx配置一 6 nginx配置...

nginx keepalived雙機熱備

採用nginx實現反向 配置集群,實現軟f5的功能。nginx.conf的配置如下 server 192.168.2.134 8082 upstream socologin server access log home soco share nginx logs access.log access ...

nginx keepalived雙機熱備

前提條件 nginx已經安裝ok了,具體可以參考 系統環境 centos 5.6 64位 實驗的伺服器如下 主伺服器ip 192.168.10.69 備伺服器ip 192.168.10.84 虛擬ip 192.168.10.99 一 keepalived安裝與及相關配置 yum install op...