Chinaunix首页 | 论坛 | 博客
  • 博客访问: 452404
  • 博文数量: 104
  • 博客积分: 3016
  • 博客等级: 中校
  • 技术积分: 1992
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-24 11:37
文章分类

全部博文(104)

文章存档

2009年(44)

2008年(60)

我的朋友

分类: LINUX

2009-04-19 20:19:52

网络环境:
LVS IP:192.168.100.129(Virtual IP:192.168.100.135)
Real IP1:192.168.100.128
Real IP2:192.168.100.130

一、准备工作
用VMware ESXi 3.5新建一台虚拟机安装CentOS5.0,安装时选择安装Apache。
然后clone两台。

二、在129上安装LVS等
1、安装ipvsadmin:
# wget
# rpm -Uvh ipvsadm-1.24-6.src.rpm
# ln -s /usr/src/kernels/2.6.18-8.el5-i686/ /usr/src/linux
# cd /usr/src/redhat/SOURCES
# tar xzf ipvsadm-1.24.tar.gz
# cd ipvsadm-1.24
# make && make install
# ipvsadm --help //能看到帮助信息则表示成功
# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.conf.lo.arp_ignore = 0
net.ipv4.conf.lo.arp_announce = 0
net.ipv4.conf.all.arp_ignore = 0
net.ipv4.conf.all.arp_announce = 0
# sysctl -p

# vi /etc/sysconfig/network-scripts/ifcfg-eth0:1
DEVICE=eth0:1
BOOTPROTO=none
ONBOOT=yes
NETMASK=255.255.255.255
IPADDR=192.168.100.135
GATEWAY=192.168.100.1
BROADCAST=192.168.100.135
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes

以下这条静态路由,好像有无都无所谓,因为重启网络或系统后,照样可以用的
# route add -host 192.168.100.135 dev eth0:1
//add virtual server
# ipvsadm -A -t 192.168.100.135:80 -s rr
// add real server
# ipvsadm -a -t 192.168.100.135:80 -r 192.168.100.128 -g   
# ipvsadm -a -t 192.168.100.135:80 -r 192.168.100.130 -g
# ipvsadm --save > /etc/sysconfig/ipvsadm
//查看是否配置成功
# ipvsadm -l

2、安装keepalived
# wget
# tar zxvf keepalived-1.1.15.tar.gz
# cd keepalived-1.1.15
# ./configure --prefix=/usr/local/keepalived
# make && make install
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

# mv /usr/local/keepalived/etc/keepalived/keepalived.conf /usr/local/keepalived/etc/keepalived/keepalived.conf.090419
# vi /usr/local/keepalived/etc/keepalived/keepalived.conf
添加以下内容:
! Configuration File for keepalived
global_defs {
notification_email {
hwan@abc.com.cn
}
notification_email_from hwan@abc.com.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER ###BACKUP
interface eth0
virtual_router_id 51 ###BACKUP一样
priority 102 ###BACKUP不一样
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 ######BACKUP一样
}
virtual_ipaddress {
192.168.100.135
}
}
virtual_server 192.168.100.135 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 50
protocol TCP

real_server 192.168.100.128 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 30
}
}
real_server 192.168.100.130 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 30
}
}

# service keepalived start


3、配置Real Server,分别在128和130上执行以下操作
#  vi /etc/sysctl.conf
net.ipv4.ip_forward = 0
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
# sysctl -p
# vi /etc/sysconfig/network-scripts/lo:0

DEVICE=lo:0
IPADDR=192.168.100.135
NETMASK=192.168.100.135
BROADCAST=192.168.100.135
ONBOOT=yes

# route add -host 192.168.100.135 dev lo:0

三、测试:
1、192.168.100.128
# service httpd start
# echo 128 > /var/www/html/index.html

2、192.168.100.130
# service httpd start
# echo 130 > /var/www/html/index.html

在浏览器上访问
可以看到页面上显示128,刷新,页面上即显示130
这样就表示成功了。


一些命令:
删除服务地址:
ipvsadm -D -t 192.168.100.135:80
不知道keepalived是做什么的,貌似keepalived stop的时候,负载均衡照样可用。
奇怪
[root@localhost ~]# service network restart
正在关闭接口 eth0: [确定]
关闭环回接口: [确定]
禁用 IPv4 包转送: net.ipv4.ip_forward = 0
[确定]
弹出环回接口: [确定]
弹出界面 eth0: [确定]


[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward
1

奇怪啊!!

主要参考文章:
http://hi.baidu.com/hwangato/blog/item/bf29b1b7a0fa2df230add16e.html

http://hi.baidu.com/jackbillow/blog/item/e007fcfb1ff08f204e4aea82.html
阅读(1151) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~