LVS+Keepalived+Nginx+Tomcat部署实现

环境介绍

1.两台前端 keepalived+lvs,热备的方式,保证一台lvs前端能正常访问就行,如果一台down,另外一台热备升级到master主机
master: 192.168.20.85 centos6
salve: 192.168.20.87 centos6

2.后端两台web服务器通过lvs的算法轮询被访问
web1: 192.168.1.204 centos 5.10 nginx+tomcat
web2: 192.168.1.206 centos 5.10 nginx+tomcat

3.vip: 192.168.1.207(虚拟IP)

安装配置nginx和tomcat过程请参考其相关文档,在此不做详细说明。

安装配置LVS+Keepalived

1.首先 web1和web2都不用装ipvsadm和keepalived,只要启用一个脚本即可,
当然前提是你已经把nginx+tomcat已经配置完,并能正常访问页面。

编辑脚本realserver.sh文件

#!/bin/bash  
#   
# Script to start LVS DR real server.   
# description: LVS DR real server   
#   
. /etc/rc.d/init.d/functions
VIP=192.168.20.90   #这里根据需要改成自己的VIP地址
host=`/bin/hostname`
case "$1" in  
start)   
       # Start LVS-DR real server on this machine.   
        /sbin/ifconfig lo down   
        /sbin/ifconfig lo up   
        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
        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up  
        /sbin/route add -host $VIP dev lo:0
;;  
stop)
        # Stop LVS-DR real server loopback device(s).  
        /sbin/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
;;  
status)
        # Status of LVS-DR real server.  
        islothere=`/sbin/ifconfig lo:0 | grep $VIP`   
        isrothere=`netstat -rn | grep "lo:0" | grep $VIP`   
        if [ ! "$islothere" -o ! "isrothere" ];then   
            # Either the route or the lo:0 device   
            # not found.   
            echo "LVS-DR real server Stopped."   
        else   
            echo "LVS-DR real server Running."   
        fi   
;;   
*)   
            # Invalid entry.   
            echo "$0: Usage: $0 {start|status|stop}"   
            exit 1   
;;   
esac

只需要改脚本中的VIP即可,VIP可以任意设置成未使用的IP,其他都一样。

然后,分别将realserver.sh上传至web1、web2服务器,cd对应目录执行命令:

chmod +x realserver.sh

./realserver.sh start

启动realserver服务,可通过ifconfig和route -n来查询刚才脚本实现的功能,如果可以把此脚本放在启动时运行。

启动报错情况(不报错的请略过):

如果这个脚本在Windows下编辑过,就有可能被转换成Windows下的dos文本格式了,这样的格式每一行的末尾都是以\r\n来标识,它的ASCII码分别是0x0D,0x0A。如果你将这个脚本文件直接放到Linux上执行就会报/bin/bash^M: bad interpreter错误提示。
解决方法很简单,首先你先要检查一下看看你的脚本文件是不是这个问题导致的,用vi命令打开要检查的脚本文件,然后用
:set ff?
命令检查一下,看看是不是dos字样,如果是dos格式的,继续执行
:set ff=unix
然后执行
:qw
保存退出即可

ifconfig命令

LVS+Keepalived+Nginx+Tomcat部署实现_第1张图片

route -n命令

LVS+Keepalived+Nginx+Tomcat部署实现_第2张图片

2.在master和salve分别安装ipvsadm和keepalived

yum install -y keepalived ipvsadm (centos redhat)
或者
apt-get install keepalived ipvsadm (debian ubuntu)

安装完ipvsadm和keepalived,不用配置lvs,直接用keepalived来启用lvs就行。

3.在master主机设置:

vim /etc/keepalived/keepalived.conf

global_defs

{

router_id master_85

}

  

vrrp_instance aiyou {

state MASTER

interface eth0    #这个要和本机网卡名字一致,否则故障切换不能生效

virtual_router_id 100    #这个数值masterslave必须统一

priority 151     #这个数值决定哪台服务器是master

advert_int 1

authentication {

        auth_type PASS

        auth_pass 123456

        }

virtual_ipaddress {

        192.168.20.90

        }

}

virtual_server 192.168.20.90 80 {

            delay_loop 6

            lb_algo wrr

            lb_kind DR

#            persistence_timeout 50

            protocol TCP

real_server 192.168.20.88 80 {

                weight 1

                TCP_CHECK {

                    connect_timeout 3

                    nb_get_retry 3

                    delay_before_retry 3

                    connect_port 80

                }

            }

real_server 192.168.20.89 80 {

                weight 1

                TCP_CHECK {

                    connect_timeout 3

                    nb_get_retry 3

                    delay_before_retry 3

                    connect_port 80

                }

            }

}

启动keepalived

/etc/init.d/keepalived start

4.在slave主机设置:

vim /etc/keepalived/keepalived.conf

global_defs

{

router_id slave_87

}

  

vrrp_instance aiyou {

state MASTER

interface eth1    #这个要和本机网卡名字一致,否则故障切换不能生效

virtual_router_id 100    #这个数值masterslave必须统一

priority 150     #这个数值决定哪台服务器是master这里我们比master数值低,所以角色是backup

advert_int 1

authentication {

        auth_type PASS

        auth_pass 123456

        }

virtual_ipaddress {

        192.168.20.90

        }

}

virtual_server 192.168.20.90 80 {

            delay_loop 6

            lb_algo wrr

            lb_kind DR

#            persistence_timeout 50

            protocol TCP

real_server 192.168.20.88 80 {

                weight 1

                TCP_CHECK {

                    connect_timeout 3

                    nb_get_retry 3

                    delay_before_retry 3

                    connect_port 80

                }

            }

real_server 192.168.20.89 80 {

                weight 1

                TCP_CHECK {

                    connect_timeout 3

                    nb_get_retry 3

                    delay_before_retry 3

                    connect_port 80

                }

            }

}

启动keepalived

/etc/init.d/keepalived start

可以运行 ipvsadm -ln(主从服务器都验证) 来查看是否启用

好了,现在我们直接访问http://192.168.20.90 就实现了,高可用性,高负载的集群

注意:如果访问不了,考虑一下防火墙有木有关掉,关闭防火墙命令:

centos 7
systemctl stop firewalld.service #停止
systemctl disable firewalld.service #禁用
之前的版本:
service iptables stop #停止
chkconfig iptables off #禁用


注意事项:

realserver一定要执行realserver.sh脚本

vip端口和realserver端口必须一致

如果出现服务器403错误执行chmod -R  777 项目文件夹路径


参考文章:

http://my.oschina.net/liting/blog/384369?p=1

http://lovelace.blog.51cto.com/1028430/1550188

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