keepalived整合lvs集群实现
系统环境:rhel6.0-x86_64 iptables and selinux off
master_server:192.168.0.2
backup_server:192.168.0.3
realserver: 192.168.0.4
192.168.0.5
####在master和backup安装软件包
yum install ipvsadm kernel-devel libnl-devel popt-devel openssl-devel gcc make -y
tar -zxf keepalived-1.2.2.tar.gz
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-71.el6.x86_64/
make
make install
mkdir /etc/keepalived/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
yungho@yeah.net #接收报警那的邮件地址
}
notification_email_from keepalived@localhost #邮件的发送方
smtp_server 127.0.0.1 #使用本机转发email
smtp_connect_timeout 30
router_id LVS_DEVEL #路由标识,用于email报警
}
vrrp_instance VI_1 {
state MASTER 此处的关键字在备节点上改BACKUP
interface eth0 #HA通信的网络接口
virtual_router_id 51 #主备路由ID一样,除此以外,不能再有相同的路由ID
priority 100 备节点上的优先级应小于此处(数字越小优先级越小).
advert_int 1 #主备之间通报的间隔秒数
authentication { #主备切换时的切换的验证
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.111 #提供给客户端的VIP
}
}
virtual_server 192.168.0.111 80 {
delay_loop 6 #隔多长时间查询realserver状态
lb_algo rr #LVS调度算法,此处使用轮循
lb_kind DR #LVS负载均衡,此处采用直连机制
# nat_mask 255.255.255.0
# persistence_timeout 50 #同一IP的连接60秒内被分配到同一台realserver
protocol TCP #用TCP协议检查realserver状态
real_server 192.168.0.4 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3 #故障重试秒数
delay_before_retry 3 #重试延迟
}
}
real_server 192.168.0.5 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
/etc/init.d/keepalived start
####在realserver做如下操作
yum install httpd -y
vi /etc/sysctl.conf #屏蔽ARP封包
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
sysctl -p
ifconfig eth0:0 192.168.0.144 netmask 255.255.255.255 #添加虚拟IP子接口
route add -host 192.168.0.144 dev eth0:0 #添加直连路由
echo `hostname` > /var/www/html/index.html
service httpd start
####测试:
a. 可通过将master和backup的keepalived服务停掉或网络接口down掉,来观察主备节点之间任务的交接
b. 直接将realserver的httpd服务stop掉,在调度端观察
c. 访问 看到页面在两个 realserver 上切换表示成功!
d. 你也可以通过 ipvsadm -Lnc 查看详细连接情况
ip addr list
ipvsadm 在主备调度节点查看realserver健康状况
注:若刚开始没有自动切换和接管,通常先手动切换几次,就OK!
GOOD LUCK!
阅读(1176) | 评论(0) | 转发(0) |