4.安装配置LVS
4.1 准备工作
地址规划:
服务器名 |
IP地址 |
网关 |
虚拟设备名 |
虚拟IP |
Director Server |
192.168.98.75 |
192.168.98.1 |
eth0:0 |
192.168.98.77 |
Real Server 1 |
192.168.98.74 |
192.168.98.1 |
lo:0 |
192.168.98.77 |
Real Server 2 |
192.168.98.117 |
192.168.98.1 |
lo:0 |
192.168.98.77 |
Real Server n |
… |
… |
.. |
.. |
4.2 安装LVS
LVS的具体实现是由ipvsadm 这个程序来完成,因此判断一个系统是否具备 ipvs 功能,只需要查看看 ipvsadm 程序是否被安装。查看 ipvsadm 程序最简单的办法就是在任意路径执行命令ipvsadm。下表为安装 ipvsadm 及未安装 ipvsadm 的输出对比。
执行 ipvsadm 后的输出 |
|
未安装 ipvsadm |
-bash: ipvsadm: command not foun |
安装 ipvsadm |
IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->RemoteAddress:Port Forward Weight ActiveConn InActConn |
*所有Director Server需要安装ipvsadm,Real Server只需要执行配置脚本。
1. 创建一个连接文件,其命令为:
[[email protected] ~]# ln –sv /usr/src/kernels/2.6.18-308.el5-x86_64/ /usr/src/linux
注意一定要与当前的运行的内核相一致,因为/usr/src/kernels 目录下可多个目录。如果不创建这个连接文件,在编译时会出错,从而不能继续进行安装。
2. 解压ipvsadm-1.24.tar.gz软件包:
[[email protected] ~]# tar zxvf ipvsadm-1.24.tar.gz
3. 编译安装
[[email protected] ~]# cd ipvsadm-1.24
[[email protected] ~]# make
[[email protected] ~]# make install
可执行文件被安装到/sbin/ipvsadm
l 检验安装ipvsadm是否被正确安装
1. 执行ipvsadm,输出如上表第二列底三行所示。
2. 检查当前加载的内核模块,看是否存在 ip_vs 模块。
[[email protected] ~]# lsmod|grep ip_vs
ip_vs 122112 0
4.3 配置及验证Director Server
4.3.1 配置Director Server
#!/bin/bash VIP=192.168.98.77 #虚拟ip(供用户访问的ip) RIP1=192.168.98.74 #真实服务器(real server)ip RIP2=192.168.98.117 #真实服务器(real server)ip PORT=2200 #端口 case "$1" in start) echo "start config LVS Director Server..." ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up route add -host $VIP dev eth0:0 echo "1">/proc/sys/net/ipv4/ip_forward ipvsadm -C ipvsadm -A -t $VIP:$PORT -s rr 120 ipvsadm -a -t $VIP:$PORT -r $RIP1:$PORT –g #添加real server,多个可配置多条 ipvsadm -a -t $VIP:$PORT -r $RIP2:$PORT –g #添加real server,多个可配置多条 ipvsadm echo "config LVS Director Server success!" ;; stop) echo "shut down LVS Director Server" echo "0">/proc/sys/net/ipv4/ip_forward ipvsadm -C ifconfig eth0:0 down echo "shut down LVS Director Server success!" ;; *) echo "usage:$0 {start|stop}" exit 1 esac |
执行:./ lvsDR.sh start 打开
执行:./ lvsDR.sh stop 关闭
*执行前修改权限:chmod 755 lvsDR.sh
4.3.2 验证Director Server配置
[[email protected] lvs]# ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.98.77:2200 wlc -> 192.168.98.117:2200 Route 1 0 0 -> 192.168.98.74:2200 Route 1 0 0 |
输出上述信息配置成功。
4.3.3 将ipvsadm配置成开机启动脚本
lvsDR_auto.sh:Director Server配置脚本
其中VIP,RIP1,RIP2,PORT应根据实际需要自行修改
#!/bin/bash VIP=192.168.98.77 #虚拟ip(供用户访问的ip) RIP1=192.168.98.74 #Real Server ip RIP2=192.168.98.117 #Real Server ip PORT=2200 #端口 echo "start config LVS Director Server..." ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up route add -host $VIP dev eth0:0 echo "1">/proc/sys/net/ipv4/ip_forward ipvsadm -C ipvsadm -A -t $VIP:$PORT -s wlc ipvsadm -a -t $VIP:$PORT -r $RIP1:$PORT -g ipvsadm -a -t $VIP:$PORT -r $RIP2:$PORT -g ipvsadm echo "config LVS Director Server success!" exit 1 |
lvsRS_auto.sh:Real Server配置脚本
其中VIP应该跟Director Server配置的VIP一样
#!/bin/bash VIP=192.168.98.77 #虚拟ip(供用户访问的ip) echo "start config lvs Real Server..." ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up route add -host $VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce echo "config lvs Real Server success!" exit 1 |
首先脚本文件放到服务器上后都需要添加执行权限:
[[email protected] ~]# chmod 755 lvsDR_auto.sh
[[email protected] ~]# chmod 755 lvsRS_auto.sh
Director Server:
[[email protected] ~]# cp lvsDR_auto.sh /etc/init.d/
[[email protected] ~]# ln -s /etc/init.d/lvsDR_auto.sh /etc/rc.d/rc3.d/S99lvsDR_auto
[[email protected] ~]# ln -s /etc/init.d/lvsDR_auto.sh /etc/rc.d/rc5.d/S99lvsDR_auto
Real Server:
[[email protected] ~]# cp lvsRS_auto.sh /etc/init.d/
[[email protected] ~]# ln -s /etc/init.d/lvsRS_auto.sh /etc/rc.d/rc3.d/S99lvsRS_auto
[[email protected] ~]# ln -s /etc/init.d/lvsRS_auto.sh /etc/rc.d/rc5.d/S99lvsRS_auto
4.4 配置及验证Real Server
4.4.1 配置Real Server
#!/bin/bash VIP=192.168.98.77 #虚拟ip(供用户访问的ip) case "$1" in start) echo "start config lvs Real Server..." ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up route add -host $VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce echo "config lvs Real Server success!" ;; stop) echo "shutting down lvs Real server..." ifconfig lo:0 down echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "shut down lvs Real server success!" ;; *) echo "usage:$0 {start|stop}" exit 1 esac |
执行:./ lvsRS.sh start 打开
执行:./ lvsRS.sh stop 关闭
*执行前修改权限:chmod 755 lvsRS.sh
4.4.2 验证Resl Server配置
[[email protected] ~]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet 192.168.98.77/32 brd 192.168.98.77 scope global lo:0 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:50:56:91:14:81 brd ff:ff:ff:ff:ff:ff inet 192.168.98.118/24 brd 192.168.98.255 scope global eth0 inet6 fe80::250:56ff:fe91:1481/64 scope link valid_lft forever preferred_lft forever 3: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0 |
从 输 出 可 以 看 出 , lo0:0 确实 绑 定 了 我 们 指 定 的 vip 地 址 。