搭建DR模式负载均衡

一台director
两台realserver
三台虚拟机都要设置成桥接模式

设置director

1.下载ipvsadm

                yum install -y ipvsadm

    2.编辑脚本

                vim dr.sh

                编辑内容:

                #! /bin/bash
                echo 1 > /proc/sys/net/ipv4/ip_forward
                ipv=/sbin/ipvsadm
                vip=192.168.199.177
                rs1=192.168.199.162
                rs2=192.168.199.165
                ifconfig ens33:0 down
                ifconfig ens33:0 $vip broadcast $vip netmask 255.255.255.255 up
                route add -host $vip dev ens33:0
                $ipv -C
                $ipv -A -t $vip:80 -s wrr
                $ipv -a -t $vip:80 -r $rs1:80 -g -w 3
                $ipv -a -t $vip:80 -r $rs2:80 -g -w 1

    3.运行脚本

                bash dr.sh

设置realserver

1.安装nginx
    (yum安装或者编译安装)
      以下是编译安装的方法:
                (1).wget http://nginx.org/download/nginx-1.16.0.tar.gz
                (2).tar -zxvf nginx-1.16.0.tar.gz
                (3).mv nginx-1.16.0 /usr/local/src
                (4).yum install openssl openssl-devel zlib zlib-devel pcre pcre-devel
                (5).yum install gcc
                (6).yum install gcc-c++
                (7).cd /usr/local/src/nginx-1.16.0
                (8).     ./configure
                (9).make
                (10).make install
                (11).cd /usr/local/nginx/sbin
                (12).   ./nginx
                (13)../nginx -t (检测)
    2.编辑脚本

                vim dr.sh

                编辑内容:

                #! /bin/bash
                vip=192.168.199.177
                ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
                route add -host $vip lo:0
                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

    3.运行脚本

                bash dr.sh

    4.在页面中输入内容(为了访问网页的时候,方便观察两台realserver交替出现的效果)

            cd /usr/local/nginx/html

            echo 199.162 > index.html

    5.重读nginx

                cd /usr/local/nginx/sbin

                ./nginx -s reload

访问VIP!