LVS NAT模式
LVS IP Tunnel模式
LVS DR模式
NAT模式搭建-准备工作
[root@taoyuan ~]# hostnamectl set-hostname qingyun-01
#进入个子shell
[root@taoyuan ~]# bash
[root@qingyun-01 ~]#
[root@qingyun-02 html]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.130 0.0.0.0 UG 100 0 0 ens33
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
[root@qingyun-03 html]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.130 0.0.0.0 UG 100 0 0 ens33
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
#由于3台是重新,都关闭防火墙
[root@qingyun-01 ~]# systemctl stop firewalld
[root@qingyun-01 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@qingyun-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 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 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[root@qingyun-03 ~]# yum install -y iptables-services
#如果安装很慢,可以取消epel
#把/etc/yum.repos.d/目录下epel.repo 改一下名字
#查看包安装的文件
[root@qingyun-01 yum.repos.d]# rpm -ql iptables-services
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables
/usr/lib/systemd/system/ip6tables.service
/usr/lib/systemd/system/iptables.service
#启动iptables.service
[root@qingyun-01 yum.repos.d]# systemctl start iptables
[root@qingyun-01 yum.repos.d]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
#开启的目的是为了调用一个空的规则
[root@qingyun-03 ~]# iptables -F
[root@qingyun-03 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 确定 ]
#关闭selinux
[root@qingyun-01 yum.repos.d]# setenforce 0
[root@qingyun-01 yum.repos.d]# vi /etc/selinux/config
#SELINUX=disabled
#查看网关
[root@qingyun-03 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 ens33
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
NAT模式搭建
[root@qingyun-01 yum.repos.d]# yum install -y ipvsadm
[root@qingyun-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
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
# dirrector 设置nat防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE
# director设置ipvsadm
IPVSADM=‘/usr/sbin/ipvsadm‘
$IPVSADM -C
$IPVSADM -A -t 192.168.147.144:80 -s wlc -p 3
$IPVSADM -a -t 192.168.147.144:80 -r 192.168.0.132:80 -m -w 1
$IPVSADM -a -t 192.168.147.144:80 -r 192.168.0.133:80 -m -w 1
[root@qingyun-01 ~]# sh /usr/local/sbin/lvs_nat.sh
#没有输出,说明没有错误
NAT模式效果测试
两台rs上都安装nginx
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-132
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-133
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-132
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-133
[root@qingyun-01 ~]# cat /usr/local/sbin/lvs_nat.sh
...........
$IPVSADM -C
$IPVSADM -A -t 192.168.147.144:80 -s wlc
.............
[root@qingyun-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.147.144:80 wlc
-> 192.168.0.132:80 Masq 1 0 0
-> 192.168.0.133:80 Masq 1 0 4
负载均衡集群介绍、LVS介绍、LVS调度算法、LVS NAT模式搭建
原文:http://blog.51cto.com/3622288/2066624