lzqabc

导航

负载均衡

  1. 三台虚拟机

  2. 分别取名dir rs1 rs2

  3. 给dir1和dir2分别加入网关=dir的IP

    vi /etc/sysconfig/network-scripts/ifcfg-ens33
    GETWAY=192.168.200.147
    ##也可以在虚拟机设置那边改
     
  4. 清空iptables的规则

    iptables -F; iptables -t nat -F; service iptables save       
     
  5. 安装yum源

    rm -f /etc/yum.repos.d/*
    cat /etc/yum.repos.d/local.repo
    [gpmall]
    name=gpmall
    baseurl=file:///opt/gpmall-repo
    gpgcheck=0
    enabled=1
    yum clean all
    yum repolist
     
  6. 安装iptables-service

    yum -y install iptables-services
     
  7. 清空规则

    iptables -F; iptables -t nat -F; service iptables save
     
  8. 关闭防火墙

    systemctl stop firewalld
    systemctl disable firewalld
    setenforce 0
    getenforce 0
     

Ⅰ:安装LVS

  1. dir安装ipvsadm

    yum install -y ipvsadm
     
  2. dir编写脚本

    vim /usr/local/sbin/lvs_nat.sh
    #! /bin/bash
    # director 服务器上开启路由转发功能
    echo 1 > /proc/sys/net/ipv4/ip_forward
    # 关闭icmp的重定向
    echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
    echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
    # 注意区分网卡名字
    echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
    echo 0 > /proc/sys/net/ipv4/conf/ens34/send_redirects
    # director 设置nat防火墙
    iptables -t nat -F
    iptables -t nat -X
    iptables -t nat -A POSTROUTING -s 192.168.200.0/24  -j MASQUERADE
    # director设置ipvsadm
    IPVSADM='/usr/sbin/ipvsadm'
    $IPVSADM -C
    $IPVSADM -A -t 192.168.147.144:80 -s wlc 
    ###-p 300###
    $IPVSADM -a -t 192.168.147.144:80 -r 192.168.200.131:80 -m -w 1 #rs1
    $IPVSADM -a -t 192.168.147.144:80 -r 192.168.200.132:80 -m -w 1 #rs2
    #-t 后面为主机模式IP地址
    
    bash/usr/local/sbin/lvs_nat.sh  #启动脚本
    killall nginx
     
  3. rs1+2 安装niginx,配置网页数据

    yum -y install nginx
    echo "rs1" > /usr/share/nginx/html/index.html   
    echo "rs2" > /usr/share/nginx/html/index.html  
     
  4. 测试nginx

    curl 192.168.200.150
    rs2
    curl 192.168.200.148
    rs1
     curl 192.168.183.129
    rs2
    curl 192.168.183.129
    rs1
     curl 192.168.183.129
    rs2
    curl 192.168.183.129
    rs1
     
    1. 关闭前面网关

    2. 编写dir脚本

      vim /usr/local/sbin/lvs_rs.sh 
      #! /bin/bash
      echo 1 > /proc/sys/net/ipv4/ip_forward
      ipv=/usr/sbin/ipvsadm
      vip=192.168.200.110 虚拟
      rs1=192.168.200.148
      rs2=192.168.200.150
      #注意这里的网卡名字
      ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
      route add -host $vip dev ens33:2
      $ipv -C
      $ipv -A -t $vip:80 -s wrr
      $ipv -a -t $vip:80 -r $rs1:80 -g -w 1
      $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
       
    3. 编写rs1+2脚本

      vim /usr/local/sbin/lvs_rs.sh 
      #/bin/bash
      vip=192.168.200.110  #虚拟-
      #把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
      ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
      route add -host $vip lo:0
      #以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
      #参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
      echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
      echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
      echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
      echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       
    4. 3节点运行脚本并测试

      bash /usr/local/sbin/lvs_dr.sh 

posted on 2022-09-21 15:34  李子权  阅读(16)  评论(0编辑  收藏  举报