• ~LVS的nat模式+ipvsadm命令的使用+lvs的dr模式实现+lvs火墙标记


    lvs简介

    LVS:Linux Virtual Server,负载调度器,内核集成,章文嵩,阿里的四层SLB(Server LoadBalance)是基 于LVS+keepalived实现

    lvs集群的类型

    lvs-nat: 修改请求报文的目标IP,多目标IP的DNAT

    lvs-dr: 操纵封装新的MAC地址 lvs-tun: 在原请求IP报文之外新加一个IP首部

    lvs-fullnat: 修改请求报文的源和目标IP

    一、lvs的nat模式

    lvs是nat模式

    1. [root@lvs ~]# cat /etc/NetworkManager/system-connections/eth1.connection
    2. [connection]
    3. id=eth1
    4. type=ethernet
    5. interface-name=eth1
    6. [ipv4]
    7. address1=192.168.0.100/24
    8. method=manual

     克隆两个名为webserver1和webserver2,且为仅主机模式

    webserver1

    1. [root@webserver1 ~]# cat /etc/NetworkManager/system-connections/eth0.connection
    2. [connection]
    3. id=eth0
    4. type=ethernet
    5. interface-name=eth0
    6. [ipv4]
    7. address1=192.168.0.10/24,192.168.0.100
    8. method=manual

    webserver2

    1. [root@webserver2 ~]# cat /etc/NetworkManager/system-connections/eth0.connection
    2. [connection]
    3. id=eth0
    4. type=ethernet
    5. interface-name=eth0
    6. [ipv4]
    7. address1=192.168.0.20/24,192.168.0.100
    8. method=manual

    网关设不设不影响实验 

    搭建nat模式

    别忘了下载web服务

    webserver里配置

    1. echo webserver1-192.168.0.10 > /var/www/html/index.html
    2. echo webserver1-192.168.0.20 > /var/www/html/index.html

    再下载ipvsadm

    ipvsadm命令 核心功能: 集群服务管理:增、删、改 集群服务的RS管理:增、删、改 查看

    接着配置规则

    1. ipvsadm -A -t 172.25.254.100:80 -s rr
    2. ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.10 -m
    3. ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.20 -m
    4. ipvsadm -Ln
    5. watch -n 1 ipvsadm -Ln --rate

    测试结果

    二、 ipvsadm命令的使用

    命令参数

    1. 管理集群服务
    2. ipvsadm -A|E -t(tcp)|u(udp)|f(防护墙标签) \
    3. service-address(集群地址) \
    4. [-s scheduler(调度算法)] \
    5. [-p [timeout]] \
    6. [-M netmask] \
    7. [--pepersistence_engine] \
    8. [-b sched-flags]
    9. ipvsadm -D -t|u|f service-address 删除
    10. ipvsadm –C 清空
    11. ipvsadm –R 重载
    12. ipvsadm -S [-n] 保存
    13. 管理集群中的real server
    14. ipvsadm -a|e -t|u|f service-address -r server-address [-g | -i| -m](工作模式) [-w
    15. weight](权重)
    16. ipvsadm -d -t|u|f service-address -r server-address 删除RS
    17. ipvsadm -L|l [options] 查看rs
    18. ipvsadm -Z [-t|u|f service-address] 清楚计数器

    三、lvs的dr模式实现

    DR:Direct Routing,直接路由,LVS默认模式,应用最广泛,通过为请求报文重新封装一个MAC首部进行 转发,源MAC是DIP所在的接口的MAC,目标MAC是某挑选出的RS的RIP所在接口的MAC地址;源 IP/PORT,以及目标IP/PORT均保持不变 

    lvs主机

    1. [root@lvs ~]# cat /etc/NetworkManager/system-connections/eth0.connection
    2. [connection]
    3. id=eth0
    4. type=ethernet
    5. interface-name=eth0
    6. [ipv4]
    7. address1=192.168.0.50/24,192.168.0.100
    8. method=manual

    webserver1 

    1. [root@server1 ~]# cat /etc/NetworkManager/system-connections/eth0.connection
    2. [connection]
    3. id=eth0
    4. type=ethernet
    5. interface-name=eth0
    6. [ipv4]
    7. address1=192.168.0.10/24,192.168.0.100
    8. method=manual

    webserver2

    1. [root@server2 ~]# cat /etc/NetworkManager/system-connections/eth0.connection
    2. [connection]
    3. id=eth0
    4. type=ethernet
    5. interface-name=eth0
    6. [ipv4]
    7. address1=192.168.0.20/24,192.168.0.100
    8. method=manual

     路由器主机(router

    1. [root@router ~]# cat /etc/NetworkManager/system-connections/eth0.connection
    2. [connection]
    3. id=eth0
    4. type=ethernet
    5. interface-name=eth0
    6. [ipv4]
    7. address1=172.25.254.100/24,172.25.254.2
    8. dns=114.114.114.114;
    9. method=manual
    1. [root@router ~]# cat /etc/NetworkManager/system-connections/eth1.connection
    2. [connection]
    3. id=eth1
    4. type=ethernet
    5. interface-name=eth1
    6. [ipv4]
    7. address1=192.168.0.100/24
    8. method=manual

     客户端nat模式(client)  

    1. [root@client ~]# cat /etc/NetworkManager/system-connections/eth0.connection
    2. [connection]
    3. id=eth0
    4. type=ethernet
    5. interface-name=eth0
    6. [ipv4]
    7. address1=172.25.254.200/24,172.25.254.100
    8. method=manual

    之后打开内核路由功能

    rs主机中vip不对外响应

    在lvs中配置策略

    1. echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    2. echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    3. echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    4. echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

    lvs设定:

    1. ipvsadm -A -t 192.168.0.200:80 -s wrr
    2. ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.10:80 -g -w 1
    3. ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.20:80 -g -w 2

     别忘了启用IP转发

    1. vim /etc/sysctl.conf
    2. net.ipv4.ip_forward = 1
    3. ipvsadm -p

     在lvs主机和rs主机添加vip

    测试效果

    1. [root@client ~]# for N in {1..10};
    2. do
    3. curl 192.168.0.200;
    4. done
    5. RS2 server - 192.168.0.20
    6. RS1 server - 192.168.0.10
    7. RS2 server - 192.168.0.20
    8. RS2 server - 192.168.0.20
    9. RS1 server - 192.168.0.10
    10. RS2 server - 192.168.0.20
    11. RS2 server - 192.168.0.20
    12. RS1 server - 192.168.0.10
    13. RS2 server - 192.168.0.20
    14. RS2 server - 192.168.0.20

    四、lvs火墙标记

    1. ]# yum install mod_ssl -y
    2. ]# systemctl restart httpd
    1. [root@lvs ~]# ipvsadm -A -t 192.168.0.100:80 -s rr
    2. [root@lvs ~]# ipvsadm -A -t 192.168.0.100:443 -s rr
    3. [root@lvs ~]# ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.101:80 -g
    4. [root@lvs ~]# ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.102:80 -g
    5. [root@lvs ~]# ipvsadm -a -t 192.168.0.100:443 -r 192.168.0.102:80 -g
    6. [root@lvs ~]# ipvsadm -a -t 192.168.0.100:443 -r 192.168.0.101:80 -g

    测试

    1. [root@node10 ~]# curl http://192.168.0.100;curl -k https://192.168.0.100
    2. RS1 server - 192.168.0.101
    3. RS1 server - 192.168.0.101

     

  • 相关阅读:
    量化投资00
    A. The Party and Sweets
    计算机网络常识通览
    【Proteus仿真】【51单片机】公交车报站系统
    spark shuffle写操作——UnsafeShuffleWriter
    3D可视化智慧园区建模监控系统,数字化解决方案
    Sentinel 源码学习
    竞赛选题 基于机器视觉的火车票识别系统
    ipv6一致性-NDP测试
    面试题c/c++ --STL 算法与数据结构
  • 原文地址:https://blog.csdn.net/2201_75759289/article/details/141095504