标签:linux lvs keepalived
lvs和keepalived的关系
1.lvs可以处理负载均衡,但当其中一台RealServer发生故障的时候,并不能实时检测并剔除掉发生故障的RealServer,也不能当RealServer恢复正常后自动添加到lvs集群中。
2.keepalived用于容灾,当集群中的节点发生故障的时候,会自动转移业务到可用的节点,但并不能将请求平均分配到集群中空闲的节点。
3.lvs可以和有容灾功能的模块结合,组成高可用系统。例如lvs + heartbeat, lvs + keepalived等。keepalived可以和有负载均衡功能的模块结合,组成高可用系统。例如keepalived + lvs, keepalived + haproxy等。
4.lvs单独运行,有负载均衡的作用。keepalived单独运行,有容灾的作用。这两个模块,都可以单独运行或者同其他模块结合,组成高可用系统。
5.keepalived可以运行在lvs之上,由于liunx2.6之后的内核中已经包含了lvs模块,keepalived编译的时候,可以将lvs的管理接口编译到keepalived中。在keepalived的配置文件中,如果配置lvs模块的功能,就会启用lvs。如果不配置lvs的模块,那就不会启用lvs。lvs的管理接口也可以单独安装ipvsadm软件,来完成和lvs模块的交互。
################################################
主从地址分配
192.168.10.100 #VIP
192.168.10.11 #主DR
192.168.10.12 #从DR
192.168.10.201 #RS1
192.168.10.202 #RS2
##################################################
#安装ipvsadm(DR1,DR2)
# yum install ipvsadm -y
#安装keepalived(DR1,DR2)
# tar zxvf keepalived-1.1.18.tar.gz
# cd keepalived-1.1.18
# ./configure --prefix=/usr/local/keepalived
# make
# make install
# cd /usr/local/keepalived/
# cp etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp etc/keepalived/keepalived.conf /etc/keepalived/
# cp sbin/keepalived /usr/sbin/
#开启路由转发(DR1,DR2)
# sysctl -w net.ipv4.ip_forward=1
#######################################################
#修改keepalived,添加RS(DR1,DR2)
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from garany@qq.com
# smtp_server 192.168.10.200
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #从DR为BACKUP
interface eth0
virtual_router_id 51
priority 100 #从DR为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100
}
}
virtual_server 192.168.10.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.10.202 80 {
weight 1
HTTP_GET {
url {
path /index.html
digest 66f9880264d700a22ce5119f5ab56d3a # genhash -s 192.168.10.201 -p 80 -u /index.html获取
}
connect_timeout 1
nb_get_retry 1
delay_before_retry 1
}
}
real_server 192.168.10.201 80 {
weight 1
HTTP_GET {
url {
path /index.html
digest c672bf51bae55cb4f886abbde1bd6dad
}
connect_timeout 1
nb_get_retry 1
delay_before_retry 1
}
}
}
#####################################################
#RS安装httpd并启动(RS1,RS2)
# yum install httpd* -y
# service httpd restart
#RS添加VIP(RS1,RS2)
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
. /etc/rc.d/init.d/functions
VIP=192.168.10.100
host=`/bin/hostname`
case "$1" in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig lo down
/sbin/ifconfig lo up
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep $VIP`
isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
if [ ! "$islothere" -o ! "isrothere" ];then
# Either the route or the lo:0 device
# not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
######################################################
#清除所有虚拟路由路由(DR1,DR2)
# ipvsadm -C
#重启keepalived(DR1,DR2)
# service keepalived restart
#查看虚拟地址(DR1,DR2)
# ip addr #此时只有主DR上有192.168.10.100的地址
#查看生成的虚拟路由(DR1,DR2)
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.10.100:80 rr persistent 1
-> 192.168.10.201:80 Route 1 0 0
-> 192.168.10.202:80 Route 1 0 0
#############################################################
#测试
1.主从切换
# service keepalived stop #在主DR上停止keepalived
此时,VIP漂移到DR2上。通过日志查看(/var/log/message)
# service keepalived restart#在主DR上重启keepalived
此时,VIP从新回到DR1上,DR2回到BACKUP状态。通过日志查看(/var/log/message)
2.RealServer健康检查
# service httpd stop #在RS1上停止httpd
此时,主DR从路由中删除RS1。使用ipvsadm查看(ipvsadm -Ln)
# service httpd start #在RS1上启动httpd
此时,主DR从路由中添加RS1。使用ipvsadm查看(ipvsadm -Ln)
标签:linux lvs keepalived
原文地址:http://7352714.blog.51cto.com/7342714/1737019