前言:在NAT模式的群集中,LVS负载调度器是所有节点访问Internet的网关服务器。其外网地址同时也作为整个群集的VIP地址。LVS调度器具有两块网卡,分别连接内外网
对于LVS负载调度器来说,需使用iptables为出站响应配置SNAT转发规则,以便节点服务器能够访问Internet。所有的节点服务器、共享存储均位于私有网络内,其默认网关设为LVS负载调度器的内网地址
下面做个简单的实验,让win10客户机访问12.0.0.1的网址,通过nat地址转换,轮询的访问到Apache1和Apache2主机;并搭建出nfs网络文件存储服务
[root@nfs ~]# fdisk /dev/sdb //sdb配置
Welcome to fdisk (util-linux 2.23.2).
Command (m for help): n //创建
。。。。省略部分内容
Command (m for help): w //保存
Calling ioctl() to re-read partition table.
Syncing disks.
[root@nfs ~]# mkfs.xfs /dev/sdb1 //格式化
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=1310656 blks
。。。省略部分内容
[root@nfs ~]# mkdir /opt/csdn /opt/accp //创建挂载点
[root@nfs ~]# mkfs.xfs /dev/sdc1 //sdc配置
meta-data=/dev/sdc1 isize=512 agcount=4, agsize=1310656 blks
。。。省略部分内容
[root@nfs ~]# vim /etc/fstab
/dev/sdb1 /opt/csdn xfs defaults 0 0
/dev/sdc1 /opt/accp xfs defaults 0 0
[root@nfs ~]# mount -a //重载
[root@nfs ~]# df -hT //查看
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 20G 13G 8.0G 61% /
devtmpfs devtmpfs 897M 0 897M 0% /dev
tmpfs tmpfs 912M 0 912M 0% /dev/shm
tmpfs tmpfs 912M 9.1M 903M 1% /run
tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 xfs 6.0G 179M 5.9G 3% /boot
/dev/mapper/centos-home xfs 10G 33M 10G 1% /home
tmpfs tmpfs 183M 4.0K 183M 1% /run/user/42
tmpfs tmpfs 183M 20K 183M 1% /run/user/0
/dev/sdb1 xfs 20G 33M 20G 1% /opt/csdn
/dev/sdc1 xfs 20G 33M 20G 1% /opt/accp
[root@nfs ~]# rpm -q nfs-utils //nfs组件
nfs-utils-1.3.0-0.48.el7.x86_64
[root@nfs ~]# rpm -q rpcbind //远程调用包
rpcbind-0.2.0-42.el7.x86_64
[root@nfs ~]# vim /etc/exports //提供挂载点目录的存储空间
/opt/csdn 192.168.100.0/24(rw,sync,no_root_squash) //提供读写,sync,不做降级处理功能
/opt/accp 192.168.100.0/24(rw,sync,no_root_squash)
[root@nfs ~]# systemctl start nfs //开启服务
[root@nfs ~]# systemctl start rpcbind
[root@nfs ~]# systemctl stop firewalld.service //关闭防火墙
[root@nfs ~]# setenforce 0
[root@nfs ~]# showmount -e //查看nfs路径是否提供出来
Export list for nfs:
/opt/accp 192.168.9.0/24
/opt/csdn 192.168.9.0/24
[root@nfs ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 //更改网卡
4 BOOTPROTO=static
16 IPADDR=192.168.100.120
17 NETMASK=255.255.255.0
18 GATEWAY=192.168.100.1
[root@nfs ~]# service network restart
Restarting network (via systemctl): [ 确定 ]
[root@nfs ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.100.120
。。。省略部分内容
Web1:
[root@Web1 ~]# yum install httpd -y
[root@Web1 ~]# systemctl stop firewalld.service
[root@Web1 ~]# setenforce 0
切换仅主机模式
[root@Web1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 //更改网卡
4 BOOTPROTO=static
16 IPADDR=192.168.100.110
17 NETMASK=255.255.255.0
18 GATEWAY=192.168.100.1
[root@Web1 ~]# service network restart
Restarting network (via systemctl): [ 确定 ]
[root@Web1 ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.100.110
Web2:
[root@Web2 ~]# yum install httpd -y
[root@Web2 ~]# systemctl stop firewalld.service
[root@Web2 ~]# setenforce 0
切换仅主机模式
[root@Web2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 //更改网卡
4 BOOTPROTO=static
16 IPADDR=192.168.100.111
17 NETMASK=255.255.255.0
18 GATEWAY=192.168.100.1
[root@Web2 ~]# service network restart
Restarting network (via systemctl): [ 确定 ]
[root@Web2 ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.100.111
Web1:
[root@Web1 ~]# showmount -e 192.168.100.120 //用showmount查看
Export list for 192.168.100.120:
/opt/accp 192.168.100.0/24
/opt/csdn 192.168.100.0/24
[root@Web1 ~]# vim /etc/fstab //设置自动挂载
192.168.100.120:/opt/csdn /var/www/html nfs defaults,_netdev 0 0
[root@Web1 ~]# mount -a //更新挂载
[root@Web1 ~]# df -hT //查看
文件系统 类型 容量 已用 可用 已用% 挂载点
192.168.100.120:/opt/csdn nfs4 20G 32M 20G 1% /var/www/html
Web2:
[root@Web2 ~]# showmount -e 192.168.100.120
Export list for 192.168.100.120:
/opt/accp 192.168.100.0/24
/opt/csdn 192.168.100.0/24
[root@Web2 ~]# vim /etc/fstab
192.168.100.120:/opt/accp /var/www/html nfs defaults,_netdev 0 0
[root@Web2 ~]# mount -a
[root@Web2 ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
192.168.100.120:/opt/accp nfs4 20G 32M 20G 1% /var/www/html
Web1:
[root@Web1 ~]# cd /var/www/html/
[root@Web1 html]# vim index.html
this is csdn
[root@Web1 html]# systemctl start httpd
[root@Web1 html]# netstat -ntap | grep 80
tcp6 0 0 :::80 :::* LISTEN 56638/httpd
Web2:
[root@Web2 ~]# cd /var/www/html/
[root@Web2 html]# vim index.html
this is accp
[root@Web2 html]# systemctl start httpd
[root@Web2 html]# netstat -ntap | grep 80
tcp6 0 0 :::80 :::* LISTEN 56311/httpd
[root@LVS network-scripts]# modprobe ip_vs //加载LVS内核模块
[root@LVS network-scripts]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@LVS network-scripts]# yum install ipvsadm -y //安装工具
[root@LVS ~]# cd /etc/sysconfig/network-scripts/
[root@LVS network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@LVS network-scripts]# vim ifcfg-ens33
BOOTPROTO=static
IPADDR=192.168.100.1
NETWASK=255.255.255.0
[root@LVS network-scripts]# vim ifcfg-ens36
BOOTPROTO=static
NAME=ens36
DEVICE=ens36
IPADDR=12.0.0.1
NETMASK=255.255.255.0
[root@LVS network-scripts]# service network restart
Restarting network (via systemctl): [ 确定 ]
[root@LVS network-scripts]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.100.1 netmask 255.255.255.0 broadcast 192.168.100.255
。。省略部分内容
ens36: flags=4163 mtu 1500
inet 12.0.0.1 netmask 255.255.255.0 broadcast 12.0.0.255
。。省略部分内容
[root@Web1 html]# ping 192.168.100.1
PING 192.168.100.1 (192.168.100.1) 56(84) bytes of data.
64 bytes from 192.168.100.1: icmp_seq=1 ttl=64 time=1.54 ms
64 bytes from 192.168.100.1: icmp_seq=2 ttl=64 time=1.21 ms
64 bytes from 192.168.100.1: icmp_seq=3 ttl=64 time=0.659 ms
^C
--- 192.168.100.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.659/1.138/1.542/0.366 ms
[root@Web2 html]# ping 192.168.100.1
PING 192.168.100.1 (192.168.100.1) 56(84) bytes of data.
64 bytes from 192.168.100.1: icmp_seq=1 ttl=64 time=0.725 ms
64 bytes from 192.168.100.1: icmp_seq=2 ttl=64 time=0.615 ms
^C
--- 192.168.100.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.615/0.670/0.725/0.055 ms
[root@LVS network-scripts]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@LVS network-scripts]# sysctl -p //重载路由转发功能
net.ipv4.ip_forward = 1
[root@LVS network-scripts]# iptables -F //清除转发表
[root@LVS network-scripts]# iptables -t nat -F //清除nat地址转换表
[root@LVS network-scripts]# iptables -t nat -A POSTROUTING -o ens36 -s 192.168.100.0/24 -j SNAT --to-source 12.0.0.1
[root@LVS mnt]# ipvsadm --save > /etc/sysconfig/ipvsadm //保存防火墙规则
[root@LVS mnt]# systemctl start ipvsadm.service //开启服务
[root@LVS mnt]# cd /opt/
[root@LVS opt]# ls
rh
[root@LVS opt]# vim nat.sh //编写一个脚本
#!/bin/bash
ipvsadm -C //清空ipcs缓存(初始化)
ipvsadm -A -t 12.0.0.1:80 -s rr //指定访问入口(外网IP) 指定调度算法rr轮询
ipvsadm -a -t 12.0.0.1:80 -r 192.168.100.110:80 -m //指定添加真实服务器,建立映射关系 -m是指定NAT轮询机制
ipvsadm -a -t 12.0.0.1:80 -r 192.168.100.111:80 -m
ipvsadm //启动
[root@LVS opt]# chmod +x nat.sh
[root@LVS opt]# ./nat.sh
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP LVS:http rr
-> 192.168.100.110:http Masq 1 0 0
-> 192.168.100.111:http Masq 1 0 0