下载: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