下載:http://www.keepalived.org/html
what is keepalived?node
Keepalived is a routing software written in C. The main goal of this project is to provide simple and robust facilities for loadbalancing and high-availability to Linux system and Linux based infrastructures. Loadbalancing framework relies on well-known and widely used Linux Virtual Server (IPVS) kernel module providing Layer4 loadbalancing. Keepalived implements a set of checkers to dynamically and adaptively maintain and manage loadbalanced server pool according their health. On the other hand high-availability is achieved by VRRP protocol. VRRP is a fundamental brick for router failover. In addition, Keepalived implements a set of hooks to the VRRP finite state machine providing low-level and high-speed protocol interactions. Keepalived frameworks can be used independently or all together to provide resilient infrastructures.linux
下載源代碼nginx
./configure --prefix=/opt/keepalivedshell
報錯vim
configure: error: Popt libraries is requiredbash
安裝popt開發包服務器
yum install popt-devel網絡
安裝後configure輸出信息負載均衡
Keepalived configuration
------------------------
Keepalived version : 1.1.10
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : No
IPVS sync daemon support : No
Use VRRP Framework : Yes
Use LinkWatch : No
Use Debug flags : No
我上面的IPVS都是no,爲no不支持LVS.
看網上的說法
看網上說法要想IPVS爲yes,要帶上
--with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686/
我暫時沒有支持IPVS.
注意一點:啓動時不能文件用絕對路徑,不能用相對路徑
keepalived默認配置文件
! Configuration File for keepalived global_defs { notification_email { 通知email acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { #節點名標識,主要用於通知中
state MASTER
#配置爲主服務器
interface eth0
#通信網卡
virtual_router_id 100
#路由標識
priority 200
#優先級,0-254
advert_int 5
#通知間隔,實際部署時能夠設置小一點,減小延時
authentication {
auth_type PASS
auth_pass 123456
#驗證密碼,用於通信主機間驗證
}
virtual_ipaddress { 192.168.200.16 192.168.200.17 192.168.200.18 } } virtual_server 192.168.200.100 443 { delay_loop 6 lb_algo rr lb_kind NAT nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.201.100 443 { weight 1 SSL_GET { url { path / digest ff20ad2481f97b1754ef3e12ecd3a9cc } url { path /mrtg/ digest 9b3a0c85a887a256d6939da88aabd8cd } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } virtual_server 10.10.10.2 1358 { delay_loop 6 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP sorry_server 192.168.200.200 1358 real_server 192.168.200.2 1358 { weight 1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl2/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl3/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.200.3 1358 { weight 1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334c } url { path /testurl2/test.jsp digest 640205b7b0fc66c1ea91c463fac6334c } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } virtual_server 10.10.10.3 1358 { delay_loop 3 lb_algo rr lb_kind NAT nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.200.4 1358 { weight 1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl2/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl3/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.200.5 1358 { weight 1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl2/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl3/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
接下來就是配置了,很簡單,直接上配置文件
先是主服務器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
global_defs
{
notification_email
#通知email,根據實際狀況配置
{
admin@example.com
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id node1
#節點名標識,主要用於通知中
}
vrrp_instance VI_NODE {
state MASTER
#配置爲主服務器
interface eth0
#通信網卡
virtual_router_id 100
#路由標識
priority 200
#優先級,0-254
advert_int 5
#通知間隔,實際部署時能夠設置小一點,減小延時
authentication {
auth_type PASS
auth_pass 123456
#驗證密碼,用於通信主機間驗證
}
virtual_ipaddress {
192.168.1.206
#虛擬ip,能夠定義多個
}
}
|
(個人啓動master的日誌
keepalived -f /opt/keepalived/etc/keepalived1.conf 切記,配置文件要用絕對路徑,不能用相對路徑,不然不正常。
Keepalived_vrrp: Registering gratutious ARP shared channel Keepalived_vrrp: Configuration is using : 61723 Bytes Keepalived_vrrp: VRRP_Instance(VI_NODE) Transition to MASTER STATE
我最開始用的是相對路徑,用ip a沒看到設置的虛擬ip.
用來絕對路徑後,啓動正常了,ip a輸出
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 1c:87:2c:60:14:26 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.50/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.206/32 scope global eth0 (咱們配置的vip,我把配置文件中的virtual_address改爲了:192.168.1.206 dev eth0 scope global)
inet6 fe80::1e87:2cff:fe60:1426/64 scope link
valid_lft forever preferred_lft forever
能夠看到,咱們設定的虛擬 IP 192.168.1.206 已經掛接在網卡 eth0上。
這個時候咱們在局域網內ping能夠ping通。
ping 192.168.1.206
咱們在nginx中增長一個server
server{
listen 9889;
server_name 192.168.1.206;
location / {
root /usr/local/www;
}
}
這時候在咱們電腦上訪問http://192.168.1.206:9889/是能夠訪問的。
)
接下是從服務器設置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
global_defs {
notification_email {
admin@example.com
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id node2
}
vrrp_instance VI_NODE {
state BACKUP
#與主服務器對應
interface eth0
#從服務器的通訊網卡
virtual_router_id 100
#路由標識,和主服務器相同
priority 100
#優先級,小於主服務器便可
advert_int 5
#這裏是接受通知間隔,與主服務器要設置相同
authentication {
auth_type PASS
auth_pass 123456
#驗證密碼,與主服務器相同
}
virtual_ipaddress {
192.168.1.206
#虛擬IP,也要和主服務器相同
}
}
|
上面的設置是最基礎的設置,實現的功能是如果主服務器的Keepalived中止服務(通常狀況下服務器宕機),則將虛擬IP切換至從服務器,主服務器恢復後重新切換回主服務器。
(
啓動從服務器後,咱們用ip a輸出
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d8:ef:7e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.156/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fed8:ef7e/64 scope link
valid_lft forever preferred_lft forever
能夠看到,虛擬 IP沒有掛接在網卡 eth0上。
咱們測試功能,
停止主服務器keepalive,咱們在客戶端ping 192.168.1.206 依然可以ping通。網址能訪問。
這時咱們在從服務器上用ip a看到
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d8:ef:7e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.156/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.206/32 scope global eth0
inet6 fe80::20c:29ff:fed8:ef7e/64 scope link
valid_lft forever preferred_lft forever
而後咱們又開啓主服務,這時在從服務器上面沒有看到 inet 192.168.1.206/32 scope global eth0這行
說明從服務器又沒有綁定虛擬ip 192.168.1.206了。
)
可是不少狀況下咱們面臨的處境是nginx掛掉了,而這個時候Keepalived就不能發揮做用,這時候就須要咱們來改良下Keepalived了。經過向Keepalived添加一個自定義腳原本監控neginx的運行狀態,若是nginx進程結束,則kill Keepalived進程,以此來達到主從服務器的切換功能。
咱們在修改上面配置的主服務器的配置文件,在中間添加腳本實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
global_defs {
notification_email {
admin@example.com
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id nginx_master
}
vrrp_script chk_http_port {
script
"/usr/local/keepalived/nginx.sh"
#在這裏添加腳本連接
interval 3
#腳本執行間隔
weight 2
#腳本結果致使的優先級變動
}
vrrp_instance VI_NODE {
state MASTER
interface eth0
virtual_router_id 100
priority 200
advert_int 5
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_http_port
#添加腳本執行
}
virtual_ipaddress {
192.168.1.206
}
}
|
具體的配置能夠參考另外一篇文章Keepalived配置詳解
若是咱們使用了LVS+Keepalived集成,那麼keepalived能夠代替ipvsadm來配置LVS,能夠方便的經過配置就能夠搞定,這在另外一篇文章Keepalived+LVS配置詳解
修改完配置文件咱們寫咱們的上面配置的nginx.sh,固然咱們假定Nginx已經安裝完成
1
2
3
4
5
|
#!/bin/bash
A=`
ps
-C nginx --no-header |
wc
-l`
if
[ $A -
eq
0 ];
then
killall keepalived
fi
|
上面的腳本簡單的查看nginx進程是否存在,不存在就kill keepalived進程。
接下來咱們對上面的哦腳本修改一下,當腳本檢測到nginx沒有運行的時候會嘗試去啓動nginx以此,若是失敗則停掉keepalived進程
1
2
3
4
5
6
7
8
9
|
#!/bin/bash
A=`
ps
-C nginx –no-header |
wc
-l`
if
[ $A -
eq
0 ];
then
/usr/local/nginx/sbin/nginx
#nginx命令的路徑
sleep
3
if
[ `
ps
-C nginx --no-header |
wc
-l` -
eq
0 ];
then
killall keepalived
fi
fi
|
把腳本保存到上面配置的具體路徑(我這裏是/usr/local/keepalived),而後很重要的一步是修改腳本的執行權限
chmod +x nginx.sh
配置完成後就能夠運行看下效果了,分別在主從服務器上啓動nginx和keepalived
service keepalived start
啓動以後經過·ip a·命令查看主服務器的網絡信息,能夠看到在eth0網卡下生成了192.168.1.206這個虛擬ip,並可經過這個ip訪問到nginx
而後咱們關閉nginx的進程(若是配置了一次嘗試重啓那要注意下),而後咱們能夠經過ps -e查看keepalived進程是否關閉,正常狀況下查看網絡信息中,能夠看到eth0網卡下的虛擬ip已經解除,而後在從服務器的網絡信息中能夠看到從服務器的eth0網卡綁定了虛擬ip,經過這個ip就訪問到了從服務器的nginx去了,這是咱們從新啓動主服務器的nginx和keepalieved,咱們能夠發現虛擬ip就綁回到了主服務器。
這樣就實現了基本雙擊主從熱備功能了。
這裏注意下防火牆的問題,就是這問題困擾了我好久。找了一些資料纔將問題解決
由於Keepalived之間是經過組播來通知對方的是否存活,以及發送優先級的,而且經過組播來選舉MASTER的,而224.0.0.18就是經常使用的組播地址,防火牆開啓容許這個組播地址通訊就能夠了:
1.若是用的是默認防火牆,只須要添加:
iptables -I RH-Firewall-1-INPUT -d 224.0.0.18 -j ACCEPT
2.若是是本身用腳本設置的防火牆,須要添加以下規則
iptables -A OUTPUT -o eth0 -d 224.0.0.18 -j ACCEPT iptables -A OUTPUT -o eth0 -s 224.0.0.18 -j ACCEPT iptables -A INPUT -i eth0 -d 224.0.0.18 -j ACCEPT iptables -A INPUT -i eth0 -s 224.0.0.18 -j ACCEPT
原文連接(支持下樓主本身的博客^_^):http://thinkjet.me/linux-ha-keepalived-nginx.html
怎麼查看keepalvied的日誌?
tail /var/log/messages
配置中
virtual_ipaddress {
192.168.1.206
}
keepalived運行後,vip不生效的排查
參考:
http://my.oschina.net/zyc1016/blog/138574
http://www.linuxidc.com/Linux/2015-03/114981.htm
http://blog.chinaunix.net/uid-20749043-id-4034770.html