keepalived+lvs负载均衡

virtual ip:192.168.0.202
load balancer:192.168.0.101
backup:192.168.0.201
real server1:192.168.0.1
real server2:192.168.0.2

load balancer的主备机配置:
安装:
yum install ipvsadm kernel-xen-devel openssl-devel -y
wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz
tar xf keepalived-1.1.20.tar.gz
cd keepalived-*
./configure --prefix=/usr/local/keepalived \
--with-kernel-dir=/usr/src/kernels/2.6.18-164.el5xen.i686/
make && make install
注意编译时需看到以下即为OK。
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
Use VRRP Framework : Yes


配置:
mkdir /etc/keepalived
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/

vi /etc/keepalived/keepalived.conf
=======================================================
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER //on backup change this to: BACKUP
interface eth0
virtual_router_id 7 //this must keep same with backup
priority 150 //主机优先级,备机改为50
advert_int 1 //主备间通告间隔秒数
authentication { //主备切换时的验证
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { //HA 的virtual ip,可多个
192.168.0.202
}
}

virtual_server 192.168.0.202 80 {
delay_loop 6 //每隔6秒查询real server 状态
lb_algo rr //lvs 轮叫调度RR
lb_kind DR //lvs负载均衡机制,直连路由
persistence_timeout 50 //同一IP的连接50秒内被分配到同一台real server
protocol TCP //用TCP协议检查real server 状态

real_server 192.168.0.1 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3 //故障重试秒数
delay_before_retry 3 //重试延迟
}
}
real_server 192.168.0.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

========================================================
real server配置:
vi /etc/sysctl.conf

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.202 netmask 255.255.255.255 up
route add -host 192.168.0.202 dev eth0:0

echo `hostname` > /var/www/html/index.html
/etc/init.d/httpd start
测试:
访问http://192.168.0.202 不断刷新看到在两个real server上切换即OK!
ipvsadm -Lnc 查看详细连接情况.
原文出处:http://www.xnlinux.cn/thread-1361-1-1.html

你可能感兴趣的:(LVS,keepalived,职场,负载,休闲)