HAproxy-Keepalived自動インストール配置shell
#!/bin/bash
##################################
haproxy_path=/usr/local/haproxy
haproxy_conf=/usr/local/haproxy/conf/haproxy.cfg
#############haproxy-install#####################
cd /tmp
tar xvf haproxy-1.4.*.tar.gz
if [ $? -eq 0 ];then
echo “haproxy tar ok"
else
echo “haproxy tar no"
exit
fi
sleep 5
yum install gcc openssl openssl-devel make kernel-devel popt-devel openssl -y
cd /tmp/haproxy-*
make TARGET=linux26 PREFIX=$haproxy_path install
if [ $? -eq 0 ];then
echo “haproxy install ok"
else
echo “haproxy install no"
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 dontlognull\n\toption redispatch\n\tretries 2\n\tmaxconn 2000\n\tcontimeout 5000\n\tclitimeout 50000\n\tsrvtimeout 50000" >> $haproxy_conf
echo -e “\nlisten web 0.0.0.0:80\n\tmode http\n\tbalance roundrobin\n\tcookie SERVERID insert indirect nocache\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 server$i $ip:80 cookie $i check inter 2000 rise 2 fall 5" >> $haproxy_conf
done
###########################keepalived###############################
mandir=/usr/local/src/man
kernel=$(uname -r)
keepalived_conf=/etc/keepalived/keepalived.conf
################################
cd /tmp
tar -xvf keepalived*.tar.gz
cd /tmp/keepalived-*
#############keepalived make##################
./configure –prefix=/ –mandir=$mandir –with-kernel-dir=/usr/src/kernels/$kernel*
if [ $? -eq 0 ];then
make
if [ $? -eq 0 ];then
make install
if [ $? -eq 0 ];then
echo “keepalived ok"
else
echo “keepalived no"
exit
fi
else
echo “keeplaived make no"
exit
fi
else
echo “keelalived make no"
fi
touch /etc/keepalived/keepalived.conf
########################################################
read -p “NIC binding " network
####################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 "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\t priority $priority\n\tadvert_int 1\n\tauthentication {\n\tauth_type PASS\n\tauth_pass tdoa\n\t}\n\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
#############################################################################
chmod +x /etc/rc.d/haproxy.sh
/usr/bin/nohup /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
############################################################################