环境:
主机 | IP地址 | 系统 |
---|---|---|
LVS | 192.168.10.128/24 192.168.119.190/24 | CentOS 7.8 |
web01 | 192.168.119.191/24 192.168.18.231/24 | CentOS 7.8 |
web02 | 192.168.119.192/24 192.168.18.232/24 | CentOS 7.8 |
NFS Server | 192.168.18.233/24 | CentOS 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>