二、架构01-LVS的DR模式用法

LVS实现DR(nginx)

1、架构图


2、RS配置httpd、php示例

[root@node02 ~]# yum install -y httpd php php-mysql

[root@node02 ~]# vim /var/www/html/phpinfo.php

RS1

        phpinfo();

?>

[root@node02 ~]# vim /var/www/html/test1.html

RS1,192.168.32.128

[root@node02 ~]# vim setparam.sh

#!/bin/bash

case $1 in

start)

        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

        ifconfig $iface $vip netmask broadcast $vip up

        route add -host $vip dev $iface

        ;;

stop)

        ifconfig $iface down

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

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

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

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

;;

*)

        echo "Usage: $(basename $0) start|stop"

        exit 1

        ;;

esac

[root@node02 ~]#

[root@node02 ~]#

[root@node02 ~]# chmod +x setparam.sh

[root@node02 ~]# ls

anaconda-ks.cfg  setparam.sh

[root@node02 ~]# bash -n setparam.sh

[root@node02 ~]# bash -x setparam.sh start

+ case $1 in

+ echo 1

+ echo 1

+ echo 2

+ echo 2

[root@node02 ~]# ifconfig lo:0 192.168.32.188 netmask 255.255.255.255 broadcast  192.168.32.188 up

[root@node02 ~]# route add -host 192.168.32.188 dev lo:0

[root@node02 ~]# ss -tnl

State      Recv-Q Send-Q                                                                        Local Address:Port

LISTEN     0      128                                                                                       *:111

LISTEN     0      128                                                                                       *:22

LISTEN     0      100                                                                               127.0.0.1:25

LISTEN     0      128                                                                                      :::111

LISTEN     0      128                                                                                      :::80

LISTEN     0      128                                                                                      :::22

LISTEN     0      100                                                                                     ::1:25

node03一样配置

改进RS脚本

[root@node02 ~]# more setparam.sh

#!/bin/bash

#

vip='192.168.32.188'

mask='255.255.255.255'

iface='lo:0'

case $1 in

start)

        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

        ifconfig $iface $vip netmask broadcast $vip up

        route add -host $vip dev $iface

        ;;

stop)

        ifconfig $iface down

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

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

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

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

;;

*)

        echo "Usage: $(basename $0) start|stop"

        exit 1

        ;;

esac

3、VS配置

[root@node01 ~]# ifconfig ens33:0 192.168.32.188 netmask 255.255.255.255 broadcast 192.168.32.188 up

[root@node01 ~]# yum install -y ipvsadm

[root@node01 ~]# ipvsadm -A -t 192.168.32.188:80 -s rr

[root@node01 ~]# 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.32.188:80 rr

[root@node01 ~]# ipvsadm -a -t 192.168.32.188:80 -r 192.168.32.128 -g

[root@node01 ~]# ipvsadm -a -t 192.168.32.188:80 -r 192.168.32.131 -g

[root@node01 ~]# 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.32.188:80 rr

  -> 192.168.32.128:80            Route   1      0          0

  -> 192.168.32.131:80            Route   1      0          0

                VS的配置脚本:

                #!/bin/bash

                #

                vip='192.168.32.188'

                iface='ens33:0'

                mask='255.255.255.255'

                port='80'

                rs1='192.168.32.128'

                rs2='192.168.32.131'

                scheduler='wrr'

                type='-g'

                case $1 in

                start)

                    ifconfig $iface $vip netmask $mask broadcast $vip up

                    iptables -F


                    ipvsadm -A -t ${vip}:${port} -s $scheduler

                    ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1

                    ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1

                    ;;

                stop)

                    ipvsadm -C

                    ifconfig $iface down

                    ;;

                *)

                    echo "Usage $(basename $0) start|stop"

                    exit 1

                    ;;

                esac                

4、验证

[40796.DESKTOP-Q4O3FV5] ➤ for i in {1..20}; do curl http://192.168.32.188/test1.html;done

RS2,192.168.32.131

RS1,192.168.32.128

RS2,192.168.32.131

RS1,192.168.32.128

RS2,192.168.32.131

RS1,192.168.32.128

RS2,192.168.32.131

RS1,192.168.32.128

RS2,192.168.32.131

RS1,192.168.32.128

RS2,192.168.32.131

RS1,192.168.32.128

RS2,192.168.32.131

RS1,192.168.32.128

RS2,192.168.32.131

RS1,192.168.32.128

RS2,192.168.32.131

RS1,192.168.32.128

RS2,192.168.32.131

RS1,192.168.32.128




你可能感兴趣的:(二、架构01-LVS的DR模式用法)