title: LVS系列之二:部署LVS的NAT模式
categories: Linux
tags:
- LVS
timezone: Asia/Shanghai
date: 2019-02-04
简介
Virtual Server via Network Address Translation(VS/NAT)
通过网络地址转换,调度器重写请求报文的目标地址,根据预设的调度算法,将请求分派给后端的真实服务器;真实服务器的响应报文通过调度器时,报文的源地址被重写,再返回给客户,完成整个负载调度过程。
环境
[root@centos181001 ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
准备3台服务器
一台Director server(两个IP,一个内网IP一个外网IP)
两台Real server(只有内网IP,并需要把内网网关设置为Director server的内网IP)。
Director server
外网IP:192.168.0.122
内网IP:11.11.11.61
Real server 1:11.11.11.62
Real server 2:11.11.11.63
第零步:关闭系统默认防火墙(by all)
setenforce 0
sed -i -r "/^SELINUX=/c SELINUX=disabled" /etc/selinux/config
which systemctl && systemctl stop firewalld
which systemctl && systemctl disable firewalld
which systemctl && systemctl stop iptables || service iptables stop
which systemctl && systemctl disable iptables || chkconfig iptables off
第一步:Director server节点设置
1.安装ipvsadm
yum install -y ipvsadm
2.编辑 nat 实现脚本并执行
cat <<EOF >/usr/local/sbin/lvs_nat.sh
# 编辑写入如下内容:
#!/bin/bash
# director服务器上开启路由转发功能:
echo 1 > /proc/sys/net/ipv4/ip_forward
# 关闭 icmp 的重定向(ens33和ens34为两块网卡名,请根据自己情况更改)
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/ens34/send_redirects
# director设置 nat 防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 11.11.11.0/24 -j MASQUERADE
# director设置 ipvsadm
/usr/sbin/ipvsadm -C
/usr/sbin/ipvsadm -A -t 192.168.0.122:80 -s wrr
/usr/sbin/ipvsadm -a -t 192.168.0.122:80 -r 11.11.11.62:80 -m -w 1
/usr/sbin/ipvsadm -a -t 192.168.0.122:80 -r 11.11.11.63:80 -m -w 1
EOF
cat /usr/local/sbin/lvs_nat.sh
chmod +x /usr/local/sbin/lvs_nat.sh
3.执行脚本并查看ipvsadm设置的规则
保存后,在 Director 上直接运行这个脚本就可以完成 lvs/nat 的配置
/usr/local/sbin/lvs_nat.sh
ipvsadm -ln
第二步:两个Real server节点安装httpd
节点1:
yum install -y httpd
systemctl start httpd
echo "1111111111" > /var/www/html/index.html
节点2:
yum install -y httpd
systemctl start httpd
echo "2222222222" > /var/www/html/index.html
第三步:测试
[root@centos181001 ~]# curl http://192.168.0.122
1111111111
[root@centos181001 ~]# curl http://192.168.0.122
2222222222
[root@centos181001 ~]# curl http://192.168.0.122
1111111111
[root@centos181001 ~]# curl http://192.168.0.122
2222222222
[root@centos181001 ~]# curl http://192.168.0.122
1111111111
[root@centos181001 ~]# curl http://192.168.0.122
2222222222
网友评论