LVS-NAT部署

环境:

主机IP地址系统
LVS192.168.10.128/24
192.168.119.190/24
CentOS 7.8
web01192.168.119.191/24
192.168.18.231/24
CentOS 7.8
web02192.168.119.192/24
192.168.18.232/24
CentOS 7.8
NFS Server192.168.18.233/24CentOS 7.8

一、基本环境配置

1. IP地址配置

2. 主机名设置

# lvs
$ hostnamectl set-hostname lvs

# web01
$ hostnamectl set-hostname web01

# web02
$ hostnamectl set-hostname web-02

# nfs-server
$ hostnamectl set-hostname nfs-server

3. 关闭Selinux

# 临时
$ setenforce 0

# 永久
$ sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

4. 设置web网关指向LVS

# web01
$ nmcli connection modify ens33 ipv4.gateway 192.168.119.190
$ nmcli con up ens33

# web02
$ nmcli connection modify ens33 ipv4.gateway 192.168.119.190
$ nmcli con up ens33

5. 防火墙

$ systemctl stop firewalld
$ systemctl disable firewalld

二、配置LVS

1. 安装ipvsadm

$ yum install ipvsadm -y

2. 开启路由功能

$ echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
$ sysctl -p

3. 加载ip_vs模块

$ modprobe ip_vs
$ lsmod | grep ip_vs
ip_vs                 145497  0 
nf_conntrack          139264  7 ip_vs,nf_nat,nf_nat_ipv4,nf_nat_ipv6,xt_conntrack,nf_conntrack_ipv4,nf_conntrack_ipv6
libcrc32c              12644  4 xfs,ip_vs,nf_nat,nf_conntrack
$ 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

4. 启动ipvsadm服务

$ ipvsadm --save > /etc/sysconfig/ipvsadm
$ systemctl start ipvsadm
$ systemctl enable ipvsadm
Created symlink from /etc/systemd/system/multi-user.target.wants/ipvsadm.service to /usr/lib/systemd/system/ipvsadm.service.
$ systemctl status ipvsadm
● ipvsadm.service - Initialise the Linux Virtual Server
   Loaded: loaded (/usr/lib/systemd/system/ipvsadm.service; enabled; vendor preset: disabled)
   Active: active (exited) since Mon 2021-03-22 04:37:01 EDT; 24s ago
 Main PID: 3050 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/ipvsadm.service

Mar 22 04:37:01 lvs systemd[1]: Starting Initialise the Linux Virtual Server...
Mar 22 04:37:01 lvs systemd[1]: Started Initialise the Linux Virtual Server.
5. lvs_rule 脚本
# 编写
$ vim lvs_rule.sh
#!/bin/bash
ipvsadm -C
ipvsadm -A -t 192.168.10.128:80 -s rr
ipvsadm -A -t 192.168.10.128:80 -r 192.168.119.191:80 -m
ipvsadm -A -t 192.168.10.128:80 -r 192.168.119.192:80 -m
ipvsadm -L -n &> /dev/null

# 授权
$ chmod  x lvs_rule.sh

# 执行
$ source lvs_rule.sh 
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.10.128:80 rr
  -> 192.168.119.191:80           Masq    1      0          0         
  -> 192.168.119.192:80           Masq    1      0          0
  
# 设置开机自动执行
$ tail -1 /etc/rc.d/rc.local 
source /root/lvs_rule.sh 

三、配置网站

1. web服务器安装Apache

$ yum install httpd -y

2. 设置网站界面

# web01
$ vim /var/www/html/index.html
<h1>This is web01 page!</h1>

# web02
$ vim /var/www/html/index.html
<h1>This is web02 page!</h1>

3. 启动服务

$ systemctl start httpd
$ systemctl enable httpd

四、访问测试(使用LVS服务器)

1. web01

$ curl 192.168.119.191
<h1>This is web01 page!</h1>

2. web02

$ curl 192.168.119.192
<h1>This is web02 page!</h1>

五、NFS服务器

1. 安装nfs-utils

# 安装
$ yum install nfs-utils -y

# 启动
$ systemctl start nfs-server
$ systemctl enable nfs-server

2. 配置共享目录

# 创建
$ mkdir /webroot

# 配置文件
$ cat /etc/exports
/webroot 192.168.18.0/24(sync,rw,no_root_squash)
$ cat /webroot/index.html 
<h1>This is a test page!</h1>

# 重启
$ systemctl restart nfs-server

六、挂载NFS目录

1. web服务器安装nfs-utils

$ yum install nfs-utils -y

2. 临时挂载

$ mount 192.168.18.233:/webroot /var/www/html/

3. 永久挂载

$ tail -1 /etc/fstab 
192.168.18.233:/webroot /var/www/html		nfs	defaults 0 0

七、实现LVS-NAT

$ curl 192.168.119.191
<h1>This is a test page!</h1>
$ curl 192.168.119.192
<h1>This is a test page!</h1>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值