lvs+keepalived+nginx+tomcat高可用高性能集羣部署

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 linux

vip: 192.168.1.207 nginx

1:首先 web1和web2都不用裝ipvsadm和keepalived,只要啓用一個腳本便可,
固然你已經把nginx+tomcat已經配置完,並能正常訪問頁面 web

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

ifconfig ubuntu

route -n

route -n vim

2:而後再master和salve分別安裝ipvsadm和keepalived 後端

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

安裝完ipvsadm和keepalived,不用配置lvs,直接用keepalived來啓用lvs就行 tomcat

在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

ipvsadm

好了,如今咱們直接訪問 http://192.168.1.207 就實現了,高可用性,高負載的集羣

相關文章
相關標籤/搜索