实验前提:
- 主机server1:172.25.55.1(配置yum源,下载httpd)
- 主机server2:172.25.55.2(配置yum源,下载httpd,编辑网页)
- 主机server3:172.25.55.3(配置yum源,下载httpd,编辑网页)
DR模式要求虚拟ip与真实ip在同一网段
实验步骤
添加yum源
[root@server1 yum.repos.d]# vim rhel-source.repo
[rhel-source]
name=Red
baseurl=http://172.25.55.250/wanggua1
gpgcheck=0
[LoadBalancer]
name=kk
baseurl=http://172.25.55.250/wanggua1/LoadBalancer
gpgcheck=0
[root@server1 yum.repos.d]# yum clean all
[root@server1 yum.repos.d]# yum repolist
repo id repo name status
LoadBalancer kk 4(多了四个安装包)
rhel-source Red 3,690
[root@server1 yum.repos.d]# yum install ipvsadm.x86_64
[root@server1 yum.repos.d]# ipvsadm -A -t 172.25.55.100:80 -s rr
[root@server1 yum.repos.d]# ipvsadm -a -t 172.25.55.100:80 -r 172.25.55.3:80 -g
[root@server1 yum.repos.d]# ipvsadm -a -t 172.25.55.100:80 -r 172.25.55.2:80 -g
[root@server1 yum.repos.d]# ipvsadm -l 查看策略
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.55.100:http rr
-> server2:http (主机名,服务) Route 1 0 0
-> server3:http (主机名,服务) Route 1 0 0
[root@server1 ~]# ipvsadm -C (如果写错策略,删除)
[root@server1 yum.repos.d]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.55.100:80 rr
-> 172.25.55.2:80 (主机号,服务端口号) Route 1 0 0
-> 172.25.55.3:80 (主机号,服务端口号) Route 1 0 0
[root@server1 yum.repos.d]# ip addr add 172.25.55.100/32 dev eth0(为server1添加虚拟ip)
[root@server1 yum.repos.d]# ip addr show(查看ip)
[root@server2 ~]# ip addr add 172.25.55.100/32 dev eth0 (server2加上虚拟ip)
[root@server3 ~]# ip addr add 172.25.55.100/32 dev eth0 (server3加上虚拟ip)
测试
[root@foundation55 ~]# curl 172.25.55.100
www.westos.com server3
[root@foundation55 ~]# curl 172.25.55.100
www.westos.com server3
[root@foundation55 ~]# arp -an |grep 100(查看mac地址)
? (172.25.55.100) at 52:54:00:44:3c:7a [ether] on br0(此时mac地址是server3虚拟机)
[root@foundation55 ~]# arp -d 172.25.55.100(清除缓存)
[root@foundation55 ~]# curl 172.25.55.100
www.westos.com server3
[root@foundation55 ~]# curl 172.25.55.100
HELLO server2
[root@foundation55 ~]# curl 172.25.55.100
www.westos.com server3
[root@foundation55 ~]# arp -an |grep 100
? (172.25.55.100) at 52:54:00:d1:f9:32 [ether] on br0(此时mac地址是server1虚拟机)
[root@foundation55 ~]#
[root@server1 yum.repos.d]# /etc/init.d/ipvsadm save(保存操作)
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [ OK ]
[root@server1 yum.repos.d]# cat /etc/sysconfig/ipvsadm(可通过修改文件修改策略)
-A -t 172.25.55.100:80 -s rr
-a -t 172.25.55.100:80 -r 172.25.55.2:80 -g -w 1
-a -t 172.25.55.100:80 -r 172.25.55.3:80 -g -w 1
不访问真实主机的操作
访问时不访问真实主机,只能访问到server1.如果用户直接访问server2,server3会使服务器压力过大。
-
server2操作
[root@server2 ~]# yum install -y arptables_jf [root@server2 ~]# arptables -A IN -d 172.25.55.100 -j DROP [root@server2 ~]# arptables -A OUT -s 172.25.55.100 -j mangle --mangle-ip-s 172.25.55.2 [root@server2 ~]# arptables -L 查看策略 [root@server2 ~]# /etc/init.d/arptables_jf save 保存操作 Saving current rules to /etc/sysconfig/arptables: [ OK ] [root@server2 ~]# cat /etc/sysconfig/arptables 查看文件
-
server3操作
[root@server2 ~]# yum install -y arptables_jf [root@server2 ~]# arptables -A IN -d 172.25.55.100 -j DROP [root@server2 ~]# arptables -A OUT -s 172.25.55.100 -j mangle --mangle-ip-s 172.25.55.3 [root@server2 ~]# arptables -L 查看策略 [root@server2 ~]# /etc/init.d/arptables_jf save 保存操作 Saving current rules to /etc/sysconfig/arptables: [ OK ] [root@server2 ~]# cat /etc/sysconfig/arptables 查看文件
测试
此时只能访问到server1,访问不到真实主机上。