LVS-TUN隧道模式

当然可以。以下是按照您的要求整理的表格形式的实验手册:

主机名称 网卡信息 安装应用 系统
Client客户端 192.168.2.101 RHEL8/CentOS8
Lvs服务器(DR) DIP:192.168.2.110 VIP:192.168.40.111 ipvsadm RHEL8/CentOS8
RS1 RIP:192.168.2.111 httpd RHEL8/CentOS8
RS2 RIP:192.168.2.112 httpd RHEL8/CentOS8

这里的每个主机都有其特定的功能和配置:

  • Client客户端:用于发起请求。
  • Lvs服务器(DR):作为负载均衡器,负责接收和分发流量。
  • RS1RS2:作为真实服务器,处理来自负载均衡器的请求。

负载均衡器 (LB)

  1. 保存 IPVS 配置:

    sh 复制代码
    ipvsadm --save > /etc/sysconfig/ipvsadm
  2. 启用 IP 转发:

    sh 复制代码
    echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
    sysctl -p
  3. 配置 TUN 接口:

    sh 复制代码
    ifconfig tunl0 192.168.2.200 broadcast 192.168.2.200 netmask 255.255.255.255 up
  4. 创建虚拟服务器并设置轮询调度:

    sh 复制代码
    ipvsadm -A -t 192.168.2.200:80 -s rr
  5. 添加真实服务器:

    sh 复制代码
    ipvsadm -a -t 192.168.2.200:80 -r 192.168.2.111:80 -i
    ipvsadm -a -t 192.168.2.200:80 -r 192.168.2.112:80 -i
  6. 禁用防火墙与 SELinux:

    sh 复制代码
    systemctl stop firewalld.service
    setenforce 0
  7. 验证 IPVS 配置:

    sh 复制代码
    ipvsadm -ln

真实服务器 1 (RS1)

  1. 启动 HTTP 服务:

    sh 复制代码
    systemctl start httpd
  2. 禁用 SELinux 和防火墙:

    sh 复制代码
    setenforce 0
    systemctl stop firewalld.service
  3. 配置网站内容:

    sh 复制代码
    echo "this is web1" >> /var/www/html/index.html
  4. 测试网站:

    sh 复制代码
    curl 127.0.0.1
  5. 配置 TUN 接口:

    sh 复制代码
    ifconfig tunl0 192.168.2.200 broadcast 192.168.2.200 netmask 255.255.255.255 up
  6. 调整 sysctl 设置:

    sh 复制代码
    echo "net.ipv4.conf.tunl0.arp_ignore = 1" >> /etc/sysctl.conf
    echo "net.ipv4.conf.tunl0.arp_announce = 2" >> /etc/sysctl.conf
    echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
    echo "net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf
    echo "net.ipv4.conf.tunl0.rp_filter = 0" >> /etc/sysctl.conf
    echo "net.ipv4.conf.all.rp_filter = 0" >> /etc/sysctl.conf
    sysctl -p

真实服务器 2 (RS2)

  1. 启动 HTTP 服务:

    sh 复制代码
    systemctl start httpd
  2. 禁用 SELinux 和防火墙:

    sh 复制代码
    setenforce 0
    systemctl stop firewalld.service
  3. 配置网站内容:

    sh 复制代码
    echo "this is web2" >> /var/www/html/index.html
  4. 测试网站:

    sh 复制代码
    curl 127.0.0.1
  5. 配置 TUN 接口:

    sh 复制代码
    ifconfig tunl0 192.168.2.200 broadcast 192.168.2.200 netmask 255.255.255.255 up
  6. 调整 sysctl 设置:

    sh 复制代码
    echo "net.ipv4.conf.tunl0.arp_ignore = 1" >> /etc/sysctl.conf
    echo "net.ipv4.conf.tunl0.arp_announce = 2" >> /etc/sysctl.conf
    echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
    echo "net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf
    echo "net.ipv4.conf.tunl0.rp_filter = 0" >> /etc/sysctl.conf
    echo "net.ipv4.conf.all.rp_filter = 0" >> /etc/sysctl.conf
    sysctl -p

测试

  • 测试负载均衡器:

    sh 复制代码
    curl 192.168.2.200
    sh 复制代码
    [root@localhost ~]# curl 192.168.2.200
    this is web2
    [root@localhost ~]# curl 192.168.2.200
    this is web1
    [root@localhost ~]# curl 192.168.2.200
    this is web2
    [root@localhost ~]# curl 192.168.2.200
    this is web1
    [root@localhost ~]# curl 192.168.2.200
    this is web2
    [root@localhost ~]# curl 192.168.2.200
    this is web1
    [root@localhost ~]# curl 192.168.2.200
    this is web2
    [root@localhost ~]# curl 192.168.2.200
    this is web1