lvs+keepalived实现高可用负载均衡集群

一、LVS+Keepalived介绍

    1. LVS

        LVSLinux Virtual Server的简写,意即Linux虚拟服务器是一个虚拟的服务器集群系统本项目在19985月由章文嵩博士成立是中国国内最早出现的自由软件项目之一目前有三种IP负载均衡技术VS/NATVS/TUNVS/DR);八种调度算法rr,wrr,lc,wlc,lblc,lblcr,dh,sh

    2. Keepalived

        Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。

二、网站负载均衡拓朴图

lvs+keepalived实现高可用负载均衡集群_第1张图片

+---------------------------------VIP(192.168.1.222)---------------------------------+

|                                                                    |                                                                 |

|                                                                    |                                                                 |

server(MASTER)<--------------------keepalived------------------->server(BACKUP)

192.168.1.202                                                                                              192.168.1.204

            

IP规划:

Hostname IP
VIP 192.168.1.222
MASTER 192.168.1.202
BACKUP 192.168.1.204
REALSERVER1 192.168.1.203
REALSERVER2 192.168.1.201

三、部署步骤

    安装LVS和Keepalived软件包

    1. 下载相关软件包

        mkdir /usr/local/src/lvs

        cd /usr/local/src/lvs

        wget http://down1.chinaunix.net/distfiles/ipvsadm-1.24.tar.gz

        wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz

    2. 安装LVS和Keepalived     

        lsmod |grep ip_vs                                        #显示已载入系统的ip_vs模块

        uname -r                                                     #显示操作系统的发行版号

        2.6.32-279.el6.x86_64                                  #操作系统的发行版号

        yum install kernel-devel                              #安装与系统内核配套的开发包

        yum -y install popt*

        yum -y install libnl*

        yum -y install openssl*

        ln -s /usr/src/kernels/2.6.32-279.9.1.el6.x86_64/ /usr/src/linux         #建立软连接

        

        tar zxvf ipvsadm-1.24.tar.gz

        cd ipvsadm-1.24

        make && make install

        find / -name ipvsadm  # 查看ipvsadm的位置

        

        tar zxvf keepalived-1.1.15.tar.gz

        cd keepalived-1.1.15

        ./configure  && make && make install

        find / -name keepalived  # 查看keepalived位置                

        

        cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

        cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

        mkdir /etc/keepalived

        cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

        cp /usr/local/sbin/keepalived /usr/sbin/

        service keepalived start|stop     #做成系统启动服务方便管理.

    3. 配置realserver脚本

        在192.168.203(realserver)上配置,脚本我保存在了/opt下

        #!/bin/bash

        #description Config LVS to realserver lo and apply noarp

        #Written by ce

        

        VIP=192.168.1.222

        . /etc/init.d/functions

        

        case "$1" in

                start)

                       ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP

                       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

                       sysctl -p >/dev/null 2>&1

                       echo "RealServer Start OK"

        

                       ;;

                stop)

                       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

                       echo "RealServer Stoped"

                       ;;

                *)

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

                       exit 1

                esac

        

                exit 0

        chmod a+x /opt/realserver.sh                                    #赋予可执行权限

        /opt/realserver.sh start                                               #启动

        之后,通过rsync远程备份到192.168.1.201(另一台realserver上)

        rsync -av /opt/realserver.sh [email protected]:/root/

        然后,在192.168.1.201上启动realserver脚本。

    4. 利用keepalived实现负载均衡和高可用性

        1. 配置在主负载均衡服务器(192.168.1.202)上配置keepalived.conf

            vi /etc/keepalived/keepalived.conf

            ! Configuration File for keepalived

            global_defs {

               router_id LVS_DEVEL

            }

            vrrp_instance VI_1 {

                state MASTER

                interface eth0

                virtual_router_id 51

                priority 100

                advert_int 1

                authentication {

                    auth_type PASS

                    auth_pass 1111

                }

                virtual_ipaddress {

                    192.168.1.222

                }

            }

            virtual_server 192.168.1.222 80 {

                delay_loop 6

                lb_algo wrr

                lb_kind DR

                persistence_timeout 60

                protocol TCP

                real_server 192.168.1.203 80 {

                    weight 3

                    TCP_CHECK {

                        connect_timeout 10

                        nb_get_retry 3

                        delay_before_retry 3

                        connect_port 80

                    }

                }

                real_server 192.168.1.201 80 {

                    weight 3

                    TCP_CHECK {

                        connect_timeout 10

                        nb_get_retry 3

                        delay_before_retry 3

                        connect_port 80

                    }

                 }

            }

            

    2. 配置在备份负载均衡服务器(192.168.1.204)上配置keepalived.conf

            vi /etc/keepalived/keepalived.conf

            ! Configuration File for keepalived

            global_defs {

               router_id LVS_DEVEL

            }

            vrrp_instance VI_1 {

                state BACKUP

                interface eth0

                virtual_router_id 51

                priority 99

                advert_int 1

                authentication {

                    auth_type PASS

                    auth_pass 1111

                }

                virtual_ipaddress {

                    192.168.1.222

                }

            }

            virtual_server 192.168.1.222 80 {

                delay_loop 6

                lb_algo wrr

                lb_kind DR

                persistence_timeout 60

                protocol TCP

                real_server 192.168.1.203 80 {

                    weight 3

                    TCP_CHECK {

                        connect_timeout 10

                        nb_get_retry 3

                        delay_before_retry 3

                        connect_port 80

                    }

                }

                real_server 192.168.1.201 80 {

                    weight 3

                    TCP_CHECK {

                        connect_timeout 10

                        nb_get_retry 3

                        delay_before_retry 3

                        connect_port 80

                    }

                 }

            }

    3. 启动keepalived

        /etc/init.d/keepalived start(或者service keepalived start)

        启动keepalived 服务keepalived就能利用keepalived.conf 配置文件实现负载均衡和高可用.  整个LVS负均衡HA方案keepalived.conf一个文件即可搞定

    4. 查看lvs服务是否正常

        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.1.200:80 wrr persistent 60

           -> 192.168.1.203:80                        Route        3            0
           -> 192.168.1.201:80                        Route        3            0

        tail –f /var/log/message  监听日志,查看状态




你可能感兴趣的:(lvs+keepalived实现高可用负载均衡集群)