#! /bin/bash # director 服务器上开启路由转发功能 echo 1 > /proc/sys/net/ipv4/ip_forward # 关闭icmp的重定向 echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects # 注意区分网卡名字,阿铭的两个网卡分别为ens33和ens37 echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects echo 0 > /proc/sys/net/ipv4/conf/ens37/send_redirects # director 设置nat防火墙 iptables -t nat -F iptables -t nat -X iptables -t nat -A POSTROUTING -s 192.168.180.0/24 -j MASQUERADE # director设置ipvsadm IPVSADM='/usr/sbin/ipvsadm' $IPVSADM -C $IPVSADM -A -t 192.168.142.144:80 -s wlc -p 3 $IPVSADM -a -t 192.168.142.144:80 -r 192.168.180.135:80 -m -w 1 $IPVSADM -a -t 192.168.142.144:80 -r 192.168.180.138:80 -m -w 1
A机器IP为192.168.180.13四、B机器IP为192.168.180.135,C机器IP为192.168.180.138html
PS:网关最后设置,不然包没法下载linux
B机器和C机器的网关必须设置成分发器(即A机器)的内网IP,若不设置成它的网关,是无法通讯的nginx
网卡配置文件中更改(即本机器) vi /etc/sysconfig/network-scripts/ifcfg-ens33 更改完以后重启网络服务 systemctl restart network [root@yong-02 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.180.134 0.0.0.0 UG 100 0 0 ens33 192.168.180.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
关闭firewalld服务 systemctl stop firewalld 使firewalld服务再也不开机启动 systemctl disable firewalld
yum install -y iptables-services
机器B [root@yong-02 ~]# systemctl start iptables 机器C [root@yong-03 ~]# systemctl start iptables
机器B [root@yong-02 ~]# systemctl start iptables [root@yong-02 ~]# systemctl enable iptables Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. [root@yong-02 ~]# iptables -F [root@yong-02 ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 肯定 ] [root@yong-02 ~]# iptables -nvL Chain INPUT (policy ACCEPT 74 packets, 4884 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 42 packets, 3952 bytes) pkts bytes target prot opt in out source destination 机器C同上
[root@yong-01 ~]# iptables -nvL Chain INPUT (policy ACCEPT 27 packets, 1888 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 20 packets, 1968 bytes) pkts bytes target prot opt in out source destination
setenforce 0 //临时关闭selinux getenforce //查看selinux是否关闭 为了保险起见,在配置文件中永久关闭selinux vi /etc/selinux/config SELINUX=enforcing更改成SELINUX=disabled
NAT模式搭建web
[root@yong-01 ~]# yum install -y ipvsadm
[root@yong-01 ~]# vim /usr/local/sbin/lvs_nat.sh #! /bin/bash # director 服务器上开启路由转发功能 echo 1 > /proc/sys/net/ipv4/ip_forward //对内核参数修改,打开路由转发 # 关闭icmp的重定向 echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects //假装操做,否则不能转发rs的数据 echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects //假装操做,否则不能转发rs的数据 # 注意区分网卡名字,dir机器的两块网卡分别为ens33和ens37 echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects echo 0 > /proc/sys/net/ipv4/conf/ens37/send_redirects # director 设置nat防火墙 iptables -t nat -F iptables -t nat -X iptables -t nat -A POSTROUTING -s 192.168.180.0/24 -j MASQUERADE //MASQUERADE实现同网段的机器去上网,路由器使用的就是这个功能 # director设置ipvsadm IPVSADM='/usr/sbin/ipvsadm' //设置一个变量,方便下面命令引用 $IPVSADM -C //清空规则 $IPVSADM -A -t 192.168.142.144:80 -s lc -p 3 //用来定义lvs 的模式;wlc,为算法,能够按需求选择 lvs 里面适合的算法 $IPVSADM -a -t 192.168.142.144:80 -r 192.168.180.135:80 -m -w 1 //小规则,-r 指定dir机器IP,-m 指定nat模式,-w指定rs权重 $IPVSADM -a -t 192.168.142.144:80 -r 192.168.180.138:80 -m -w 1 //小规则,-r 指定dir机器IP,-m 指定nat模式,-w指定rs权重
[root@yong-01 ~]# sh /usr/local/sbin/lvs_nat.sh
[root@yong-02 ~]# vim /usr/share/nginx/html/index.html [root@yong-02 ~]# curl localhost yongge02.
[root@yong-03 ~]# vim /usr/local/nginx/html/index.html [root@yong-03 ~]# curl localhost yongge03.
[root@yong-01 ~]# curl 192.168.142.144 yongge02. [root@yong-01 ~]# curl 192.168.142.144 yongge03.
[root@yong-01 ~]# iptables -t nat -nvL Chain PREROUTING (policy ACCEPT 29 packets, 5245 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 25 packets, 1610 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 1 76 MASQUERADE all -- * * 192.168.180.0/24 0.0.0.0/0
[root@yong-01 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.142.144:80 rr -> 192.168.180.135:80 Masq 1 0 6 -> 192.168.180.138:80 Masq 1 0 6