您的位置:首页 > 运维架构 > Nginx

keepalived架设简单高可用的nginx的web服务器   ----那些你不知道的秘密 推荐

2013-10-01 17:57 696 查看
keepalived架设简单高可用的nginx的web服务器----那些你不知道的秘密
如果負載均衡軟件不使用LVS的話,那麼keepalived的配置是相當的簡單的,只需要配置好MASTER和SLAVE的vrrp相關配置就可以了,後端realServer的偵測就可以交給負載均衡軟件去做,比如我使用的負載均衡軟件是haproxy,個人認為比LVS更高效、更省資源、配置更簡單、條理更清晰。
下面來說說我在配置keepalived和nginx過程中遇到的一些問題,之所以會出現很多問題,關鍵就在於keepalived沒有配置文件查錯的機制,不管你的配置文件寫成了啥樣,你照樣可以成功啟動keepalived,它不會給你任務錯誤提示,就像它沒有出錯一樣,你查看進程時也不會覺得它異常,和普通的正常的進程一模一樣,因此一旦配置文件配置錯誤就很要命了。
nginx的配置就沒啥好說的了,重點講講keepalived。
1,环境
test2为主ngin服务器,test3为辅的nginx服务器

test2 eth2:192.168.46.132
192.168.46.132
test3 eth2:192.168.46.133
192.168.46.133

2、安裝:1
如果你使用LVS作為負載均衡器的話,需要讓編譯程序能找到你的linux內核所在目錄,要帶 --with-kernel-dir=/usr/src/linux參數編譯,因此你需要先安裝內核開發包:
with-kernel-dir=/usr/src/linux參數編譯,因此你需要先安裝內核開發包:
yum install kernel-devel
這是CENTOS的安裝方法。
接著為內核源文件目錄做一個軟鏈接:
ln -s /usr/src/kernels/`uname -r` /usr/src/linux
現在可以開始編譯了:
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/linux
with-kernel-dir=/usr/src/linux
如果不使用LVS,就不需要帶--with-kernel-dir參數,我用的是nginx,因此沒有指定此參數。
with-kernel-dir參數,我用的是nginx,因此沒有指定此參數。
configure結束,開始make時,出现了以下錯誤:
/usr/include/stdint.h:41: error: conflicting types for‘int64_t’
include/stdint.h:41: error: conflicting types for
include/stdint.h:41: error: conflicting types for‘int64_t’
/usr/src/linux/include/linux/types.h:126: error: previous declaration of ‘int64_t’ was here
include/linux/types.h:126: error: previous declaration of ‘int64_t’ was here
/usr/include/stdint.h:56: error: conflicting types for‘uint64_t’
include/stdint.h:56: error: conflicting types for
include/stdint.h:56: error: conflicting types for‘uint64_t’
/usr/src/linux/include/linux/types.h:124: error: previous declaration of ‘uint64_t’ was here
include/linux/types.h:124: error: previous declaration of ‘uint64_t’ was here
In file included from /usr/include/stdlib.h:438,
include/stdlib.h:438,
在源目錄裡找到keepalived/libipvs-2.6/ip_vs.h文件,編輯它,把 #include<linux/types.h> 移动到 #include<sys/types.h> 的下面即可,再次make正常,make install安裝完畢。
2.6/ip_vs.h文件,編輯它,把 #include
<linux/types.h> 移动到 #include
2.6/ip_vs.h文件,編輯它,把 #include<linux/types.h> 移动到 #include<sys/types.h> 的下面即可,再次make正常,make install安裝完畢。

3、配置
3.1主服务器test2的keepalived的配置

/etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server 127.0.0.1
127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/root/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interfaceeth2
virtual_router_id 51
mcast_src_ip 192.168.46.132
192.168.46.132
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.46.200/24
}
track_script {
chk_http_port
}
}
3.2辅助nginx服务器keepalived的配置
[root@test3 init.d]# vim /etc/keepalived/keepalived.conf
!Configuration File forkeepalived
forkeepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server 127.0.0.1
127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/root/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interfaceeth2
virtual_router_id 51
mcast_src_ip 192.168.46.133
192.168.46.133
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.46.200/24
}
track_script {
chk_http_port
}
}
~


4,监控nginx的脚本
chk_http_port腳本內容如下:
[root@test2 init.d]# vim /root/nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if[ $A -eq 0];then
[ $A -eq 0
if[ $A -eq 0];then
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
sleep 3
if[ `ps -C nginx --no-header |wc -l` -eq 0];then
[ `ps -C nginx --no-header |wc -l` -eq 0
if[ `ps -C nginx --no-header |wc -l` -eq 0];then
killall keepalived
fi
fi

5,注意事項:
1、第一行!開頭表示是一行註釋,keepalived配置文件的註釋行都是以!或#開頭的。2、vrrp_scriptchk_http_port是指定一個vrrp檢測腳本,當切換到MASTER時,自動執行腳本中指定的程序,要特別注意的是vrrp_scriptchk_http_port與後面跟著的{之間要至少留有一個空格,否則腳本不會執行,我為了這個問題糾結了2小時!keepalived的配置文件中所有的{都要與之前的字符至少間隔一個空格位置。就因為keepalived不檢測任何配置問題,因此如果不知道的話就相當的杯具了。。3、track_script{,這是執行vrrp_script指令所指定的腳本,要注意的是它必須放在virtual_ipaddress配置的后面,意思就是只有當VIP正常啟動生效後才能執行track_script中指定的腳本,其原因是我的負載均衡器使用的是haproxy,在nginx配置文件中設置的綁定IP就是這個VIP,而我的檢測腳本chk_http_port就是檢測nginx狀態並根據條件啟動它,因此如果track_script放到前面了就會發生綁定的VIP還沒有生效就試圖啟動nginx,這當然是不可能成功的了,再看chk_http_port內容--當找不到nginx進程時,就停止keepalived,這樣就導致了nginx啟動不起來,連累了keepalived也被停掉,MASTER被SLAVE接管,要命的是SLAVE上的配置和MASTER是一樣的,結果就是不管MASTER還是SLAVE,VIP都無法啟動。這個問題也讓我糾結了4個多小時。。根據這個經驗,一切檢測任務的配置都最好寫在VIP啟動配置的後面,而不要寫到前面!4、從機keepalived進程啟動時,chk_http_port腳本會執行失敗,這是因為主機佔用了VIP,從機上的nginx得不到這個IP而啟動失敗,當從機接管MASTER時,雖然VIP飄過來了,但chk_http_port是不會執行的,因此nginx不會啟動,這個原因我想應該是vrrp_script所定義的腳本只是在keepalived啟動時才執行的吧,雖然名字是以vrrp開頭。因此最好先讓從機接管MASTER,將VIP飄過來,再手動啟動nginx,這樣不管MASTER飄到主機還是從機,都能正常執行負載均衡任務了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息