• Linux下部署LVS(DR)+keepalived+Nginx负载均衡


    架构部署 

    LVS/keepalived(master):192.168.21.3 
    LVS/keepalived(Slave):192.168.21.6 
    Nginx1:192.168.21.4 
    Nginx2:192.168.21.5 
    VIP:192.168.21.10
    1、安装ipvsadm、keepalived(Master/Slave) 
    yum -y install keepalived ipvsadm 
    2、修改keepalived.conf文件 
    LVS_master
    cd /etc/keepalived 
    vi /deepalived 
     

    ! Configuration File for keepalived

    global_defs {

       router_id master_201

    }

    vrrp_instance VI_1 {

        state MASTER

        interface eth0

        virtual_router_id 100

        priority 151

        advert_int 1

        authentication {

            auth_type PASS

            auth_pass 123456

        }

        virtual_ipaddress {

            192.168.21.10

        }

    }

    virtual_server 192.168.21.10 80 {

        delay_loop 6

        lb_algo wrr

        lb_kind DR

    #    persistence_timeout 50

        protocol TCP

        real_server 192.168.21.4 80 {

            weight 1

            TCP_CHECK {

                connect_timeout 3

                nb_get_retry 3

                delay_before_retry 3

                connect_port 80

            }

        }

        real_server 192.168.21.5 80 {

            weight 1

            TCP_CHECK {

                connect_timeout 3

                nb_get_retry 3

                delay_before_retry 3

                connect_port 80

            }

        }

    }

    LVS_Slave

    ! Configuration File for keepalived

    global_defs {

       router_id slave_211

    }

    vrrp_instance VI_1 {

        state MASTER

        interface eth0

        virtual_router_id 100

        priority 150

        advert_int 1

        authentication {

            auth_type PASS

            auth_pass 123456

        }

        virtual_ipaddress {

            192.168.21.10

        }

    }

    virtual_server 192.168.21.10 80 {

        delay_loop 6

        lb_algo wrr

        lb_kind DR

    #    persistence_timeout 50

        protocol TCP

        real_server 192.168.21.4 80 {

            weight 1

            TCP_CHECK {

                connect_timeout 3

                nb_get_retry 3

                delay_before_retry 3

                connect_port 80

            }

        }

        real_server 192.168.21.5 80 {

            weight 1

            TCP_CHECK {

                connect_timeout 3

                nb_get_retry 3

                delay_before_retry 3

                connect_port 80

            }

        }

    }

     
    3、Nginx端配置 
    写一下lvs脚本 

    #!/bin/bash

    #

    # Script to start LVS DR real server.

    # description: LVS DR real server

    #

    .  /etc/rc.d/init.d/functions

    VIP=192.168.21.10   #这里根据需要改成自己的VIP地址

    host=`/bin/hostname`

    case "$1" in

    start)

           # Start LVS-DR real server on this machine.

            /sbin/ifconfig lo down

            /sbin/ifconfig lo up

            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

            /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up

            /sbin/route add -host $VIP dev lo:0

    ;;

    stop)

            # Stop LVS-DR real server loopback device(s).

            /sbin/ifconfig lo:0 down

            echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore

            echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce

            echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore

            echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce

    ;;

    status)

            # Status of LVS-DR real server.

            islothere=`/sbin/ifconfig lo:0 | grep $VIP`

            isrothere=`netstat -rn | grep "lo:0" | grep $VIP`

            if [ ! "$islothere" -o ! "isrothere" ];then

                # Either the route or the lo:0 device

                # not found.

                echo "LVS-DR real server Stopped."

            else

                echo "LVS-DR real server Running."

            fi

    ;;

    *)

                # Invalid entry.

                echo "$0: Usage: $0 {start|status|stop}”

                exit 1

    ;;

    esac

    4、测试

    Master

    [root@text1 keepalived]# ipvsadm -ln

    IP Virtual Server version 1.2.1 (size=4096)

    Prot LocalAddress:Port Scheduler Flags

      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

    TCP  192.168.21.10:80 wrr

      -> 192.168.21.4:80              Route   1      0          0         

      -> 192.168.21.5:80              Route   1      0          0

    Slave

    [root@omserver keepalived]# ipvsadm -ln

    IP Virtual Server version 1.2.1 (size=4096)

    Prot LocalAddress:Port Scheduler Flags

      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

    TCP  192.168.21.10:80 wrr

      -> 192.168.21.4:80              Route   1      0          0         

      -> 192.168.21.5:80              Route   1      0          0  

  • 相关阅读:
    复习一allure
    本地push 到git hub 二
    项目过程中,测试总感觉自己的时间不够怎么办?
    记一次接口并发,酿成20万损失的惨案
    记录一次管理员组织应用的运行
    记录一次jmeter脚本开发缺少utf-8惹的祸
    记录一次pycharm中,引入其他类可用,下面总是有波浪线,而且Ctrl+b 无法查看类函数的源码
    Fiddler如何过滤无用的链接
    【bat】将当前文件夹及其子文件夹下的所有文件移动到新文件夹中
    python 安装python-docx模块(如何本地安装)
  • 原文地址:https://www.cnblogs.com/TaleG/p/5352315.html
Copyright © 2020-2023  润新知