接上文 ? 學習日記4:nginx負載均衡(二)2012.01.08
......
到此,整體工作還差一個備用nginx proxy,其中涉及到keepalived,做心跳。
?
黃線以上做完了。
?
?
本次主要做的是1加一臺從nginx_proxy ,暫且命令為front2(為從)。之前的nginx_proxy命令為front(為主)
?
1 在主nginx上配置keepalived。
cd /usr/local/src/ wget http: // keepalived.org/software/keepalived-1.2.7.tar.gz tar -xzvf keepalived- 1.2 . 7 . tar .gz cd keepalived - 1.2 . 7 / . /configure --prefix=/usr/local/keepalived
這時候會報錯的!
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
缺少gcc,安裝下面擴展
yum install gcc gcc -c++ autoconf automake
繼續編譯,下面還會報錯,類似這樣的錯誤。
?
make
: *** No targets specified and no makefile found. Stop.
configure: error: ! OpenSSL is not properly installed on your system. configure: error: !
?
?對此,我們安裝以下擴展即可解決。
yum -y install openssl-devel
yum -y install popt-devel
?
在編譯
./configure --prefix=/usr/local/keepalived
?
這時候,會出現下面的結果,這個結果就表明是ok的。到此你的keepalived安裝是成功的!
Keepalived configuration ------------------------ Keepalived version : 1.2 . 7 Compiler : gcc Compiler flags : -g - O2 Extra Lib : -lpopt -lssl - lcrypto Use IPVS Framework : Yes IPVS sync daemon support : Yes IPVS use libnl : No Use VRRP Framework : Yes Use VRRP VMAC : Yes SNMP support : No Use Debug flags : No
?
繼續下面的操作
?
make make install cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ mkdir /etc/ keepalived cd /etc/ keepalived vim keepalived.conf
?
keepalived.conf配置文件比較重要
front1 :主nginx的配置文件如下:
! Configuration File for keepalived global_defs { router_id LVS_01 } vrrp_instance VI_1 { state MASTER #master 主nginx interface eth1 virtual_router_id 51 mcast_src_ip 192.168 . 65.133 #本機ip地址也即主nginx地址 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168 . 65.128 #vip 虛擬出來的ip地址 要求在內網中未被使用 } }
?
到此,主nginx配置完成。同樣的操作,在從nginx上也配置一遍。
不過從nginx的配置文件如下:
[root@localhost nginx]# vim /etc/keepalived/ keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_01 } vrrp_instance VI_1 { state BACKUP #slave interface eth2 virtual_router_id 51 mcast_src_ip 192.168 . 65.137 #slave ip priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168 . 65.128 #vip } }
?
重啟兩邊的keepalived服務:
[root@localhost conf.d]# service keepalived restart
Stopping keepalived: [FAILED]
Starting keepalived: [ OK ]
主nginx上,用
[root@localhost conf.d]# ip a 1 : lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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 inet6 :: 1 / 128 scope host valid_lft forever preferred_lft forever 2 : eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 00 :0c: 29 : 20 :e9: 20 brd ff:ff:ff:ff:ff:ff inet 192.168 . 65.133 / 24 brd 192.168 . 65.255 scope global eth1 inet 192.168 . 65.128 / 32 scope global eth1 inet6 fe80::20c:29ff:fe20:e920 / 64 scope link valid_lft forever preferred_lft forever
?到此,我們的vip算是真正的綁定上了。
查看keepalived的日志:
root@localhost conf.d]# tail -f /var/log/ messages Jan 9 18 : 53 : 06 localhost Keepalived_healthcheckers[ 3355 ]: Using LinkWatch kernel netlink reflector... Jan 9 18 : 53 : 06 localhost Keepalived_vrrp[ 3356 ]: Configuration is using : 34824 Bytes Jan 9 18 : 53 : 06 localhost Keepalived_vrrp[ 3356 ]: Using LinkWatch kernel netlink reflector... Jan 9 18 : 53 : 06 localhost Keepalived_vrrp[ 3356 ]: VRRP sockpool: [ifindex( 2 ), proto( 112 ), fd( 11 , 12 )] Jan 9 18 : 53 : 06 localhost Keepalived_vrrp[ 3356 ]: VRRP_Instance(VI_1) Transition to MASTER STATE Jan 9 18 : 53 : 07 localhost Keepalived_vrrp[ 3356 ]: VRRP_Instance(VI_1) Entering MASTER STATE Jan 9 18 : 53 : 07 localhost Keepalived_vrrp[ 3356 ]: VRRP_Instance(VI_1) setting protocol VIPs. Jan 9 18 : 53 : 07 localhost avahi-daemon[ 1765 ]: Registering new address record for 192.168 . 65.128 on eth1.IPv4. Jan 9 18 : 53 : 07 localhost Keepalived_healthcheckers[ 3355 ]: Netlink reflector reports IP 192.168 . 65.128 added Jan 9 18 : 53 : 07 localhost Keepalived_vrrp[ 3356 ]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168 . 65.128 Jan 9 18 : 53 : 12 localhost Keepalived_vrrp[ 3356 ]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168 . 65.128
從nginx上,同樣
[root@localhost nginx]# ip a 1 : lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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 inet6 :: 1 / 128 scope host valid_lft forever preferred_lft forever 2 : eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 00 :0c: 29 :a7: 81 :c7 brd ff:ff:ff:ff:ff:ff inet 192.168 . 65.137 / 24 brd 192.168 . 65.255 scope global eth2 inet6 fe80::20c:29ff:fea7:81c7 / 64 scope link valid_lft forever preferred_lft forever
我們發現并沒有綁定vip 128
同時;
[root@localhost nginx]# tail -f /var/log/ messages Jan 9 19 : 16 : 25 localhost Keepalived_healthcheckers[ 22420 ]: Registering Kernel netlink reflector Jan 9 19 : 16 : 25 localhost Keepalived_healthcheckers[ 22420 ]: Registering Kernel netlink command channel Jan 9 19 : 16 : 25 localhost Keepalived_vrrp[ 22421 ]: Opening file ' /etc/keepalived/keepalived.conf ' . Jan 9 19 : 16 : 25 localhost Keepalived_healthcheckers[ 22420 ]: Opening file ' /etc/keepalived/keepalived.conf ' . Jan 9 19 : 16 : 25 localhost Keepalived_vrrp[ 22421 ]: Configuration is using : 34822 Bytes Jan 9 19 : 16 : 25 localhost Keepalived_healthcheckers[ 22420 ]: Configuration is using : 4821 Bytes Jan 9 19 : 16 : 25 localhost Keepalived_vrrp[ 22421 ]: Using LinkWatch kernel netlink reflector... Jan 9 19 : 16 : 25 localhost Keepalived_vrrp[ 22421 ]: VRRP_Instance(VI_1) Entering BACKUP STATE Jan 9 19 : 16 : 25 localhost Keepalived_vrrp[ 22421 ]: VRRP sockpool: [ifindex( 2 ), proto( 112 ), fd( 11 , 12 )] Jan 9 19 : 16 : 25 localhost Keepalived_healthcheckers[ 22420 ]: Using LinkWatch kernel netlink reflector...
?
貌似從nginx似乎出了什么問題,先不著急。
這時候我們停止掉主nginx的服務,再看看效果怎么樣!
killall
nginx
再觀察從nginx的狀態變化
[root@localhost nginx]# tail -f /var/log/ messages Jan 9 19 : 16 : 25 localhost Keepalived_healthcheckers[ 22420 ]: Registering Kernel netlink reflector Jan 9 19 : 16 : 25 localhost Keepalived_healthcheckers[ 22420 ]: Registering Kernel netlink command channel Jan 9 19 : 16 : 25 localhost Keepalived_vrrp[ 22421 ]: Opening file ' /etc/keepalived/keepalived.conf ' . Jan 9 19 : 16 : 25 localhost Keepalived_healthcheckers[ 22420 ]: Opening file ' /etc/keepalived/keepalived.conf ' . Jan 9 19 : 16 : 25 localhost Keepalived_vrrp[ 22421 ]: Configuration is using : 34822 Bytes Jan 9 19 : 16 : 25 localhost Keepalived_healthcheckers[ 22420 ]: Configuration is using : 4821 Bytes Jan 9 19 : 16 : 25 localhost Keepalived_vrrp[ 22421 ]: Using LinkWatch kernel netlink reflector... Jan 9 19 : 16 : 25 localhost Keepalived_vrrp[ 22421 ]: VRRP_Instance(VI_1) Entering BACKUP STATE Jan 9 19 : 16 : 25 localhost Keepalived_vrrp[ 22421 ]: VRRP sockpool: [ifindex( 2 ), proto( 112 ), fd( 11 , 12 )] Jan 9 19 : 16 : 25 localhost Keepalived_healthcheckers[ 22420 ]: Using LinkWatch kernel netlink reflector... Jan 9 19 : 17 : 33 localhost Keepalived_vrrp[ 22421 ]: VRRP_Instance(VI_1) Transition to MASTER STATE Jan 9 19 : 17 : 34 localhost Keepalived_vrrp[ 22421 ]: VRRP_Instance(VI_1) Entering MASTER STATE Jan 9 19 : 17 : 34 localhost Keepalived_vrrp[ 22421 ]: VRRP_Instance(VI_1) setting protocol VIPs. Jan 9 19 : 17 : 34 localhost Keepalived_vrrp[ 22421 ]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168 . 65.128 Jan 9 19 : 17 : 34 localhost avahi-daemon[ 1810 ]: Registering new address record for 192.168 . 65.128 on eth2.IPv4. Jan 9 19 : 17 : 34 localhost Keepalived_healthcheckers[ 22420 ]: Netlink reflector reports IP 192.168 . 65.128 added Jan 9 19 : 17 : 39 localhost Keepalived_vrrp[ 22421 ]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168 . 65.128
從第33秒后發現,備用nginx已經轉為master狀態。
再看
[root@localhost nginx]# ip a 1 : lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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 inet6 :: 1 / 128 scope host valid_lft forever preferred_lft forever 2 : eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 00 :0c: 29 :a7: 81 :c7 brd ff:ff:ff:ff:ff:ff inet 192.168 . 65.137 / 24 brd 192.168 . 65.255 scope global eth2 inet 192.168 . 65.128 / 32 scope global eth2 inet6 fe80::20c:29ff:fea7:81c7 / 64 scope link valid_lft forever preferred_lft forever
發現沒有?vip飄過來了!
?
針對Keepalived的不足,用check_nginx.sh來監控nginx進程,實現真正意義上的負載均衡高可用.
此腳本思路其實也很簡單,即放置在后臺一直監控nginx進程;如進程消失,嘗試重啟nginx,
如是失敗則立即停掉本機的keepalived服務,讓另一臺負載均衡器接手.
vi /shell/nginx_pid. sh ################################################### # !/bin/ bash while : do nginxpid =` ps -C nginx --no-header | wc - l` if [ $nginxpid -eq 0 ] then /usr/local/nginx- 0.8 . 53 /sbin/ nginx sleep 5 if [ $nginxpid -eq 0 ] then /etc/init.d/ keepalived stop fi fi sleep 5 done ###################################################
?
?
我們仍然可以繼續訪問我們的站點。至此。故障點切換宣告結束。
?
?
?
?
【原創】學習日記5:nginx負載均衡(三)2012.01.10 nginx+keepalievd,實現負載均衡和故障點切換。keepalived雙機熱備。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
