LVS+keepalived构建高可用负载均衡群集(DR)

 名称
角色
IP地址
Load Balancer
(RHEL5)
均衡器
(LVS-Master)
eth0:0(Vip):192.168.1.198
eth0:192.168.1.196
                                             
Load Balancer
(RHEL5)
均衡器
(LVS-Slave)
eth0:0(Vip):192.168.1.198
eth0:192.168.1.197
 
RealServer1
(RHEL5)
服务器群集节点1
(Web服务器)
lo:0(Vip):192.168.1.198
eth0:192.168.1.191
                                          
RealServer2
(RHEL5)
服务器群集节点2
(Web服务器)
lo:0(Vip):192.168.1.198
eth0:192.168.1.192
                                           

实验步骤:
一、Load Balancer1(LVS-Master)配置 (RHEL6)
1) 设置网卡IP地址
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:0C:29:4C:BD:42
ONBOOT=yes
IPADDR=192.168.1.196
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
2)安装支持软件
[root@localhost ~]# mount /dev/cdrom /media/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost media]# vim /etc/yum.repos.d/rhel-source.repo
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=file:///media/Server
enabled=1
gpgcheck=0
[root@localhost media]# yum -y install kernel-devel openssl-devel popt-devel ipvsadm
3) 安装keepalived所需软件
[root@localhost ~]# tar zxf keepalived-1.2.2.tar.gz
[root@localhost ~]# cd keepalived-1.2.2
[root@localhost keepalived-1.2.2]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686 ;make ;make install //此内核是RHEL6上的,若使用其它RHEL版本则会报错
4)使用keepalived服务
[root@localhost ~]# ls -l /etc/init.d/keepalived
-rwxr-xr-x. 1 root root 1288 5月 10 14:27 /etc/init.d/keepalived
[root@localhost ~]# chkconfig --add keepalived
[root@localhost ~]# chkconfig keepalived
5)配置/etc/keepalived/keepalived.conf主配置文档
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vi keepalived.conf
global_defs {
   router_id LVS_DEVEL_1   //本路由器(服务器)的名称
}
vrrp_instance VI_1 {       //定义vrrp热备实例
    state MASTER       //热本状态,MASTER表示主服务器
    interface eth0       //承载vip的物理端口
    virtual_router_id 51   //虚拟路由器的ID号,每个热备组保持一致
    priority 150         //优先级,数值越大优先级越高,这里应该主服务器高于辅助的
    advert_int 1         //通告间隔秒数(心跳频率)
     authentication {      //认证信息,每个热备组保持一致
        auth_type PASS   //认证类型
        auth_pass 1111   //认证密码
    }
    virtual_ipaddress {     //指定漂移地址(vip),可以有多个
        192.168.1.198
    }
}
virtual_server 192.168.1.198 80 {     //虚拟服务器地址(vip)、端口
    delay_loop 6                 //健康检查间隔时间
    lb_algo rr                     //轮询(rr)调度算法
    lb_kind DR                   //直接路由模式(DR)
protocol TCP                   //应用服务采用的模式是TCP模式
 
    real_server 192.168.1.191 80 {     //第一个Web节点的地址、端口
        weight 1                    //节点的权重
        TCP-CHECK {                 //健康检查方式
            connect_timeout 3        //连接超时(秒)
            nb_get_retry 3            //重试次数
            delay_before_retry 3       //重试间隔(秒)
            connect_port 80          //检查的目标端口
    }
}
    real_server 192.168.1.192 80 {       //第二个Web节点
        weight 1
        TCP-CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
    }
}
}
6)重新启动keepalived服务
[root@localhost keepalived]# service keepalived restart
停止 keepalived:                                           [失败]
正在启动 keepalived:                                       [确定]
[root@localhost keepalived]#ip add show dev eth0
二、Load Balancer2(LVS-Slave)配置(RHEL6)
1) 设置网卡IP地址
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:0C:29:AB:B0:02
ONBOOT=yes
IPADDR=192.168.1.197
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
2)安装支持软件
[root@localhost ~]# mount /dev/cdrom /media/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost media]# vim /etc/yum.repos.d/rhel-source.repo
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=file:///media/Server
enabled=1
gpgcheck=0
[root@localhost media]# yum -y install kernel-devel openssl-devel popt-devel ipvsadm
3) 安装keepalived所需软件
[root@localhost ~]# tar zxf keepalived-1.2.2.tar.gz
[root@localhost ~]# cd keepalived-1.2.2
[root@localhost keepalived-1.2.2]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686 ;make ;make install //此内核是RHEL6上的,若使用其它RHEL版本则会报错
4)使用keepalived服务
[root@localhost ~]# ls -l /etc/init.d/keepalived
-rwxr-xr-x. 1 root root 1288 5月 10 14:27 /etc/init.d/keepalived
[root@localhost ~]# chkconfig --add keepalived
[root@localhost ~]# chkconfig keepalived
5)配置/etc/keepalived/keepalived.conf主配置文档, LVS-Master上的配置文件拷过来修改如下三个参数即可
[root@localhost keepalived]# scp keepalived.conf [email protected]:/etc/keepalived/此步骤在LVS-Master上做,向197辅助服务器拷贝  //
[root@localhost ~]# cd /etc/keepalived/   //回到LVS-Slave上编辑
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vi keepalived.conf
global_defs {
   router_id LVS_DEVEL_2 //本路由器(服务器)的名称
}
vrrp_instance VI_1 {       //定义vrrp热备实例
    state SLAVE       //热本状态,MASTER表示主服务器
    interface eth0       //承载vip的物理端口
    virtual_router_id 51   //虚拟路由器的ID号,每个热备组保持一致
    priority 100          //优先级,数值越大优先级越高,这里应该主服务器高于辅助的
    advert_int 1         //通告间隔秒数(心跳频率)
    authentication {      //认证信息,每个热备组保持一致
        auth_type PASS   //认证类型
        auth_pass 1111   //认证密码
    }
    virtual_ipaddress {     //指定漂移地址(vip),可以有多个
        192.168.1.198
    }
}
virtual_server 192.168.1.198 80 {     //虚拟服务器地址(vip)、端口
    delay_loop 6                 //健康检查间隔时间
    lb_algo rr                     //轮询(rr)调度算法
    lb_kind DR                   //直接路由模式(DR)
protocol TCP                   //应用服务采用的模式是TCP模式
 
    real_server 192.168.1.191 80 {     //第一个Web节点的地址、端口
        weight 1                    //节点的权重
        TCP-CHECK {                 //健康检查方式
            connect_timeout 3        //连接超时(秒)
            nb_get_retry 3            //重试次数
            delay_before_retry 3       //重试间隔(秒)
            connect_port 80          //检查的目标端口
    }
}
    real_server 192.168.1.192 80 {       //第二个Web节点
        weight 1
        TCP-CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
    }
}
}
6)重新启动keepalived服务
[root@localhost keepalived]# service keepalived restart
停止 keepalived:                                           [失败]
正在启动 keepalived:                                       [确定]
[root@localhost keepalived]#ip add show dev eth0
三、Realserver服务器上的配置 (RHEL5)
提供服务的RealServer1和RealServer2不需要安装任何IPVS软件,只需要设置在开机后自动执行以下脚本:
1)在RealServer1上的设置
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:0C:29:AB:B0:02
ONBOOT=yes
IPADDR=192.168.1.191
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
2)编写lvs-dr脚本
[root@localhost ~]# vim /opt/lvs-dr
#!/bin/bash
vip="192.168.1.198"
/sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
/sbin/route add -host $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
[root@localhost ~]# chmod +x /opt/lvs-dr    //添加脚本lvs-dr的执行权限
[root@localhost ~]# echo "/opt/lvs-dr" >> /etc/rc.local //让脚本lvs-dr随机开机启动
[root@localhost ~]# /opt/lvs-dr    //执行lvs-dr脚本
3)挂载光盘,YUM安装apache服务
[root@localhost ~]# mount /dev/cdrom /media/
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@localhost ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo
[rhel-debuginfo]
name=Red Hat Enterprise Linux $releasever - $basearch - Debug
baseurl=file:///media/Server
enabled=1
gpgcheck=0
[root@localhost ~]# yum -y install http*
[root@localhost opt]# echo "1111" > /var/www/html/index.html //在RealServer1添加测试页
[root@localhost opt]# service httpd restart
停止 httpd                                               [确定]
启动 httpd:                                                [确定]
1)在RealServer2上的设置
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:0C:29:AB:B0:02
ONBOOT=yes
IPADDR=192.168.1.192
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
2)编写lvs-dr脚本, RealServer1节点上的lvs-dr拷贝到/opt下
[root@localhost ~]# cd /opt/  //节点一上
[root@localhost opt]# scp lvs-dr [email protected]:/opt/在节点一做,将节点一上/opt/lvs-dr文件拷贝到192的/opt目录下即可,省去下面的在节点二上编辑/opt/lvs-dr   //
[root@localhost ~]# vim /opt/lvs-dr     //或者直接在节点二上进行此步骤
#!/bin/bash
vip="192.168.1.198"
/sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
/sbin/route add -host $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
[root@localhost ~]# chmod +x /opt/lvs-dr    //添加脚本lvs-dr的执行权限
[root@localhost ~]# echo "/opt/lvs-dr" >> /etc/rc.local //让脚本lvs-dr随机开机启动
[root@localhost ~]# /opt/lvs-dr    //执行lvs-dr脚本
3)挂载光盘,YUM安装apache服务
[root@localhost ~]# mount /dev/cdrom /media/
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@localhost ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo
[rhel-debuginfo]
name=Red Hat Enterprise Linux $releasever - $basearch - Debug
baseurl=file:///media/Server
enabled=1
gpgcheck=0
[root@localhost ~]# yum -y install http*
[root@localhost opt]# echo "1111" > /var/www/html/index.html //在RealServer1添加测试页
[root@localhost opt]# service httpd restart
停止 httpd:                                                [确定]
启动 httpd:                                                [确定]
四、测试
1)启动keepalived服务
分别在主、副均衡服务器上启动keepalived服务,启动命令为:
[root@localhost ~]# service keepalived start
启动 keepalived:                                           [确定]

你可能感兴趣的:(LVS+keepalived,构建高可用负载均衡群集)