一,LVS的搭建 (Balancer用RHEL 6 搭建)
1.搭建网络环境 (Balancer 01)
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:BD:E7:65
inet addr:172.16.10.11 Bcast:172.16.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:febd:e765/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:77849 errors:0 dropped:0 overruns:0 frame:0
TX packets:510 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:56807279 (54.1 MiB) TX bytes:66326 (64.7 KiB)
Interrupt:18 Base address:0x2000
eth1 Link encap:Ethernet HWaddr 00:0C:29:BD:E7:6F
inet addr:192.168.10.11 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:febd:e76f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:199 errors:0 dropped:0 overruns:0 frame:0
TX packets:72 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:30857 (30.1 KiB) TX bytes:10545 (10.2 KiB)
Interrupt:16 Base address:0x2080
2.安装ipvsadm管理器
[root@localhost ~]# cd /media/Packages/
[root@localhost Packages]# rpm -ivh ipvsadm-1.25-9.el6.i686.rpm
warning: ipvsadm-1.25-9.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
1:ipvsadm ########################################### [100%]
3.配置VIP 脚本,并给予执行权限,然后执行
[root@localhost ~]# vi /opt/vip.sh
#!/bin/bash
#
VIP=192.168.10.19
/sbin/ifconfig eth1:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth1:0
[root@localhost ~]# chmod +x /opt/vip.sh
[root@localhost ~]# /opt/vip.sh
4.将该脚本加入开机启动项
[root@localhost ~]# echo "/opt/vip.sh" >> /etc/rc.local
[root@localhost ~]# ifconfig eth1:0
eth1:0 Link encap:Ethernet HWaddr 00:0C:29:BD:E7:6F
inet addr:192.168.10.19 Bcast:192.168.10.19 Mask:255.255.255.255
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:16 Base address:0x2080
5.使用ipvsadm命令工具设置DR模式负载均衡群集 (www服务器IP为192.168.10.13-14)
[root@localhost ~]# ipvsadm -A -t 192.168.10.19:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.10.19:80 -r 192.168.10.13 -g
[root@localhost ~]# ipvsadm -a -t 192.168.10.19:80 -r 192.168.10.14 -g
保存ipvsadm设置到/etc/sysconfig/ipvsadm文件以后,下次启动可以直接执行“service ipvsadm start”命令,不再需要逐条输入命令。
[root@localhost ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@localhost ~]# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.10.19:http rr
-> 192.168.10.13:http Local 1 0 0
-> 192.168.10.14:http Route 1 0 0
WWW1的配置
1.配置网络环境
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:FE:39:09
inet addr:172.16.10.13 Bcast:172.16.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fefe:3909/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:23 errors:0 dropped:0 overruns:0 frame:0
TX packets:58 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5569 (5.4 KiB) TX bytes:11304 (11.0 KiB)
Interrupt:59 Base address:0x2024
[root@localhost ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:FE:39:13
inet addr:192.168.10.13 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fefe:3913/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:77 errors:0 dropped:0 overruns:0 frame:0
TX packets:89 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9046 (8.8 KiB) TX bytes:13582 (13.2 KiB)
Interrupt:67 Base address:0x20a4
2.在www1上的设置
[root@localhost ~]# vi /opt/lvs-dr //编写lvs-dr脚本
[root@localhost ~]# vi /opt/lvs-dr
#!/bin/bash
VIP=192.168.10.19
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
3.给予lvs-dr脚本以执行权限,并加入开机启动,然后执行该脚本
[root@localhost ~]# chmod u+x /opt/lvs-dr
[root@localhost ~]# echo "/opt/lvs-dr" >> /etc/rc.local
[root@localhost ~]# /opt/lvs-dr
4.安装apache网站服务
[root@localhost ~]# mount /dev/cdrom /media/
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@localhost ~]# cd /media/Server/
[root@localhost Server]# rpm -ivh httpd-2.2.3-43.el5.i386.rpm
5.编写基本测试页,并开启httpd服务
[root@localhost Server]# echo "1111" > /var/www/html/index.html
[root@localhost Server]# service httpd restart
停止 httpd: [确定]
启动 httpd: [确定]
WWW2的配置
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:4E:07:88
inet addr:172.16.10.14 Bcast:172.16.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4e:788/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:18 errors:0 dropped:0 overruns:0 frame:0
TX packets:63 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4068 (3.9 KiB) TX bytes:12063 (11.7 KiB)
Interrupt:59 Base address:0x2024
[root@localhost ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:4E:07:92
inet addr:192.168.10.14 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4e:792/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:126 errors:0 dropped:0 overruns:0 frame:0
TX packets:87 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:13293 (12.9 KiB) TX bytes:12862 (12.5 KiB)
Interrupt:67 Base address:0x20a4
2.在www2上的设置:将WWW1的lvs-dr脚本传到www2上,并做修改
www1上的操作
[root@localhost Server]# scp /opt/lvs-dr root@192.168.10.14:/opt/
The authenticity of host '192.168.10.14 (192.168.10.14)' can't be established.
RSA key fingerprint is 6e:9d:7a:2d:98:87:46:f1:48:47:f4:04:62:b6:29:4b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.10.14' (RSA) to the list of known hosts.
root@192.168.10.14's password:
lvs-dr
3.给予lvs-dr脚本以执行权限,并加入开机启动,然后执行该脚本
[root@localhost ~]# chmod u+x /opt/lvs-dr
[root@localhost ~]# echo "/opt/lvs-dr" >> /etc/rc.local
[root@localhost ~]# /opt/lvs-dr
4.安装Apache服务,并编写基本测试页,开启httpd服务
[root@localhost ~]# mount /dev/cdrom /media/
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@localhost ~]# cd /media/Server/
[root@localhost Server]# rpm -ivh httpd-2.2.3-43.el5.i386.rpm
warning: httpd-2.2.3-43.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing... ########################################### [100%]
1:httpd ########################################### [100%]
[root@localhost Server]# echo "2222" > /var/www/html/index.html
[root@localhost Server]# service httpd start
启动 httpd: [确定]