LVS+keepalived+nginx+tomcat部署实现

阅读更多

# 拓扑如下所示

LVS+keepalived+nginx+tomcat部署实现_第1张图片

2台前端 keepalived+lvs,热备的方式,保证一台lvs前端能正常访问就行,如果一台down,另外一台热备升级到master主机
master: 192.168.1.210 ubuntu 12.0.4
salve: 192.168.1.211 ubuntu 12.0.4

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

vip: 192.168.1.207

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

vim 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.1.207   #这里根据需要改成自己的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
chmod +x realserver.sh
./realserver.sh start

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

ifconfig

route -n

2:然后再master和salve分别安装ipvsadm和keepalived

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

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

在master主机设置:

vim /etc/keepalived/keepalived.conf
global_defs
{
router_id master_210
}
 
vrrp_instance shengzc {
state MASTER
interface eth0
virtual_router_id 100    #这个数值 master和slave必须统一
priority 151     #这个数值决定哪台服务器是master 
advert_int 1
authentication {
        auth_type PASS
        auth_pass 123456
        }
virtual_ipaddress {
        192.168.1.207
        }
}
virtual_server 192.168.1.207 80 {
            delay_loop 6
            lb_algo wrr
            lb_kind DR
#            persistence_timeout 50
            protocol TCP
real_server 192.168.1.204 80 {
                weight 1
                TCP_CHECK {
                    connect_timeout 3
                    nb_get_retry 3
                    delay_before_retry 3
                    connect_port 80
                }
            }
real_server 192.168.1.206 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

在slave主机设置:

vim /etc/keepalived/keepalived.conf
global_defs
{
router_id slave_211
}
 
vrrp_instance shengzc {
state MASTER
interface eth0
virtual_router_id 100    #这个数值 master和slave必须统一
priority 150     #这个数值决定哪台服务器是master 这里我们比master数值低,所以角色是backup,
advert_int 1
authentication {
        auth_type PASS
        auth_pass 123456
        }
virtual_ipaddress {
        192.168.1.207
        }
}
virtual_server 192.168.1.207 80 {
            delay_loop 6
            lb_algo wrr
            lb_kind DR
#            persistence_timeout 50
            protocol TCP
real_server 192.168.1.204 80 {
                weight 1
                TCP_CHECK {
                    connect_timeout 3
                    nb_get_retry 3
                    delay_before_retry 3
                    connect_port 80
                }
            }
real_server 192.168.1.206 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 来查看是否启用

ipvsadm

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

参考文章:

http://www.5ilinux.com/2014/05/lvs_keepalived_nginx_tomcat.html

你可能感兴趣的:(LVS)