Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web 服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人 工做的只是修复故障的web服务器。
Layer3,4&7工作在IP/TCP协议栈的IP层,TCP层,及应用层,原理分别如下:
Layer3:Keepalived使用Layer3的方式工作式时,Keepalived会定期向服务器群中的服 务器发送一个ICMP的数据包(既我们平时用的Ping程序),如果发现某台服务的IP地址没有激活,Keepalived便报告这台服务器失效,并将它 从服务器群中剔除,这种情况的典型例子是某台服务器被非法关机。Layer3的方式是以服务器的IP地址是否有效作为服务器工作正常与否的标准。
Layer4:如果您理解了Layer3的方式,Layer4就容易了。Layer4主要以TCP端口的状态来决定 服务器工作正常与否。如web server的服务端口一般是80,如果Keepalived检测到80端口没有启动,则Keepalived将把这台服务器从服务器群中剔除。
Layer7:Layer7就是工作在具体的应用层了,比Layer3,Layer4要复杂一点,在网络上占用的带 宽也要大一些。Keepalived将根据用户的设定检查服务器程序的运行是否正常,如果与用户的设定不相符,则Keepalived将把服务器从服务器 群中剔除。
keepavlied起初专门为LVS设计的,专门用来监控LVS集群系统中各个服务节点的状态,后来加入VRRP功能,因此除了配合LVS服务外,也可以作为其他服务(nginx,haproxy)的高可用软件。
keepalived核心功能:
healthcheck 健康检查(针对RealServer)
failover 高可用故障转移(主备)
keepalived的安装,主和备都需要安装
为了安装顺利,最好也先给内核路径做一个软连接到/usr/src/linux
[root@localhost ~]
# ln -s /usr/src/kernels/2.6.32-573.8.1.el6.i686/ /usr/src/linux
[root@localhost ~]
# cd /usr/local/src/
[root@localhost src]
# wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
[root@localhost src]# tar xf keepalived-1.1.19.tar.gz
[root@localhost src]
# cd keepalived-1.1.19
[root@localhost keepalived-1.1.19]
# ./configure
configure执行成功的结果:有3个Yes;支持IPVS框架,IPVS的同步支持,VRRP的框架支持;
Keepalived configuration
------------------------
Keepalived version : 1.1.19
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
Use VRRP Framework : Yes
Use Debug flags : No
安装keepalived
[root@localhost keepalived-1.1.19]
# make
[root@localhost keepalived-1.1.19]
# make install
配置keepalived
[root@localhost keepalived-1.1.19]
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ -ap
[root@localhost keepalived-1.1.19]
# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ -ap
[root@localhost keepalived-1.1.19]
# mkdir /etc/keepalived -p
[root@localhost keepalived-1.1.19]
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ -ap
[root@localhost keepalived-1.1.19]
# cp /usr/local/sbin/keepalived /usr/sbin/ -ap
[root@localhost keepalived-1.1.19]
# /etc/init.d/keepalived start
主MASTER、备backup上配置文件:/etc/keepalived/keepalived.conf
主master配置:
global_defs {
notification_email {
xxxxxx@163.com
}
notification_email_from yyyyyy@163.com
smtp_server 192.168.0.100
smtp_connect_timeout 30
router_id IP_ROUTER
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.253
/24
}
备 backup 的配置:
global_defs {
notification_email {
xxxxxx@163.com
}
notification_email_from yyyyyy@163.com
smtp_server 192.168.0.100
smtp_connect_timeout 30
router_id IP_ROUTER
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.253
/24
}
主、备启动keepalived,主绑定VIP地址,主停止keepalived服务,备机马上接管VIP;
本文出自 “Keyman” 博客,谢绝转载!
原文地址:http://keyman.blog.51cto.com/9807984/1716392