分类: LINUX
2010-09-19 20:54:51
利用LVS+Keepalived 实现高性能高可用负载均衡服务器
作者:feiyang blog:pkfeiyang.cublog.cn
, , , ,
1 LVS-DR-Master 192.168.188.138 (lvs本身的真实ip) 2 LVS-DR-BACKUP 192.168.188.139 3 LVS-DR-VIP 192.168.188.131 (虚拟ip) 4 WEB1-Realserver 10.10.10.133 (web的真实ip) 5 WEB2-Realserver 10.10.10.136 6 GateWay 192.168.188.1 复制代码 三. 安装LVS和Keepalvied软件包 7 #mkdir /usr/local/src/lvs 8 #cd /usr/local/src/lvs 9 wget wget yum install -y kernel-devel 如果没找内核目录时要进行安装 2. 安装LVS和Keepalived 10 #lsmod |grep ip_vs 11 #uname -r 12 2.6.18-164.el5 13 # ln -s /usr/src/kernels/2.6.18-194.11.3.el5-i686/ /usr/src/linux (不能连接时 缺包 要装一个kernel-devel) 14 15 #tar zxvf ipvsadm-1.24.tar.gz 16 #cd ipvsadm-1.24 17 #make && make install 18 #find / -name ipvsadm # 查看ipvsadm的位置 19 20 #tar zxvf keepalived-1.1.20.tar.gz 21 #cd keepalived-1.1.20 22 #./configure && make && make install 23 #find / -name keepalived # 查看keepalived位置 24 25 #cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ 26 #cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ 27 #mkdir /etc/keepalived 28 #cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ 29 #cp /usr/local/sbin/keepalived /usr/sbin/ 30 #service keepalived start|stop #做成系统启动服务方便管理 复制代码 四. 配置LVS实现负载均衡 1 [root@daniel ~]# vi /usr/local/sbin/lvs-dr-sh 2 3 #!/bin/bash 4 GW=192.168.188.1 5 # website director vip. 6 SNS_VIP=192.168.188.131 7 SNS_RIP1=192.168.188.133 8 SNS_RIP2=192.168.188.136 9 /etc/rc.d/init.d/functions 10 11 logger $0 called with $1 12 13 case "$1" in 14 15 start) 16 # set squid vip 17 /sbin/ipvsadm --set 30 5 60 18 /sbin/route add -host $SNS_VIP dev eth0:0 19 /sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3 20 /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1 21 /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1 22 touch /var/lock/subsys/ipvsadm >/dev/null 2>&1 23 24 ;; 25 stop) 26 /sbin/ipvsadm -C 27 /sbin/ipvsadm -Z 28 ifconfig eth0:0 down 29 ifconfig eth0:1 down 30 route del $SNS_VIP 31 route del $SS_VIP 32 rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1 33 echo "ipvsadm stoped" 34 ;; 35 36 status) 37 38 if [ ! -e /var/lock/subsys/ipvsadm ];then 39 echo "ipvsadm stoped" 40 exit 1 41 else 42 echo "ipvsadm OK" 43 fi 44 ;; 45 46 *) 47 echo "Usage: $0 {start|stop|status}" 48 exit 1 49 esac 50 exit 0 复制代码 2. 配置Realserver脚本. 1 [root@daniel ~]# vi /usr/local/sbin/realserver.sh 2 3 #!/bin/bash 4 # description: Config realserver lo and apply noarp 5 6 SNS_VIP=192.168.188.131 7 /etc/rc.d/init.d/functions 8 9 case "$1" in 10 start) 11 ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 up 12 /sbin/route add -host $SNS_VIP dev lo:0 13 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore 14 echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce 15 echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore 16 echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce 17 sysctl -p >/dev/null 2>&1 18 echo "RealServer Start OK" 19 20 ;; 21 stop) 22 ifconfig lo:0 down 23 route del $SNS_VIP >/dev/null 2>&1 24 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore 25 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce 26 echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore 27 echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce 28 echo "RealServer Stoped" 29 ;; 30 *) 31 echo "Usage: $0 {start|stop}" 32 exit 1 33 esac 34 exit 0 复制代码 或者采用secondary ip address方式配置
35 net.ipv4.conf.lo.arp_ignore = 1 36 net.ipv4.conf.lo.arp_announce = 2 37 net.ipv4.conf.all.arp_ignore = 1 38 net.ipv4.conf.all.arp_announce = 2 39 [code] 40 #sysctl –p 41 #ip addr add 192.168.188.131/24 dev lo:0 (在web服务器上也要做) 42 #ip add list 查看是否绑定 43 3. 启动lvs-dr脚本和realserver启本,在DR上可以查看LVS当前状态: 44 #watch ipvsadm –ln 复制代码 2. 配置虚地址: vi /etc/sysconfig/network-scripts/ifcfg-lo:0, 添加 DEVICE=lo:0 IPADDR=192.168.188.131 NETMASK=255.255.255.0 ONBOOT=yes NAME=loopback 3. 关闭RealServer的ARP, 或者编辑/etc/sysctl.conf,修改系统变量 net.ipv4.conf.eth0.arp_ignore=1 net.ipv4.conf.eth0.arp_announce=1 net.ipv4.ip_forward = 1 #sysctl –p : 使配置生效 补充:打开 echo "1" > /proc/sys/net/ipv4/ip_forwarding
五.利用Keepalvied实现负载均衡和和高可用性 [root@daniel ~]# vi /etc/keepalived/keepalived.conf
/bin/bash: Configuration: command not found bal_defs { notification_email { pangkuo@kaixinad.com } notification_email_from pangkuo@kaixinad.com smtp_server 127.0.0.1 # smtp_connect_timeout 30 router_id LVS_DEVEL } ###by daniel vrrp_instance VI_1 { state MASTER #备份服务器上将MASTER改为BACKUP interface eth0 virtual_router_id 51 priority 100 # 备份服务上将100改为99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.188.131 #(如果有多个VIP,继续换行填写.) } }
virtual_server 192.168.188.131 80 { 1 delay_loop 6 #(每隔10秒查询realserver状态) 2 lb_algo wrr #(lvs 算法) 3 lb_kind DR #(Direct Route) 4 persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver) 5 protocol TCP #(用TCP协议检查realserver状态) 6 7 real_server 192.168.188.133 80 { 8 weight 3 #(权重) 9 TCP_CHECK { 10 connect_timeout 10 #(10秒无响应超时) nb_get_retry 3 delay_before_retry 3 conect_port 80 } } real_server 192.168.188.136 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 conect_port 80 } } } 复制代码 2. BACKUP服务器同上配置,先安装lvs再按装keepalived,仍后配置/etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可.
11 IP Virtual Server version 1.2.1 (size=4096) 12 Prot LocalAddressort Scheduler Flags 13 -> RemoteAddressort Forward Weight ActiveConn InActConn 14 TCP 192.168.188131:80 wrr persistent 60 15 -> 192.168.188.133:80 Route 3 0 0 16 -> 192.168.188.136:80 Route 3 0 0 复制代码 #tail –f
/var/log/message 监听日志,查看状态,测试LVS负载均衡及高可用性是否有效。 |
错误输出:
./lvs-dr.sh: line 21: ./etc/rc.d/init.d/functions: No such file or directory
SIOCSIFBRDADDR: Cannot assign requested address
SIOCSIFFLAGS: Cannot assign requested address
SIOCSIFFLAGS: Cannot assign requested address
SIOCADDRT: No such device
Service already exists
Destination already exists
Destination already exists
解决:ip地址冲突
2:[root@ipzhu67 ~]# /etc/init.d/ipvsadm start
Clearing the current IPVS table: [ OK ]
Applying IPVS configuration: /etc/init.d/ipvsadm: line 62: /etc/sysconfig/ipvsadm: No such file or directo
解决:touch ipvsadm
chinaunix网友2010-09-21 07:58:39
很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com