• LVS实战笔记-DR单网段


    LVS实战笔记-DR单网段

    创建虚拟机
    set vmcentos[0]=CentOS7X64-Client
    set vmcentos[1]=CentOS7X64-Gateway
    set vmcentos[2]=CentOS7X64-LVS101
    set vmcentos[3]=CentOS7X64-RS201
    set vmcentos[4]=CentOS7X64-RS202
    
    • 1
    • 2
    • 3
    • 4
    • 5
    set vmsour="d:\VirtualMachine.Spaces\CentOS7X64.Core\CentOS7X64.Core.vmx"
    :: vmname注意不能用引号,否则被作为字符串中字符处理
    set vmname=%vmcentos[0]%
    set vmdest="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun.exe -T ws clone %vmsour%  %vmdest%  full -cloneName=%vmname%
    
    set vmname=%vmcentos[1]%
    set vmdest="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun.exe -T ws clone %vmsour%  %vmdest%  full -cloneName=%vmname%
    
    set vmname=%vmcentos[2]%
    set vmdest="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun.exe -T ws clone %vmsour%  %vmdest%  full -cloneName=%vmname%
    
    set vmname=%vmcentos[3]%
    set vmdest="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun.exe -T ws clone %vmsour%  %vmdest%  full -cloneName=%vmname%
    
    set vmname=%vmcentos[4]%
    set vmdest="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun.exe -T ws clone %vmsour%  %vmdest%  full -cloneName=%vmname%
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    快照备份-20220128
    :: vmname注意不能用引号,否则被作为字符串中字符处理
    set vmname=%vmcentos[0]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    set vmshot=20220128
    vmrun.exe -T ws snapshot %vmpath% %vmshot%
    vmrun.exe -T ws listSnapshots %vmpath%
    
    set vmname=%vmcentos[1]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    set vmshot=20220128
    vmrun.exe -T ws snapshot %vmpath% %vmshot%
    vmrun.exe -T ws listSnapshots %vmpath%
    
    set vmname=%vmcentos[2]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    set vmshot=20220128
    vmrun.exe -T ws snapshot %vmpath% %vmshot%
    vmrun.exe -T ws listSnapshots %vmpath%
    
    set vmname=%vmcentos[3]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    set vmshot=20220128
    vmrun.exe -T ws snapshot %vmpath% %vmshot%
    vmrun.exe -T ws listSnapshots %vmpath%
    
    set vmname=%vmcentos[4]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    set vmshot=20220128
    vmrun.exe -T ws snapshot %vmpath% %vmshot%
    vmrun.exe -T ws listSnapshots %vmpath%
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    启动虚拟机
    :: vmname注意不能用引号,否则被作为字符串中字符处理
    set vmname=%vmcentos[0]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws start %vmpath%
    
    set vmname=%vmcentos[1]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws start %vmpath%
    
    set vmname=%vmcentos[2]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws start %vmpath%
    
    set vmname=%vmcentos[3]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws start %vmpath%
    
    set vmname=%vmcentos[4]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws start %vmpath%
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    挂起虚拟机
    :: vmname注意不能用引号,否则被作为字符串中字符处理
    set vmname=%vmcentos[0]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws suspend %vmpath%
    
    set vmname=%vmcentos[1]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws suspend %vmpath%
    
    set vmname=%vmcentos[2]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws suspend %vmpath%
    
    set vmname=%vmcentos[3]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws suspend %vmpath%
    
    set vmname=%vmcentos[4]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws suspend %vmpath%
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    还原虚拟机
    :: vmname注意不能用引号,否则被作为字符串中字符处理
    set vmname=%vmcentos[0]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    set vmshot=20220128
    vmrun.exe -T ws revertToSnapshot %vmpath% %vmshot%
    
    set vmname=%vmcentos[1]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    set vmshot=20220128
    vmrun.exe -T ws revertToSnapshot %vmpath% %vmshot%
    
    set vmname=%vmcentos[2]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    set vmshot=20220128
    vmrun.exe -T ws revertToSnapshot %vmpath% %vmshot%
    
    set vmname=%vmcentos[3]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    set vmshot=20220128
    vmrun.exe -T ws revertToSnapshot %vmpath% %vmshot%
    
    set vmname=%vmcentos[4]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    set vmshot=20220128
    vmrun.exe -T ws revertToSnapshot %vmpath% %vmshot%
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    关闭虚拟机
    :: vmname注意不能用引号,否则被作为字符串中字符处理
    set vmname=%vmcentos[0]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws stop %vmpath%
    
    set vmname=%vmcentos[1]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws stop %vmpath%
    
    set vmname=%vmcentos[2]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws stop %vmpath%
    
    set vmname=%vmcentos[3]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws stop %vmpath%
    
    set vmname=%vmcentos[4]%
    set vmpath="d:\VirtualMachine.Spaces\%vmname%\%vmname%.vmx"
    vmrun -T ws stop %vmpath%
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    配置DR模式:单网段

    实验网络规划
    客户端GWLVSRS
    网段CentOS7X64-Client
    CentOS7X64-GatewayCentOS7X64-LVS101CentOS7X64-RS201
    CentOS7X64-RS202
    133.15.6.0/24
    (NAT模式)
    133.15.6.199
    GW: 133.15.6.200
    133.15.6.200
    199.31.81.0/24
    (仅主机模式)
    199.31.81.200199.31.81.101
    GW: 199.31.81.200
    199.31.81.201
    199.31.81.202
    GW: 199.31.81.200
    lo网卡199.31.81.100199.31.81.100
    199.31.81.100

    注意:一定要关闭防火墙,或是添加相关端口!

    配置hostname及ipaddress
    # 使用模板的ip(133.15.6.81)登录, 随机配置相关信息
    ssh root@133.15.6.81
    
    # CLI-199
    hostnamectl set-hostname cli-199
    cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak
    cat /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i 's/IPADDR=133.15.6.81/IPADDR=133.15.6.199/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i 's/^GATEWAY=133.15.6.1/GATEWAY=133.15.6.200/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    systemctl restart network
    
    
    # GW200
    hostnamectl set-hostname gw-200
    cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak
    cat /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i 's/IPADDR=133.15.6.81/IPADDR=133.15.6.200/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i 's/^GATEWAY=133.15.6.1//g' /etc/sysconfig/network-scripts/ifcfg-ens33
    
    cp /etc/sysconfig/network-scripts/ifcfg-ens33.bak /etc/sysconfig/network-scripts/ifcfg-ens38
    sed -i 's/ens33/ens38/g' /etc/sysconfig/network-scripts/ifcfg-ens38
    sed -i 's/IPADDR=133.15.6.81/IPADDR=199.31.81.200/g' /etc/sysconfig/network-scripts/ifcfg-ens38
    sed -i 's/^GATEWAY=133.15.6.1//g' /etc/sysconfig/network-scripts/ifcfg-ens38
    systemctl restart network
    
    
    # LVS101
    hostnamectl set-hostname LVS-101
    cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak
    cat /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i 's/IPADDR=133.15.6.81/IPADDR=199.31.81.101/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i 's/^GATEWAY=133.15.6.1/GATEWAY=133.15.6.200/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    systemctl restart network
    
    
    # RS201
    hostnamectl set-hostname RS-201
    cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak
    cat /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i 's/IPADDR=133.15.6.81/IPADDR=199.31.81.201/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i 's/^GATEWAY=133.15.6.1/GATEWAY=199.31.81.200/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    systemctl restart network
    
    
    # RS202
    hostnamectl set-hostname RS-202
    cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak
    cat /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i 's/IPADDR=133.15.6.81/IPADDR=199.31.81.202/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i 's/^GATEWAY=133.15.6.1/GATEWAY=199.31.81.200/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    systemctl restart network
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    注意:网络配置好了一定要移除附加的桥接网络,否则网络route出现问题,无法访问

    公共配置
    setenforce 0
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    
    systemctl disable firewalld 
    systemctl stop firewalld
    
    • 1
    • 2
    • 3
    • 4
    • 5
    CentOS7X64-Client配置
    # 确认路由
    route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.0.1     0.0.0.0         UG    101    0        0 ens37
    0.0.0.0         133.15.6.200    0.0.0.0         UG    102    0        0 ens33
    133.15.6.0      0.0.0.0         255.255.255.0   U     102    0        0 ens33
    192.168.0.0     0.0.0.0         255.255.255.0   U     101    0        0 ens37
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    CentOS7X64-Gateway配置

    配置路由

    # 开启net.ipv4.ip_forward
    echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf
    # 加载并生效
    sysctl -p
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注意:不能配置网关为自身,否则转发出现问题

    CentOS7X64-LVS101配置

    配置ipvsadm

    yum install ipvsadm -y
    
    #增加集群
    ipvsadm -A -t 199.31.81.100:80 -s rr
    #增加RS服务器
    ipvsadm -a -t 199.31.81.100:80 -r 199.31.81.201 -g
    ipvsadm -a -t 199.31.81.100:80 -r 199.31.81.202 -g
    ipvsadm -Ln
    
    # 保存规则
    ipvsadm -S -n > /etc/sysconfig/ipvsadm
    sudo systemctl enable ipvsadm
    sudo systemctl start ipvsadm
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    配置lo网卡

    cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo.bak
    echo '' >> /etc/sysconfig/network-scripts/ifcfg-lo
    echo IPADDR1=199.31.81.100 >> /etc/sysconfig/network-scripts/ifcfg-lo
    echo NETMASK1=255.255.255.255 >> /etc/sysconfig/network-scripts/ifcfg-lo
    echo PREFIX1=32 >> /etc/sysconfig/network-scripts/ifcfg-lo
    systemctl restart network
    
    # 临时配置lo网卡为vip 199.31.81.100
    ifconfig lo:0 199.31.81.100/32  # 注意这里掩码只能是32/0, 否则转发有问题
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    CentOS7X64-RS201配置

    配置httpd服务

    yum install -y httpd
    echo 199.31.81.201 RS201 > /var/www/html/index.html
    systemctl start httpd
    systemctl enable httpd
    curl 199.31.81.201
    
    # 移除附加外网适配器,ens33切换到仅主机模式
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    配置lo网卡

    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    
    cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo.bak
    echo '' >> /etc/sysconfig/network-scripts/ifcfg-lo
    echo IPADDR1=199.31.81.100 >> /etc/sysconfig/network-scripts/ifcfg-lo
    echo NETMASK1=255.255.255.255 >> /etc/sysconfig/network-scripts/ifcfg-lo
    echo PREFIX1=32 >> /etc/sysconfig/network-scripts/ifcfg-lo
    systemctl restart network
    
    # 临时配置lo网卡为vip 199.31.81.100
    ifconfig lo:0 199.31.81.100/32  # 注意这里掩码只能是32/0, 否则转发有问题
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    CentOS7X64-RS202配置

    配置httpd服务

    yum install -y httpd
    echo 199.31.81.202 RS202 > /var/www/html/index.html
    systemctl start httpd
    systemctl enable httpd
    curl 199.31.81.202
    
    # 移除附加外网适配器,ens33切换到仅主机模式
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    配置lo网卡

    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    
    cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo.bak
    echo '' >> /etc/sysconfig/network-scripts/ifcfg-lo
    echo IPADDR1=199.31.81.100 >> /etc/sysconfig/network-scripts/ifcfg-lo
    echo NETMASK1=255.255.255.255 >> /etc/sysconfig/network-scripts/ifcfg-lo
    echo PREFIX1=32 >> /etc/sysconfig/network-scripts/ifcfg-lo
    systemctl restart network
    
    # 临时配置lo网卡为vip 199.31.81.100
    ifconfig lo:0 199.31.81.100/32  # 注意这里掩码只能是32/0, 否则转发有问题
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    在客户端上测试

    在这里插入图片描述

    常用操作命令

    # 查看规则
    cat /proc/net/ip_vs
    # 查看连接
    cat /proc/net/ip_vs_conn
    # 查看当前规则
    ipvsadm-save -n 或 sudo ipvsadm -S -n
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    # 保存规则
    sudo ipvsadm -S -n > ipvsadm & sudo mv ipvsadm /etc/sysconfig/
    # 清理规则
    sudo ipvsadm -C
    # 重载规则
    sudo ipvsadm -R < /etc/sysconfig/ipvsadm
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    # 查询ipvsadm相关信息
    rpm -ql ipvsadm
    
    • 1
    • 2

    在这里插入图片描述

    # 配置开机启动, 会清除/etc/sysconfig/ipvsadm 的内容,测试不通过
    sudo systemctl enable ipvsadm
    sudo systemctl start ipvsadm
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    【计算机毕业设计】基于SpringBoot+Vue的流浪猫狗救助救援网站的设计与实现
    C语言经典习题(异或思想)
    瑞利-贝纳尔对流(Rayleigh–Bénard convection)
    Linux系统管理:虚拟机Kylin OS安装
    Python第五章 函数
    IO和进程day06(线程续、同步线程互斥)
    无源蜂鸣器驱动实验
    学习记忆——图像篇——图像记忆
    ubuntu上通过修改grub启动参数,将串口重定向到sol
    Python如何判断当前时间是否为夏令时?
  • 原文地址:https://blog.csdn.net/yidichaxiang/article/details/126845630