1. 程式人生 > >nginx+keepalived+tomcat做web負債均衡高可用

nginx+keepalived+tomcat做web負債均衡高可用

cat ive keepalved inter uri lse echo yum src

vip: 10.110.200.200
nginx1(keepalived): 10.110.200.201
nignx2(keepalived): 10.110.200.202
tomcat1: 10.110.200.203
tomcat2: 10.110.200.204

  1. tomcat1-2:
    yum install tomcat -y
    java -version

  2. nginx1-2:
    yum install nginx keepalived -y
  3. nginx1(keepalived):
    vim /etc/keepalived/keepalved.conf

             vrrp_script check_nginx

    {
    script "/etc/keepalived/check_nginx.sh"

    interval 2
    weight 5
    user root

}
vrrp_instance VI_1 {
state MASTER
interface eth0
mcast_src_ip 10.110.200.201
virtual_router_id 51
preempt
priority 120
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

track_script {
check_nginx
}
virtual_ipaddress {
10.110.200.200
}
}

vim /etc/keepalived/check_nginx.sh

#!/bin/bash
count=0
for (( k=0; k<2; k++ ))

do
check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\n" http://localhost -o /dev/null )
if [ "$check_code" != "200" ]; then
count=count+1
continue
echo $count
else
count=0
break
echo "break"
fi
done
if [ "$count" != "0" ]; then
systemctl stop keepalived
echo "keepalived is stoped"
exit 1
else
exit 0
fi
或者:
#!/bin/bash

check_code=curl -o /dev/null -s -m 10 --connect-timeout 10 -w"%{http_code}" "localhost"
if [ "$check_code" != 200 ];then
systemctl stop keepalived
break
else
break
fi

  1. nginx2(keepalived):
    vim /etc/keepalived/keepalved.conf

             vrrp_script check_nginx

    {
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -5
    user root
    enable_script_security
    }
    vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    mcast_src_ip 10.110.200.202
    virtual_router_id 51
    preempt
    priority 120
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }

track_script {
check_nginx
}
virtual_ipaddress {
10.110.200.200
}
}

vim /etc/keepalived/check_nginx.sh

#!/bin/bash
count=0
for (( k=0; k<2; k++ ))
do
check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\n" http://localhost -o /dev/null )
if [ "$check_code" != "200" ]; then
count=count+1
continue
echo $count
else
count=0
break
echo "break"
fi
done
if [ "$count" != "0" ]; then
systemctl stop keepalived
echo "keepalived is stoped"
exit 1
else
exit 0
fi

nginx+keepalived+tomcat做web負債均衡高可用