分发器的设置

配置ens33网卡

[root@lynn-04 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.116
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

启动nginx 这里nginx是自定义安装的启动文件时自己设置的

[root@lynn-04 ~]# /etc/init.d/nginx start
[root@lynn-04 ~]# ps aux |grep nginx
root        912  0.0  0.1  45988  1240 ?        Ss   08:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody      913  0.0  0.4  48476  4388 ?        S    08:06   0:00 nginx: worker process
nobody      915  0.0  0.4  48476  4392 ?        S    08:06   0:00 nginx: worker process
root       2072  0.0  0.0 112680   980 pts/0    S+   10:23   0:00 grep --color=auto nginx

写/usr/local/sbin/lvs_dr.sh脚本

[root@lynn-04 ~]# vim /usr/local/sbin/lvs_dr.sh

#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.130.100
rs1=192.168.130.118
rs2=192.168.130.128
#注意这里的网卡名字
ifdown ens33
ifup ens33
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev ens33:2
$ipv -C
$ipv -A -t $vip:80 -s rr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1

执行/usr/local/sbin/lvs_dr.sh脚本

[root@lynn-04 ~]# sh /usr/local/sbin/lvs_dr.sh
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/2)

rs1的设置

配置ens33网卡

[root@lynn-05 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.118
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

启动nginx yum安装的nginx

[root@lynn-05 ~]# systemctl start nginx
[root@lynn-05 ~]# ps aux |grep nginx
root       1885  0.0  0.2 122908  2240 ?        Ss   10:19   0:00 nginx: master process /usr/sbin/nginx
nginx      1886  0.2  0.3 123292  3140 ?        S    10:19   0:00 nginx: worker process
nginx      1887  0.2  0.3 123292  3140 ?        S    10:19   0:00 nginx: worker process
nginx      1888  0.1  0.3 123292  3140 ?        S    10:19   0:00 nginx: worker process
nginx      1889  0.0  0.3 123292  3140 ?        S    10:19   0:00 nginx: worker process
root       1891  0.0  0.0 112676   980 pts/0    S+   10:19   0:00 grep --color=auto nginx

写/usr/local/sbin/lvs_rs.sh脚本

[root@lynn-05 ~]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.130.100
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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

执行/usr/local/sbin/lvs_rs.sh脚本

[root@lynn-05 ~]# sh /usr/local/sbin/lvs_rs.sh

rs2的设置

配置ens33网卡

[root@lynn-06 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=fef8a686-6a1e-49bc-b03e-7797fc4c58bb
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.128
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

启动nginx yum安装的nginx

[root@lynn-06 ~]# systemctl start nginx
[root@lynn-06 ~]# ps aux |grep nginx
root       1326  0.0  0.2 122908  2240 ?        Ss   10:19   0:00 nginx: master process /usr/sbin/nginx
nginx      1327  0.0  0.3 123292  3140 ?        S    10:19   0:00 nginx: worker process
nginx      1328  0.0  0.3 123292  3140 ?        S    10:19   0:00 nginx: worker process
nginx      1329  0.0  0.3 123292  3140 ?        S    10:19   0:00 nginx: worker process
nginx      1330  0.0  0.3 123292  3140 ?        S    10:19   0:00 nginx: worker process
root       1332  0.0  0.0 112676   984 pts/0    S+   10:19   0:00 grep --color=auto nginx

写/usr/local/sbin/lvs_rs.sh脚本

[root@lynn-06 ~]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.130.100
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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

执行/usr/local/sbin/lvs_rs.sh脚本

[root@lynn-06 ~]# sh /usr/local/sbin/lvs_rs.sh

测试

在以上三台服务器外的机器上测试

[root@lynn-02 ~]# curl 192.168.130.100
lynn-05
[root@lynn-02 ~]# curl 192.168.130.100
lynn-06
[root@lynn-02 ~]# curl 192.168.130.100
lynn-05
[root@lynn-02 ~]# curl 192.168.130.100
lynn-06
[root@lynn-02 ~]# curl 192.168.130.100
lynn-05
[root@lynn-02 ~]# curl 192.168.130.100
lynn-06
[root@lynn-02 ~]# curl 192.168.130.100
lynn-05
[root@lynn-02 ~]# curl 192.168.130.100
lynn-06