LVS NAT模式搭建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS NAT模式搭建相关的知识,希望对你有一定的参考价值。
准备工作三台机器上都执行执行
systemctl stop firewalld; systemctl disable firewalld
安装iptables-services
systemctl enable iptables; systemctl start iptables; iptables -F; service iptables save
setenforce 0
分发器的设置
配置内网ip 192.168.130.116
[[email protected] ~]# 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
配置外网ip 192.168.214.116 (vmware仅主机模式)
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens37
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=ens37
DEVICE=ens37
ONBOOT=no
IPADDR=192.168.214.116
安装ipvsadm
[[email protected] ~]# yum install -y ipvsadm
编写脚本/usr/local/sbin/lvs_nat.sh
[[email protected] ~]# 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
# director 设置nat防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.130.0/24 -j MASQUERADE
# director设置ipvsadm
IPVSADM=‘/usr/sbin/ipvsadm‘
$IPVSADM -C
$IPVSADM -A -t 192.168.214.116:80 -s wlc
$IPVSADM -a -t 192.168.214.116:80 -r 192.168.130.118:80 -m -w 1
$IPVSADM -a -t 192.168.214.116:80 -r 192.168.130.128:80 -m -w 1
加载脚本/usr/local/sbin/lvs_nat.sh
[[email protected] ~]# sh /usr/local/sbin/lvs_nat.sh
rs1的设置
配置内网ip 192.168.130.118
配置网关 192.168.130.116 (这里的网关为分发器的内网ip)
[[email protected] ~]# 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.116
DNS1=119.29.29.29
安装nginx 需要在能外网通信的时候安装
[[email protected] ~]# yum install -y nginx
启动nginx
[[email protected] ~]# systemctl start nginx
[[email protected] ~]# ps aux |grep nginx
root 2131 0.0 0.2 122908 2244 ? Ss 16:03 0:00 nginx: master process /usr/sbin/nginx
nginx 2132 0.0 0.3 123292 3596 ? S 16:03 0:00 nginx: worker process
nginx 2133 0.0 0.3 123292 3596 ? S 16:03 0:00 nginx: worker process
nginx 2134 0.0 0.3 123292 3592 ? S 16:03 0:00 nginx: worker process
nginx 2135 0.0 0.3 123292 3600 ? S 16:03 0:00 nginx: worker process
root 2142 0.0 0.0 112676 984 pts/0 S+ 16:09 0:00 grep --color=auto nginx
给nginx默认主页设置一个标志方便测试
[[email protected] ~]# echo ‘lynn-05‘ > /usr/share/nginx/html/index.html
[[email protected] ~]# cat /usr/share/nginx/html/index.html
lynn-05
rs2的设置
配置内网ip 192.168.130.128
配置网关 192.168.130.116 (这里的网关为分发器的内网ip)
[[email protected] ~]# 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.116
DNS1=119.29.29.29
安装nginx 需要在能外网通信的时候安装
[[email protected] ~]# yum install -y nginx
启动nginx
[[email protected] ~]# systemctl start nginx
[[email protected] ~]# ps aux |grep nginx
root 3615 0.0 0.2 122908 2244 ? Ss 11:03 0:00 nginx: master process /usr/sbin/nginx
nginx 3616 0.0 0.3 123292 3348 ? S 11:03 0:00 nginx: worker process
nginx 3617 0.0 0.3 123292 3600 ? S 11:03 0:00 nginx: worker process
nginx 3618 0.0 0.3 123292 3600 ? S 11:03 0:00 nginx: worker process
nginx 3619 0.0 0.3 123292 3600 ? S 11:03 0:00 nginx: worker process
root 53455 0.0 0.0 112676 984 pts/0 S+ 16:10 0:00 grep --color=auto nginx
给nginx默认主页设置一个标志方便测试
[[email protected] ~]# echo ‘lynn-06‘ > /usr/share/nginx/html/index.html
[[email protected] ~]# cat /usr/share/nginx/html/index.html
lynn-06
测试
通过外网ip访问
[[email protected] ~]# curl 192.168.214.116
lynn-05
[[email protected] ~]# curl 192.168.214.116
lynn-06
[[email protected] ~]# curl 192.168.214.116
lynn-05
[[email protected] ~]# curl 192.168.214.116
lynn-06
[[email protected] ~]# curl 192.168.214.116
lynn-05
[[email protected] ~]# curl 192.168.214.116
lynn-06
以上是关于LVS NAT模式搭建的主要内容,如果未能解决你的问题,请参考以下文章
负载均衡集群介绍 LVS介绍 LVS调度算法 LVS NAT模式搭建