LVS+Keepalived实现负载均衡和双机热备

 1. 说明

         在《keepalived实现Tomcat服务双机热备》中主要讲述如何安装及配置Keepalived来实现双机热备,主要功能是:MASTER和BACKUP两台服务器处于热备状态,对用户虚拟一个IP,屏蔽底层的真实IP地址,用户通过虚拟IP访问当前的MASTER服务器;当MASTER服务器宕机或者发生其他故障时,BACKUP自动切换为MASTER,这个过程对用户时透明的。

         本文主要讲述如何安装ipvsadm、如何配置LVS+Keepalived实现负载均衡和双机热备的功能。功能描述:用户通过虚拟IP访问时,应当会负载均衡到主备服务器上;当然,当MASTER服务器宕机或者发生其他故障时,BACKUP也会自动切换为MASTER。

2. 安装及配置

2.1 安装ipvsadm

         首先安装ipvsadm,本人用的版本是ipvsadm-1.24-6。可以去相关网站进行下载ipvsadm-1.24-6.src.rpm。

         下面安装ipvsadm:(将ipvsadm-1.24-6.src.rpm放在根目录下)

[~] rpm -ivh ipvsadm-1.24-6.src.rpm
[~] cd /usr/src/redhat/SOURCES
[SOURCES] tar -zxvf ipvsadm-1.24.tar.gz
[SOURCES] uname -r
2.6.18-238.el5
[SOURCES] ln -s/usr/src/kernels/2.6.18.238.el5-X86_64/ /usr/src/linux
[SOURCES] cd ipvsadm-1.24
[ipvsadm-1.24] make; make install

2.2 配置keepalived.conf

         接着配置keepalived.conf:(具体安装请参考《keepalived实现Tomcat服务双机热备》)

MASTER(10.10.195.53)配置:

global_defs {
   router_id LVS_DEVEL_1
}
vrrp_script chk_http_port {
   script "/opt/tomcat.pid"
   interval 5
   weight 2
}
vrrp_instance VI_1 {
   state MASTER
   interface eth0
   virtual_router_id 53
   priority 150
   advert_int 1
 
   authentication {
       auth_type PASS
       auth_pass 1111
    }
   track_script {
       chk_http_port
    }
   virtual_ipaddress {
       10.10.195.212  #VIP
    }
}
virtual_server 10.10.195.212 8080 {
       delay_loop 6
       lb_algo wrr
       lb_kind DR
       protocol TCP
       real_server 10.10.195.53 8080 {
                weight 1
                TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 8080
                }
       }
       real_server 10.10.195.190 8080 {
                weight 1
                TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 8080
                }
       }
}

BACKUP(10.10.195.190)配置:

lobal_defs {
   router_id LVS_DEVEL_2
}
vrrp_script chk_http_port {
       script "/opt/tomcat.pid"
       interval 5
       weight 2
}
vrrp_instance VI_1 {
   state BACKUP
   interface eth0
   virtual_router_id 53
   priority 100
    advert_int 1
 
   authentication {
       auth_type PASS
       auth_pass 1111
    }
   track_script {
       chk_http_port
    }
   virtual_ipaddress {
       10.10.195.212
    }
}
virtual_server 10.10.195.212 8080 {
       delay_loop 6
       lb_algo wrr
       lb_kind DR
       protocol TCP
       real_server 10.10.195.53 8080 {
                weight 1
                TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 8080
                }
       }
       real_server 10.10.195.190 8080 {
                weight 1
                TCP_CHECK {
                        connect_timeout 3
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 8080
                }
       }
}

2.3  LVS脚本配置

         两台WEB服务器安装http服务(tomcat)后,创建文件并赋予权限,配置lvs脚本,脚本作用是一直ARP广播,将请求包都由负载均衡lvs服务分配。

         主备机的LVS脚本是相同的。虚拟IP(VIP)是10.10.195.212。下面是脚本的代码:

[~] vim /sbin/realdr.sh

#!/bin/bash

VIP=10.10.195.211
/etc/rc.d/init.d/functions

case "$1" in
start)
        echo "start LVS of REALServer"
        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
#       /sbin/route add -host $VIP dev 1o: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
;;
stop)
        echo "close LVS REALServer"
        /sbin/ifconfig lo:0 down
#       /sbin/route del -host $VIP dev lo:0
        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)
        islothere=`/sbin/ifconfig lo:0 | grep $VIP | wc -l`
        isrothere=`netstat -rn | grep "lo:0" | grep $VIP | wc -l`
#       echo $islothere
#       echo $isrothere
        if [ $islothere -eq 0 ]
        then
                if [ $isrothere -eq 0 ]
                then
                        echo "LVS of REALServer Stoped."
                else
                        echo "LVS of REALServer Running."
                fi
        else
                echo "LVS of REALServer Running."
        fi
;;
*)
        echo "Usage:$0{start|stop}"
        exit 1
;;
esac


设置LVS脚本权限并运行:

[~] chmod 755 /sbin/realdr.sh
[~] /sbin/realdr.sh start

3. 查看

3.1查看虚拟ip相关信息

         之后安装并启动keepalived:service keepalived start.

         可以通过使用ip addshow命令查看(以MASTER为例):

1: lo: <LOOPBACK,UP,LOWER_UP> mtu16436 qdisc noqueue
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
   inet 127.0.0.1/8 scope host lo
    inet 10.10.195.211/32 brd 10.10.195.211 scope global lo:0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast qlen 1000
   link/ether 00:0c:29:6b:f2:a8 brd ff:ff:ff:ff:ff:ff
   inet 10.10.195.53/24 brd 10.10.195.255 scope global eth0
    inet 10.10.195.211/32 scope global eth0

         可以看到lo及eth0上都有虚拟IP地址。

3.2 查看负载均衡状态

在终端输入命令:ipvsadm可以查看。

MASTER(10.10.195.53):

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
 -> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP 10.10.195.212:webcache wrr
 -> 10.10.195.190:webcache      Route   1      0         11
 -> shr:webcache                Local   1      0         0

BACKUP(10.10.195.190):

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
 -> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP 10.10.195.211:webcache wrr
 -> server1:webcache            Local   1      0         0
 -> 10.10.195.53:webcache       Route   1      0         11

4. 测试

         如果多次打开浏览器,通过虚拟IP访问网站,应当会负载均衡到两台服务器上。第一次打开一个浏览器并输入http://10.10.195.212:8080/,显示10.10.195.53(190)服务的内容;第二次打开一个浏览器并输入http://10.10.195.212:8080/,显示10.10.195.190(53)服务器的内容。

你可能感兴趣的:(keepalived,LVS)