zoukankan      html  css  js  c++  java
  • LVS+keepalived+nginx+tomcat部署实现

    拓扑如下所示

    wKioL1QOmLrDoQ2uAAHpCQhZ_Bk403.jpg

    # 节点分布情况

    LVS-dr-master

        eth0: 192.168.146.141

      

    LVS-dr-slave

        eth0: 192.168.146.142

     

     

    nginx1:

        eth0: 192.168.146.139

    nginx2:

        eth0: 192.168.146.140

    tomcat1:

        eth0: 192.168.146.138 启用了4个tomcat

     

    VIP: 192.168.146.200

     

    # 具体配置

    ### lvs master

    ### 1、安装ipvsadm、keepalived

    1
    yum install -y keepalived ipvsadm

     

    ### 2、使用keepalived来管理lvs

    脚本:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    global_defs
    {
    router_id master_210
    }
      
    vrrp_instance aiyou {
    state MASTER
    interface eth0
    virtual_router_id 100    #这个数值 master和slave必须统一
    priority 151     #这个数值决定哪台服务器是master 
    advert_int 1
    authentication {
            auth_type PASS
            auth_pass 123456
            }
    virtual_ipaddress {
            192.168.146.200
            }
    }
    virtual_server 192.168.146.200 80 {
                delay_loop 6
                lb_algo wrr
                lb_kind DR
    #            persistence_timeout 50
                protocol TCP
    real_server 192.168.146.139 80 {
                    weight 1
                    TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                    }
                }
    real_server 192.168.146.140 80 {
                    weight 1
                    TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                    }
                }
    }

    ### lvs slave

    ### 1、安装ipvsadm、keepalived

    1
    yum install -y keepalived ipvsadm

     

    ### 2、使用keepalived来管理lvs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    global_defs
    {
    router_id slave_211
    }
      
    vrrp_instance aiyou {
    state MASTER
    interface eth0
    virtual_router_id 100    #这个数值 master和slave必须统一
    priority 150     #这个数值决定哪台服务器是master 这里我们比master数值低,所以角色是backup,
    advert_int 1
    authentication {
            auth_type PASS
            auth_pass 123456
            }
    virtual_ipaddress {
            192.168.146.200
            }
    }
    virtual_server 192.168.146.200 80 {
                delay_loop 6
                lb_algo wrr
                lb_kind DR
    #            persistence_timeout 50
                protocol TCP
    real_server 192.168.146.139 80 {
                    weight 1
                    TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                    }
                }
    real_server 192.168.146.140 80 {
                    weight 1
                    TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                    }
                }
    }

    ### nginx1

    ### nginx上要跑个脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    #!/bin/bash  
    #   
    # Script to start LVS DR real server.   
    # description: LVS DR real server   
    #   
    .  /etc/rc.d/init.d/functions
    VIP=192.168.146.200   #这里根据需要改成自己的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

    ### nginx2

    ### nginx上要跑个脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    #!/bin/bash  
    #   
    # Script to start LVS DR real server.   
    # description: LVS DR real server   
    #   
    .  /etc/rc.d/init.d/functions
    VIP=192.168.146.200   #这里根据需要改成自己的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

    ### 后端tomcat安装省略

    采用一机多实例模式(非多虚拟主机)

    ### 测试

    ### master上验证

    1
    2
    3
    4
    5
    6
    7
    8
    [root@lvs1 ~]# 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.146.200:80 wrr
      -> 192.168.146.139:80           Route   1      0          0         
      -> 192.168.146.140:80           Route   1      0          0         
    [root@lvs1 ~]#

    ### slave上验证

    1
    2
    3
    4
    5
    6
    7
    8
    [root@lvs2 ~]# 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.146.200:80 wrr
      -> 192.168.146.139:80           Route   1      0          0         
      -> 192.168.146.140:80           Route   1      0          0         
    [root@lvs2 ~]#

    参考文章:

    http://www.5ilinux.com/2014/05/lvs_keepalived_nginx_tomcat.html

    http://lovelace.blog.51cto.com/1028430/1550188

  • 相关阅读:
    教了公司新来的小姐姐这七种排序算法以及实现,她一脸崇拜的看着我
    历时两年零三个月,从刚毕业的外包到现在的阿里P7offer,我只做了这几件事
    深度分享:面试阿里,字节跳动,美团90%会被问到的HashMap知识
    备份一下我的.bash_aliases文件
    Error(10028):Can't resolve multiple constant drivers for net “ ” at **.v
    Error (10170): Verilog HDL syntax error at passwd.v(21) near text "if"; expecting an identifier ("if" is a reserved keyword ), or "endmodule", or a parallel statement
    正在从 Windows 应用商店下载... 无法从 Windows 应用商店下载。请检查网络连接。
    完全卸载win10上的Ubuntu子系统
    >>> print "hello" SyntaxError: Missing parentheses in call to 'print'
    深入Linux内核架构
  • 原文地址:https://www.cnblogs.com/crazylqy/p/7742045.html
Copyright © 2011-2022 走看看