Chinaunix首页 | 论坛 | 博客
  • 博客访问: 228452
  • 博文数量: 24
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1007
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-19 12:10
个人简介

人若不能在内心找到安宁,在哪里也无济于事。

文章分类

全部博文(24)

文章存档

2013年(24)

我的朋友

分类: 系统运维

2013-08-13 15:53:57

lpvsadm
IPVS(IP Virtual Server)是整个负载均衡的基础,如果没有这个基础,故障隔离与失败切换就毫无意义了。
IPVS 具体实现是由ipvsadm这个程序来完成。ipvsadm只要安装好就可以,所有配置由keepalived.conf这个文件来实现。

keepalived
在这里主要作用是Realserver的健康状态检查及故障隔离;负载均衡Master机和Backup主机之间失败切换的实现(failover)。

一、准备工作
1.server准备
virtual ip            192.168.1.130    由master提供,master出现故障时由backup提供
server01 (master)     192.168.1.120    提供vip,并实时监测web01、web02的健康状态
server02 (backup)     192.168.1.121    备用机,master出现故障时接替其工作
web01 (realserver01)  192.168.1.122    真实地提供web服务,与web02做轮询
web02 (realserver02)  192.168.1.123    真实地提供web服务,与web01做轮询

2.所需软件
system:      Redhat 2.6.32-279.el6.x86_64
ipvsadm:    
keepalived:  

二、安装配置
1.安装ipvsadm (master、backup都要安装)
  1. # ln -s /usr/src/kernels/2.6.32-279.el6.x86_64/ /usr/src/linux/
  2. # tar zxvf ipvsadm-1.26.tar.gz
  3. # cd ipvsadm-1.26
  4. # make && make install
  5. # ipvsadm -ln
  6. IP Virtual Server version 1.2.1 (size=4096)
  7. Prot LocalAddress:Port Scheduler Flags
  8.   -> RemoteAddress:Port Forward Weight ActiveConn InActConn
2.安装keepalived (master、backup都要安装配置,注意配置文件的区别,其它都相同)
  1. # tar zxvf keepalived-1.2.0.tar.gz
  2. # cd keepalived-1.2.0
  3. # ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64/
  4. # make && make install
  5. # cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
  6. # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
  7. # mkdir /etc/keepalived
  8. # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
  9. # cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  10. master配置文件:
  11. # vi /etc/keepalived/keepalived.conf
  12. ! Configuration File for keepalived

  13. global_defs {
  14.    notification_email {
  15.         root@localhost
  16.    }
  17.    notification_email_from root@localhost
  18.    smtp_server 127.0.0.1
  19.    smtp_connect_timeout 30
  20.    router_id LVS_MASTER
  21. }

  22. vrrp_instance VI_1 {
  23.     state MASTER --backup此处需要修改为BACKUP
  24.     interface eth0
  25.     virtual_router_id 51
  26.     priority 100 --backup此处值应略小一些,如90
  27.     advert_int 1
  28.     authentication {
  29.         auth_type PASS
  30.         auth_pass 1111
  31.     }
  32.     virtual_ipaddress {
  33.         192.168.1.130
  34.     }
  35. }

  36. virtual_server 192.168.1.130 80 {
  37.     delay_loop 6
  38.     lb_algo wrr
  39.     lb_kind DR
  40. #    persistence_timeout 50    --表示同一ip50秒内的连接都发送到同一realserver,为了测试轮询,先注释
  41.     protocol TCP

  42.     real_server 192.168.1.122 80 {
  43.         weight 3
  44.         TCP_CHECK {
  45.             connect_timeout 10
  46.             nb_get_retry 3
  47.             delay_before_retry 3
  48.             connect_port 80
  49.         }
  50.     }
  51.     real_server 192.168.1.123 80 {
  52.         weight 3
  53.         TCP_CHECK {
  54.             connect_timeout 10
  55.             nb_get_retry 3
  56.             delay_before_retry 3
  57.             connect_port 80
  58.         }
  59.     }
  60. }

  61. backup配置文件:
  62. 与master相同,仅两处需要修改为:
  63. state MASTER --> state BACKUP
  64. priority 100 --> priority 90

  65. # chkconfig keepalived on
  66. # service keepalived start
  67. # service keepalived status
  68. keepalived (pid 1344) 正在运行...
3.realserver.sh脚本(web01 web02 都要部署)
  1. vi /root/bin/realserver.sh
  2. --------------------------------------------------------------
  3. #!/bin/bash
  4. vip=192.168.1.130
  5. . /etc/rc.d/init.d/functions
  6. case "$1" in
  7.     start)
  8.         /sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
  9.         /sbin/route add -host $vip dev lo:0
  10.         echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  11.         echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
  12.         echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
  13.         echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
  14.         sysctl -p >/dev/null 2>&1
  15.         echo "RealServer Start OK" ;;
  16.     stop)
  17.         /sbin/ifconfig lo:0 down
  18.         /sbin/route del $vip >/dev/null 2>&1
  19.         echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  20.         echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
  21.         echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
  22.         echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
  23.         echo "RealServer Stoped";;
  24.      *)    echo "Usage: $0 {start|stop}"
  25.         exit 1;;
  26. esac
  27. exit 0
  28. --------------------------------------------------------------
  29. # chmod +x /root/bin/realserver.sh
  30. # sh /root/bin/realserver.sh start
  31. # echo "sh /root/bin/realserver.sh start" >> /etc/rc.local
4.启动web01、web02上的httpd服务,为了更好区分,/var/www/html/index.html页面分别修改为:
web01 server works!
web02 server works!

三、测试
1.查看状态,并测试页面是否正常轮询 (此时master、backup上都启动keepalived,web01、web02都启动httpd)
# ip a|grep 192.168
    inet 192.168.1.120/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.130/32 scope global eth0                      --master上vip正常绑定        
# ipvsadm -ln                                                    --master上lvs正常
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.130:80 wrr persistent 50
  -> 192.168.1.122:80             Route   3      0          0         
  -> 192.168.1.123:80             Route   3      0          0 
浏览器打开 ,反复刷新,如果页面轮流显示web01,web02的主页信息,则说明轮询正常。
2.停止master上的keepalived,查看backup是否正常接管vip,再测试页面轮询是否正常
master
# service keepalived stop
# ip a|grep 192.168
    inet 192.168.1.120/24 brd 192.168.1.255 scope global eth0  --master上vip绑定已经解除
backup
# ip a |grep 192.168
    inet 192.168.1.121/24 brd 192.168.1.255 scope global eth0    --backup上vip正常接管 
    inet 192.168.1.130/32 scope global eth0
查看页面轮询是否正常
3.停止web01上的httpd,查看keepalived能否正常检测出并隔离故障机器
web01
# service httpd stop
backup
# 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.130:http wrr persistent 50
  -> web02:http                   Route   3      0          0     --正常隔离了web01




阅读(3912) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~