LVS的原理,以及3种方式就不再赘述了。
一、环境说明
master(keepalive+lvs):192.168.3.166
real_server1:192.168.3.173
real_server2:192.168.3.178
vip:192.168.3.101
GATEWAY:192.168.3.1
软件环境:keepalived-1.2.8.tar.gz
ipvsadm-1.24-13.el5
需要分配请求的端口:192.168.3.173和192.168.3.178的80端口(TCP)
二、安装配置
1.编辑master网卡配置
vim/etc/sysconfig/network-script/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="static"
IPADDR=192.168.3.166
NETMASK=255.255.255.0
GATEWAT=192.168.3.1
NETWORK=192.168.3.0
DNS=8.8.8.8
2.更改主机名
vim/etc/hosts
192.168.3.166master(非必须)
3.安装软件
下载keepalive
wgethttp://www.keepalived.org/software/keepalived-1.2.8.tar.gz
yuminstallopenssl-*gcc-*kernel-devel-*popt-*-y
yuninstallipvsadm-y
tarxfkeepalived-1.2.8.tar.gz-C/usr/src
cd/usr/src/keepalived-1.2.8
./configure--prefix=/--with-kernel-dir=/usr/src/kernels/2.6.18-371.el5-i686
make&&makeinstall
三、配置keepalived
vim/etc/keepalived/keepalived.conf
global_defs{
notification_email{
}
smtp_server192.168.3.166(这个地址为邮件服务器地址,用来发邮件,如果没有填本地或者随意一个地址都可,不影响使用)
smtp_connect_timeout30
router_idmaster
}
vrrp_instanceVI_1{
stateMASTER
interfaceeth0
virtual_router_id51
priority100
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
192.168.3.101
}
}
-----------------------------下面这段,如果有多个realserver添加即可,如果有多个VIP地址,则添加VIP地址,并在VIP地址内填写RS信息
virtual_server192.168.3.10180{
delay_loop6
lb_algorr
lb_kindDR
nat_mask255.255.255.0
persistence_timeout60
protocolTCP
real_server192.168.3.17380{
weight1
TCP_CHECK{
connect_timeout10
nb_get_retry3
delay_before_retry3
connect_port80
}
real_server192.168.3.17880{
weight1
TCP_CHECK{
connect_timeout10
nb_get_retry3
delay_before_retry3
connect_port80
}
}
}
四、启动lvs
/etc/init.d/keepalivedstart
/etc/init.d/ipvsadmstart
五、架设real_server
vim/etc/rc.d/rc.local---添加
vip=192.168.3.101
ifconfiglo:0$vipbroadcast$vipnetmask255.255.255.255up
routedel-host$vip
routeadd-host$vipdevlo:0
echo1>/proc/sys/net/ipv4/conf/lo/arp_ignore
echo2>/proc/sys/net/ipv4/conf/lo/arp_announce
echo1>/proc/sys/net/ipv4/conf/all/arp_ignore
echo2>/proc/sys/net/ipv4/conf/all/arp_announce
sysctl-p
/etc/init.d/httpdstart
或
cat>>/etc/rc.d/rc.local<<EOF
vip=192.168.3.101
ifconfiglo:0$vipbroadcast$vipnetmask255.255.255.255up
routedel-host$vip
routeadd-host$vipdevlo:0
echo1>/proc/sys/net/ipv4/conf/lo/arp_ignore
echo2>/proc/sys/net/ipv4/conf/lo/arp_announce
echo1>/proc/sys/net/ipv4/conf/all/arp_ignore
echo2>/proc/sys/net/ipv4/conf/all/arp_announce
sysctl-p
/etc/init.d/httpdstart
EOF
这一段把RS需要的配置写入到开机启动项,防止系统意外关机重启而无法连接上lvs
六、在master机器上使用ipvsadm查看
[root@localhost~]#ipvsadm-l
IPVirtualServerversion1.2.1(size=4096)
ProtLocalAddress:PortSchedulerFlags
->RemoteAddress:PortForwardWeightActiveConnInActConn
TCP192.168.3.101:httprr
->192.168.3.178:httpRoute101
->192.168.3.173:httpRoute101
[root@localhost~]#
如果使用ipvsadm没有信息
[root@localhost~]#ipvsadm-l
IPVirtualServerversion1.2.1(size=4096)
ProtLocalAddress:PortSchedulerFlags
->RemoteAddress:PortForwardWeightActiveConnInActConn
[root@localhost~]#
把下面这段写到脚本里执行
#!/bin/bash
echo1>/proc/sys/net/ipv4/ip_forward----开启IP转发
ipvsadm-C
-----------清空之前的ipvsadm表
ipvsadm-At192.168.3.101:80-srr-------使用算法,轮询
ipvsadm-at192.168.3.101:80-r192.168.3.173:80-g------------增加一台RS
ipvsadm-at192.168.3.101:80-r192.168.3.178:80-g
本文出自 “tring” 博客,转载请与作者联系!