基于VMware的虚拟Linux集群搭建-lvs+keepalived

时间:2021-12-14 22:44:22

基于VMware的虚拟Linux集群搭建-lvs+keepalived

本文通过keepalived实现lvsserver的的双机热备和真实server之间的负载均衡。这方面的blog挺多,可是每一个人搭建集群的环境不同,遇到的问题也不同。本文简述配置的一些过程以及配置过程中遇到问题时的解决方式。

准本工作

1. 创建Linux虚拟机
本文是基于Vmare虚拟环境搭建集群的。所以首先在Vmare中创建一些虚拟机(安装一个Linux系统。其它的复制第一次安装的就可以)。本文创建4台虚拟机。网卡模式设置成桥接方式。这样能够保证虚拟机中的网络和主机IP在同一个网段。这种优点是,给每台虚拟机设完IP之后,就能够通过同一网段的还有一台主机登陆到Linux。就不用在虚拟机之间来回切换了。(Linux发行版本号为CentOS 5.5)
2. 配置yum库

若本机没有yum库,最好配置安装yum库,方便软件的安装

yum库创建基本步骤

①安装createrepo

②yum -y installl createrepo

③用createrepo创建

createrepo命令用于生成yum仓库所须要的一些基本配置信息。

详细请參考“yum库的安装于配置”

集群配置

1. 设置虚拟机IP

在Vmare虚拟机中虚拟四个Linux主机

LvsMaster:192.168.10.101

LvsBak:        192.168.10.102

RealServer1: 192.168.10.121

RealServer2: 192.168.10.122

基于VMware的虚拟Linux集群搭建-lvs+keepalived

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VveGlhb2ppZV80MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

基于VMware的虚拟Linux集群搭建-lvs+keepalived

主机之间的拓扑关系图

2. 安装ipvsadm 和 keepalived

① 分别在LvsMaster和LvsBak上安装 ipvsadm

yum -y install *ipvsadm*

下载地址:wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar

②分别在LvsMaster和LvsBak上安装 keepalived

安装依赖库:yum install -y openssl openssl-devel

下载keepalived:wget  http://www.keepalived.org/software/keepalived-1.2.6.tar.gz

tar zxvf keepalived-1.2.6.tar.gz

./configure --prefix=/usr/local/keepalived

make

make install

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

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

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

mkdir /etc/keepalived

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

③ 配置 keepalived

LvsMaster配置

global_defs {

    notification_email {

      540621626@qq.com  #emial通知 

    }

    notification_email_from 540621626@qq.com

    smtp_server 192.168.200.1

    smtp_connect_timeout 30

    router_id LVS_DEVEL

}



vrrp_sync_group test {
 #设置vrrp组

group {

loadbalance

}

}

vrrp_instance loadbalance {

    state MASTER                      #设置主机类型,MASTER和BACKUP两种。必须大写 ,即太主机一台备用机

    interface eth0 #设置对外服务网卡

    virtual_router_id 51

    priority 100                                        #设置优先级。备用主机的优先级要比主机低就可以

    advert_int 1#设置同步时间间隔

    authentication {#设置验证类型和password

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {#设置虚拟ip,即浮动ip

        192.168.10.115

    }



virtual_server 192.168.10.115 80 {

    delay_loop 6 #健康检查时间间隔

    lb_algo rr#负载调度算法,默觉得轮训算法

    lb_kind DR#负载均衡转发规则,也就是IP转发规则,DR转发方式最快。可是全部机器必须在一个网段内。还有NAT和TUNEL两种转发方式

    protocol TCP



    real_server 192.168.10.121 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }



    real_server 192.168.10.122 80 {

    weight 1

TCP_CHECK {

    connect_timeout 3

    nb_get_retry 3

    delay_before_retry 3

}

}

}

LvsBakr配置

global_defs {

    notification_email {

     540621626@qqq.com

    }

    notification_email_from 540621626@qq.com

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

    router_id LVS_DEVEL

}



vrrp_sync_group test {

group {

loadbalance

}

}

vrrp_instance loadbalance {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.10.115

    }

}



virtual_server 192.168.10.115 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    protocol TCP



    real_server 192.168.10.121 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }



    real_server 192.168.10.122 80 {

    weight 1

TCP_CHECK {

    connect_timeout 3

    nb_get_retry 3

    delay_before_retry 3

}

    }

}

④ 启动keepalived

LvsMastrer: /etc/init.d/keepalived start

LvsBak     :  /etc/init.d/keepalived  start

查看启动状态

LvsMaster: tail -f /var/log/messages

基于VMware的虚拟Linux集群搭建-lvs+keepalived
基于VMware的虚拟Linux集群搭建-lvs+keepalived

基于VMware的虚拟Linux集群搭建-lvs+keepalived

LvsBak: tail -f /var/log/messages

基于VMware的虚拟Linux集群搭建-lvs+keepalived

基于VMware的虚拟Linux集群搭建-lvs+keepalived
基于VMware的虚拟Linux集群搭建-lvs+keepalived

用ip addr和ipvsadm查看路由情况

LvsMaster: ip addr

基于VMware的虚拟Linux集群搭建-lvs+keepalived

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VveGlhb2ppZV80MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

LvsMaster: ipvsadm

基于VMware的虚拟Linux集群搭建-lvs+keepalived

LvsBak:ip addr

基于VMware的虚拟Linux集群搭建-lvs+keepalived

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VveGlhb2ppZV80MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

LvsBak:ipvsadm

基于VMware的虚拟Linux集群搭建-lvs+keepalived

3. 设置RealServer1和RealServer2

① 编辑RealServe脚本
RealServer1和ResalServer2的脚本设置同样,都是realserver.sh,设置脚本例如以下
SNS_VIP=192.168.10.115

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

case "$1" in

start)

ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up

/sbin/route add -host $SNS_VIP dev 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

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

echo "RealServer Start OK"

;;

stop)

ifconfig lo:0 down

route del $SNS_VIP >/dev/null 2>&1

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
脚本编辑完毕后启动realserver.sh


RealServer1: ./realserver.sh start
RealServer2: ./realserver.sh start
查看IP
基于VMware的虚拟Linux集群搭建-lvs+keepalived
② 在RealServer1和RealServer2上安装apache
安装
yum -y install httpd
配置
ServerName www.example.com
NameVirtualHost 192.168.10.121:80
<VirtualHost 192.168.10.121:80>

DocumentRoot /var/www/html

ServerName   www.example.com

</VirtualHost>
在 /var/www/hmtl/下加入index.html測试主页
启动apache :/etc/init.d/httpd start

4. 測试负载均衡和双机热备

用ipvsadm 查看 LvsMaster 和 LvsBak的路由情况

LvsMaster:  ipvsadm
基于VMware的虚拟Linux集群搭建-lvs+keepalived
LvsBak: ipvsadm

基于VMware的虚拟Linux集群搭建-lvs+keepalived

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VveGlhb2ppZV80MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

基于VMware的虚拟Linux集群搭建-lvs+keepalived

5. 配置过程中应注意的问题

① 配置keepalvied应注意的问题
LvsMaster和LvsBak仅仅有两处不同
state MASTER | BACKUP (主Lvs是MASTER,备用Lv是BACKUP)
priority 100 | 90      (主Lvs是100。备用LVS仅仅要小于100就可以)

② 要关闭iptables或者开启对应的服务port,否则无法通过浏览器訪问