Keepalived概述:
Keepalived的作用是检测服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器.
keepalived实现lvs高可用并负载均衡web服务:
准备工作:
2台keepalived节点为: node1.samlee.com node2.samlee.com
2台realserver服务为: node3.samlee.com node4.samlee.com
1、配置RSserver过程如下(node3.samlee.com\node4.samlee.com):
(1)部署web服务(使用nginx配置)
# yum -y install nginx-1.10.0-1.el6.ngx.x86_64.rpm
(2)禁用防火墙及主机名修改
# service iptables stop
# chkconfig iptables off
(3)配置路由转发及转发模块配置
# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
--添加路由条目
# ifconfig lo:0 172.16.100.99 netmask 255.255.255.255 broadcast 172.16.100.99 up
# route add -host 172.16.100.99 dev lo:0
2、配置keepalived服务器
(1)安装ipvsadm负载均衡管理器
# yum -y install ipvsadm
(2)安装keepalived
# yum -y install keepalived
# chkconfig keepalived on
(3)配置keepalived默认初始状态如下:(主节点:master状态)
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL #物理路由id,一般指定为本机的hostname
}
vrrp_instance VI_1 { #vrrp实例配置段
state MASTER #指定keepalived服务运行状态
interface eth0 #指定keepalived的心跳口
virtual_router_id 51 #指定虚拟路由id(1~255),同一vrrp实例的主备keepalived必须配置为一样
priority 101 ##指定起始优先级,优先级高的会成为master
advert_int 1 #vrrp通告的发送间隔
authentication { #配置通过密码认证
auth_type PASS
auth_pass keepalivedpass
}
virtual_ipaddress {
172.16.100.99 dev eth0 label eth0:0 #定义VIP
}
}
(4)配置keepalived默认初始状态如下:(备节点:backuo状态)
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass keepalivedpass
}
virtual_ipaddress {
172.16.100.99 dev eth0 label eth0:0
}
}
(5)调用周期性检测down文件是否存在来控制keepalived服务的主备切换
在keepalived.conf添加如下内容:
全局配置的段下添加如下:
vrrp_script chk_downfile { #服务状态检测脚本配置段
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #通过周期性检测down文件是否存在来控制keepalived服务的主备切换
interval 1 #指定检测间隔为1秒
weight -2 #指定检测失败时,优先级减2;检测的成功或失败是由script后面指定的命令或脚本执行返回的状态码决定的,0表示成功,非0表示失败
}
在vrrp_instance VI_1 ##vrrp实例配置段添加如下:
track_script {
chk_downfile #通过指定上面定义监控脚本来监控服务状态,以完成主备切换
}