实验一LVS-DR
1. 实验环境
真实主机:192.168.8.137
192.168.8.138
Director:192.168.8.140
虚拟IP:192.168.8.141
2. Director主机上
[root@xss ~]# yum -y install ipvsadm //安装ipvsadm
[root@xss ~]# ipvsadm -A -t 192.168.8.141:80 -s rr //添加虚拟主机
[root@xss ~]# ipvsadm -a -t 192.168.8.141:80 -r 192.168.8.137:80 -g -w 1 //添加真实主机
[root@xss ~]# ipvsadm -a -t 192.168.8.141:80 -r 192.168.8.138:80 -g -w 1 //添加真实主机
[root@xss ~]# ipvsadm-save > /etc/sysconfig/ipvsadm //保存ipvsadm
[root@xss ~]# service ipvsadm restart
[root@xss ~]# ipvsadm -Lnc
[root@xss ~]# vim /etc/sysctl.conf //打开ipv4转发
net.ipv4.ip_forward = 1
[root@xss ~]#sysctl –p //生效
[root@xss ~]# ifconfig eth0:0 192.168.8.141 broadcast 192.168.8.141 netmask 255.255.255.255 up //配置集群接口
[root@xss ~]# route add -host 192.168.8.141 dev eth0:0 //添加集群路由
3. 两台真实主机上
[root@xss ~]# yum -y install httpd //安装http服务
[root@xss ~]#vim /var/www/html/index.html //编写测试页
[root@xss ~]# vim /etc/sysctl.conf
关闭响应ARP请求(在文件末尾添加如下内容)
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@xss ~]# vim /etc/sysctl.conf //打开ipv4转发
net.ipv4.ip_forward = 1
[root@xss ~]#sysctl –p //生效
[root@xss ~]# ifconfig lo:0 192.168.8.141 broadcast 192.168.8.141 netmask 255.255.255.255 up //配置集群接口
[root@xss html]# route add -host 192.168.8.141 dev lo:0 //添加集群路由
注意:在 DR 模式下,2台 rs 节点的 gateway 不需要设置成 dir 节点的 IP
4. 浏览器访问192.168.8.141
刷新出现
实验二LVS-NAT
1. 实验环境
真实主机:172.16.8.137 (网关指向DR主机为172.16.8.1)
172.16.8.138 (网关指向DR主机为172.16.8.1)
Director:Eth0:192.168.8.140
Eth1:172.16.8.1(掩码:255.255.0.0,不配置网关)
2. Director主机上
[root@xss ~]# ipvsadm -A -t 192.168.8.140:80 -s rr
[root@xss ~]# ipvsadm -a -t 192.168.8.140:80 -r 172.16.8.137:80 -m
[root@xss ~]# ipvsadm -a -t 192.168.8.140:80 -r 172.16.8.138:80 -m
[root@xss ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@xss ~]# service ipvsadm restart
[root@xss ~]# vim /etc/sysctl.conf //打开ipv4转发
net.ipv4.ip_forward = 1
[root@xss ~]#sysctl -p
[root@xss ~]#modprobe iptable_nat //添加模块
[root@xss ~]#service iptables start
[root@xss ~]#iptables -F
[root@xss ~]#iptables -t nat -A POSTROUTING -s 172.16.8.0/16 -o eth0 -j SNAT --to-source 192.168.8.140
3. 两台真实主机上
[root@xss ~]# yum -y install httpd //安装http服务
[root@xss ~]#vim /var/www/html/index.html //编写网页
[root@xss ~]# vim /etc/sysctl.conf //打开ipv4转发
net.ipv4.ip_forward = 1
[root@xss ~]#sysctl -p
注意:切记一定要在两台 RS 上设置网关的 IP 为 director 的内网 IP。
虚拟ip为director的外网网卡地址
4. 浏览器访问192.168.8.140
刷新一下出现