etge 发表于 2014-7-1 10:41:16

HAproxy-Keepalived快速安装Shell脚本

#!/bin/bash
#########################################################################################
haproxy_path=/usr/local/haproxyhaproxy_conf=/usr/local/haproxy/conf/haproxy.cfg
#########################################################################################
yum install gcc openssl openssl-devel make kernel-devel popt-devel openssl -y
#######################haorixy-tar#######################################################
cd /tmptar xvf haproxy-1.4.*.tar.gz
if [ $? -eq 0 ];then
    echo-------------------------
    echo |Haproxy tar is OK !!!   |
    echo-------------------------
else
    echo--------------------------                                          
    echo |Haproxy tar is NO !!!   |
    echo--------------------------
#########################################################################################
exit
fi
sleep 5
############haproxyinstall###############################################################
cd /tmp/haproxy-*make TARGET=linux26 PREFIX=$haproxy_path install
if [ $? -eq 0 ];then
    echo------------------------------                                    
    echo |   Haproxy install is OK !!!|
    echo-----------------------------
else
    echo------------------------------                                          
    echo |Haproxy install is NO !!!    |
    echo------------------------------
exit
fi
#########################################################################################
cd $haproxy_path
mkdir conf logs
touch $haproxy_path/conf/haproxy.cfg
####################haproxy.cfg##########################################################
echo -e "global\n\tlog 127.0.0.1 local0\n\tmaxconn 65535\n\tchroot $haproxy_path\n\tuser nobody\n\tgroup nobody\n\tdaemon\n\tnbproc 8\n\tpidfile $haproxy_path/haproxy.pid\n" > $haproxy_conf

echo -e "\ndefaults\n\tlog 127.0.0.1 local3\n\tmode http\n\toption httplog\n\toption httpclose\n\toption dontlognull\n\toption forwardfor\n\tretries 2\n\toption redispatch\n\t retries 2\n\tmaxconn 2000\n\tbalance source\n\tstats refresh 15s\n\tstats   uri/status\n\tstats auth haproxy:haproxy\n\tcontimeout 5000\n\tclitimeout 50000\n\tsrvtimeout 50000" >> $haproxy_conf

echo -e "\nlisten\n\tbind*:80\n\tmode http\n\toption httplog\n\tlog global\n\toption httpchk HEAD /index.html HTTP/1.0" >> $haproxy_conf

read -p "Requires several server " server
for ((i=1;i<=$server;i++))
do
read -p "server ip " ip
echo -e "server $ip $ip:80 weight 5check inter 2000 rise 2 fall 3" >> $haproxy_conf
done
###########################keepalived####################################################
mandir=/usr/local/src/
mankernel=$(uname -r)
keepalived_conf=/etc/keepalived/keepalived.conf
#########################################################################################
cd /tmp
tar -xvf keepalived*.tar.gz
cd /tmp/keepalived-*
./configure --prefix=/ --mandir=$mandir --with-kernel-dir=/usr/src/kernels/$kernel*if [ $? -eq 0 ];then
make && make install
if [ $? -eq 0 ];then
    echo-------------------------------                                          
    echo |   Keepalved install is OK !!!|
    echo-------------------------------
else
    echo---------------------------------                                          
    echo |Keepalived install is NO !!!    |
    echo---------------------------------
exit
fi
sleep 5
#########################################################################################
touch /etc/keepalived/keepalived.conf
####################keepalived.conf######################################################
#############Global configuration########################
echo -e " ! Configuration File for keepalived\nglobal_defs {\n\tnotification_email {\n\tjqbash@gmail.com\n\t}\n\tnotification_email_from root@localhost\n\tsmtp_server 127.0.0.1\n\tsmtp_connect_timeout 30\n\trouter_id HAproxy\n}" > $keepalived_conf
############Virtual IP Configuration####################
read -p "Requires several server " server
for ((a=1;a<=$server;a++))
do
read -p "MASTER of BACKUP " states
read -p "priority " priority
read -p "NIC binding" network
read -p "virtual_router id " id
read -p "vip address " vip
echo -e "vrrp_instance VI_$a {\n\tstate$states\n\tinterface$network\n\tvirtual_router_id $id\n\tpriority $priority\n\tadvert_int 1\n\tauthentication {\n\t\tauth_type PASS\n\t\tauth_pass tdoa\n\t}\n\t\tvirtual_ipaddress {\n\t$vip/24 dev $network\n\t}\n}" >> $keepalived_conf
done
#########################################################################################
/usr/local/haproxy/sbin/haproxy -f $haproxy_conf
/etc/rc.d/init.d/keepalived restart
#########################haproxy-keepalived.sh###########################################
touch /etc/rc.d/haproxy-keepalived.sh
echo -e '#!/bin/bash\nwhile\t:\ndo\n haproxypid=$(ps -C haproxy --no-header | wc -l)\n\tif [ $haproxypid -eq 0 ];then\n\t\t/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg\n\t\tsleep 5\n\thapropid=$(ps -C haproxy --no-header | wc -l)\n\t\tif [ $hapropid -eq 0 ];then\n\t\t\t\t/etc/init.d/keepalived stop\n\t\tfi\n\tfi\n\tsleep 5\n proxypid=$(ps -C haproxy --no-header | wc -l)\n\tif (($proxypid!= 0));then\n\t\t/etc/init.d/keepalived start\n\tfi\n\tsleep 5\ndone' > /etc/rc.d/haproxy.sh
#########################################################################################
rc_local=/etc/rc.local
echo '/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg' >> $rc_local
echo '/etc/rc.d/init.d/keepalived start' >> $rc_local
echo '/usr/bin/nohup /etc/rc.d/haproxy.sh &' >> $rc_local
#########################################################################################
chmod +x /etc/rc.d/haproxy.sh
/usr/bin/nohup /etc/rc.d/haproxy.sh &
#########################################################################################


页: [1]
查看完整版本: HAproxy-Keepalived快速安装Shell脚本