LVS+Keepalived实现高可用和负载均衡的解决方案
[日期:2012-03-12] 来源:Linux社区 作者:Linux
实验拓扑图:
实验ip规划
VIP:192.168.1.200
Real server1:192.168.1.195
Real server2:192.168.1.193
心跳线:10.1.1.1、10.1.1.2
DR1:192.168.1.192
DR2:192.168.1.1.190
实现过程
第一:在主节点上的配置
首先配置LVS
[root@localhost ~]# cd tools/
[root@localhost tools]# ls ipvsadm-1.24.tar.gz
ipvsadm-1.24.tar.gz
[root@localhost tools]# tar zxvf ipvsadm-1.24.tar.gz
[root@localhost tools]# cd ipvsadm-1.24
[root@localhost ipvsadm-1.24]# ln -s /usr/src/kernels/2.6.18-164.el5 /usr/src/linux/
[root@localhost ipvsadm-1.24]# make all && make install
然后安装配置keepalived
[root@localhost tools]# ls keepalived-1.2.2.tar.gz
keepalived-1.2.2.tar.gz
[root@localhost tools]# tar zxvf keepalived-1.2.2.tar.gz
[root@localhost tools]# cd keepalived-1.2.2
[root@localhost keepalived-1.2.2]# vi keepalived/libipvs-2.6/ip_vs.h
找到下面一行:
#include /* For __beXX types in userland */
把他放到下面一行的下面:
#include
[root@localhost keepalived-1.2.2]# make && make install
[root@localhost keepalived-1.2.2]# ln -s /usr/local/sbin/keepalived /sbin/
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak //备份
[root@localhost keepalived]# vi keepalived.conf //修改设置档如下
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from
smtp_server 192.168.1.192
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_typePASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200
}
}
virtual_server 192.168.1.200 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.1.193 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.195 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
}
在备用节点上的设置和主节点基本完全相同(因为是提供相同的服务),仅仅需要修改的就是keepalived.con
vrrp_instance VI_1 {
state BACKUP //修改处
interface eth0
virtual_router_id 51
priority 80 //修改处
advert_int 1
authentication {
auth_typePASS
auth_pass 1111
}
第二:在real server上的设置(两个real server基本相同,故这里只设置一个)
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vi index.html
It's 192.168.1.195!!
[root@localhost html]# /etc/init.d/httpd start
[root@localhost ~]# vi /etc/init.d/lvsrs
#!/bin/bash
VIP=192.168.1.200
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
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
sysctl -p
#end
[root@localhost ~]# chmod a+x /etc/init.d/lvsrs
[root@localhost ~]# /etc/init.d/lvsrs start
第三:测试的部分(开启real server的apache服务)
[root@localhost ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.200:http rr persistent 50
-> 192.168.1.193:http Route 1 0 0
-> 192.168.1.195:http Route 1 0 0
[root@localhost ~]# /etc/init.d/keepalived start
IE中访问不断刷新就能看到效果
另外可以人为的制造单点故障来测试,具体的过程参看/var/log/messages
总结:这是负载均衡学习的一个总结,接下来重点要就高可用的方案。
最后列出软件呢安装时出现的问题及其解决办法
1、
Keepalived安装错误一例(make错误)(本部分拾人牙慧)
/usr/include/sys/types.h:235: 错误:与 ‘blkcnt_t’ 类型冲突
/usr/src/kernels/2.6.18-238.19.1.el5-i686//include/linux/types.h:142: 错误:‘blkcnt_t’ 的上一个声明在此
make[2]: *** [ip_vs_nl_policy.o] 错误 1
make[2]: Leaving directory `/usr/local/src/keepalived-1.2.2/keepalived/libipvs-2.6'
make[1]: *** [all] 错误 1
make[1]: Leaving directory `/usr/local/src/keepalived-1.2.2/keepalived'
make: *** [all] 错误 2
解决方法:
进入源码目录就是你解压的目录了:
cd /usr/local/src/keepalived-1.2.2
vi keepalived/libipvs-2.6/ip_vs.h
找到下面一行:
#include /* For __beXX types in userland */
把他放到下面一行的下面:
#include
我想我说明白了,这是最新版本的问题,如果用keepalived-1.2.1则不会出现这个问题了
2、ipvsadm make时的错误
make[1]: gcc: Command not found
make[1]: *** [libipvs.o] Error 127
make[1]: Leaving directory `/home/wss/lvs/ipvsadm-1.24/libipvs'
make: *** [libs] Error 2
[root@localhost ipvsadm-1.24]#
解决办法
[root@localhost ~]# ln -s /usr/src/kernels/2.6.18-164.el5 /usr/src/linux/
结束最后实验
本篇文章来源于 Linux公社网站() 原文链接: