LVS+keepalived+tomcat负载均衡

LVS+keepalived+tomcat负载均衡

一:环境

System:centos 6.5 x86_64

名称

IP

 LVS-Master

192.168.10.98

 LVS-BACKUP

192.168.10.99

 LVS-VIP

192.168.10.97

 Realserver1

192.168.10.98

 Realserver2

192.168.10.99



二:LVS+keepalived+tomcat的安装和配置

1.相关软件的安装

[root@tomcat-cluster1 ~]# yum -y install ipvsadm

[root@tomcat-cluster1 ~]# yum -y install keepalived

[root@tomcat-cluster1 ~]#wget 

http://mirror.esocc.com/apache/tomcat/tomcat-7/v7.0.53/bin/apache-tomcat-7.0.53.tar.gz

[root@tomcat-cluster1 ~]#tar -zxvfapache-tomcat-7.0.53.tar.gz

[root@tomcat-cluster1 ~]# mv apache-tomcat-7.0.53 tomcat

=============================================================

[root@tomcat-cluster2 ~]# yum -y install ipvsadm

[root@tomcat-cluster2 ~]# yum -y install keepalived

[root@tomcat-cluster2 ~]#wget 

http://mirror.esocc.com/apache/tomcat/tomcat-7/v7.0.53/bin/apache-tomcat-7.0.53.tar.gz

[root@tomcat-cluster2 ~]#tar -zxvfapache-tomcat-7.0.53.tar.gz

[root@tomcat-cluster2 ~]# mv apache-tomcat-7.0.53 tomcat

2..配置软件

配置keepadlived

global_defs {

notification_email {

               [email protected]    #email 通知

        }

        notification_email_from [email protected]

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        router_id LVS1          # 设置lvs的id,在一个网络内应该是唯一的

}

vrrp_sync_group test {           #设置vrrp组

group {

        loadbalance

}

}

 

vrrp_instance loadbalance {

        state MASTER       #设置lvs的状态,报错MASTER和BACKUP两种,必须大写

        interface eth0     #设置对外服务的接口

        lvs_sync_daemon_inteface eth0   #设置lvs监听的接口

        virtual_router_id 51                     #设置虚拟路由表示

        priority 180            #设置优先级,数值越大,优先级越高

        advert_int 5           #设置同步时间间隔

authentication {                    #设置验证类型和密码

        auth_type PASS

        auth_pass 1111

}

virtual_ipaddress {                #设置lvs vip

        192.168.10.97

}

}

 

virtual_server 192.168.10.97 8080 {

        delay_loop 6          #健康检查时间间隔

        lb_algo rr               #负载均衡调度算法

        lb_kind DR            #负载均衡转发规则

        #persistence_timeout 20  #设置会话保持时间,对bbs等很有用

        protocol TCP                #协议

        real_server 192.168.10.98 8080 {

        weight 3                #设置权重

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 8080

}

}

        real_server 192.168.10.99 8080 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 8080

}

}

}

 

LVS-backup的配置文件如下

global_defs {

notification_email {

               [email protected]

        }

        notification_email_from [email protected]

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        router_id LVS2

}

vrrp_sync_group test {

group {

        loadbalance

}

}

 

vrrp_instance loadbalance {

        state BACKUP

        interface eth0

        lvs_sync_daemon_inteface eth0

        virtual_router_id 51

        priority 150

        advert_int 5

authentication {

        auth_type PASS

        auth_pass 1111

}

virtual_ipaddress {

        192.168.10.97

}

}

 

virtual_server 192.168.10.97 8080 {

        delay_loop 6

        lb_algo rr

        lb_kind DR

        #persistence_timeout 20

        protocol TCP

        real_server 192.168.10.98 8080 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

        real_server 192.168.10.99 8080 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 8080

}

}

}

 Realserver的配置

Realserver的脚本都一样

cat /etc/rc.d/init.d/realserver.sh

#!/bin/bash

# description: Config realserver lo and apply noarp

 

SNS_VIP=192.168.1.115

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

 

case "$1" in

start)

       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP

       /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

 测试

Tomcat测试

修改

==========================

[root@tomcat-cluster1 ~]# cat tomcat/webapps/examples/index.html |grep h3

<h3>Apache Tomcat Examples</H3>

<h3>This is tomcat-cluster1 page!!!<H3>

========================================

[root@tomcat-cluster2 ~]# cat tomcat/webapps/examples/index.html |grep h3

<h3>Apache Tomcat Examples</H3>

<h3>This is tomcat-cluster2 page!!!<H3>

访问:

http://192.168.10.99:8080/examples/

Apache Tomcat Examples

This is tomcat-cluster2 page!!!

http://192.168.10.98:8080/examples/

Apache Tomcat Examples

This is tomcat-cluster1 page!!!

one:

1)        首先测试各个readserver,确定各个readserver都能正常访问,测试realserver.sh脚本,看启动后 能不能绑定lvs vip,停止后能不能去除绑定的vip

2)       测试lvs能否进行负载均衡转发,利用ipvsadm命令查看,如果想只当当前的 请求转发到哪个服务器去了,可以用ipvsadm加选项来查看,其完整形式为:ipvsadm –lcn | grep 192.168.0.97

3)        停掉主lvslvs backup是否接管!

4)        更详细信息请查看日志,tail -f /var/log/messages

tow:

访问vip地址如果显示

This is tomcat-cluster2 page!!!

1)    停掉tomcat 99服务器,这时服务正常会切换到tomcat98服务器,显示

This is tomcat-cluster1 page!!!

访问vip地址如果显示

This is tomcat-cluster1 page!!!

1)    停掉tomcat 98服务器,这时服务正常会切换到tomcat99服务器,显示

This is tomcat-cluster2 page!!!

这次tomcat负载均衡集群服务器的高可用就成功了。



你可能感兴趣的:(linux,负载均衡)