haproxy+keepalived 部署脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了haproxy+keepalived 部署脚本相关的知识,希望对你有一定的参考价值。

废话不多说,直接上脚本,编译安装的版本为:haproxy-1.5.2.tar.gz,keepalived-1.2.13.tar.gz

install.sh

#!/bin/bash

pack_source="/usr/local/src"
pack_target="/usr/local/haproxy"
ha_package="haproxy-1.5.2.tar.gz"
ha_pack_dir="haproxy-1.5.2"
keep_package="keepalived-1.2.13.tar.gz"
keep_pack_dir="keepalived-1.2.13"
if [ -f "/usr/local/sbin/haproxy" ];then echo "ERROR,haproxy already install ,please uninstall first!" exit 1 fi sudo grep -q haproxy /etc/passwd if [ $? -ne 0 ];then sudo useradd haproxy fi sudo yum install -y gcc sudo yum install -y openssl-devel #install haproxy sudo mkdir -p /usr/local/haproxy cd $pack_source rm -rf $pack_source/$ha_pack_dir sudo tar -zxvf $pack_source/$ha_package -C $pack_source cd $pack_source/$ha_pack_dir sudo make TARGET=linux26 ARCH=x86_64 sudo make install #install keepalived cd $pack_source rm -rf $pack_source/$keep_pack_dir sudo tar -zxvf $pack_source/$keep_package -C $pack_source cd $pack_source/$keep_pack_dir sudo ./configure sudo make sudo make install #config sudo mkdir -p /etc/keepalived sudo mkdir -p /etc/haproxy yes|sudo cp -f $pack_source/$ha_pack_dir/examples/haproxy.init /etc/init.d/haproxy sudo chmod +x /etc/init.d/haproxy sudo ln -s /usr/local/sbin/haproxy /usr/sbin yes|sudo cp -f /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/keepalived yes|sudo cp -f /usr/local/etc/sysconfig/keepalived /etc/sysconfig/keepalived
yes|sudo cp -f /usr/local/check_haproxy.sh /etc/keepalived/check_haproxy.sh sudo chmod
+x /etc/init.d/keepalived sudo ln -s /usr/local/sbin/keepalied /usr/sbin #autostart sudo chkconfig --add keepalived sudo chkconfig keepalived on #log sudo mkdir -p /data/log/haproxy sudo mkdir -p /data/log/keepalived sudo touch /data/log/haproxy/haproxy.log sudo touch /data/log/keepalived/keepalived.log sudo chown -R data.deploy /devcloud selinux=`sudo getenforce` if [ $selinux == "Enforcing" ];then sudo chcon -R -t default_t /data sudo chcon -R -t var_log_t /data/log sudo chcon -R -t var_log_t /data/log/haproxy sudo chcon -R -t var_log_t /data/log/keepalived else echo "OK" fi sudo sed -i "s/SYSLOGD_OPTIONS=.*/SYSLOGD_OPTIONS=\"-c 2 -r -m 0\"/g " /etc/sysconfig/rsyslog sudo sed -i "s/KEEPALIVED_OPTIONS=.*/KEEPALIVED_OPTIONS=\"-D -d -S 0\"/g" /etc/sysconfig/keepalived sudo sed -i "s/\#\$ModLoad imudp/\$ModLoad imudp/g" /etc/rsyslog.conf sudo sed -i "s/\#\$UDPServerRun 514/\$UDPServerRun 514/g" /etc/rsyslog.conf sudo sed -i "s/cron.none/cron.none;local2.none;local0.none/g" /etc/rsyslog.conf sudo sed -i ‘$a local2.* /data/log/haproxy/haproxy.log ‘ /etc/rsyslog.conf sudo sed -i ‘$a local0.* /data/log/keepalived/keepalived.log‘ /etc/rsyslog.conf sudo service rsyslog restart exit 0

 

check_haproxy.sh

#!/bin/bash
A=`ps -C haproxy --no-header | wc -l`

if [ $A -eq 0 ];then
/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg
sleep 3s
if [ `ps -C haproxy --no-header | wc -l` -eq 0 ];then
sudo service keepalived stop
fi
fi

 

以上是关于haproxy+keepalived 部署脚本的主要内容,如果未能解决你的问题,请参考以下文章

haproxy+keepalived部署

部署haproxy+keepalived高可用负载均衡器

k8s高可用部署:keepalived + haproxy

HAProxy+Keepalived高可用负载均衡

Haproxy+Keepalived高可用环境部署梳理(主主和主从模式)

Haproxy+Keepalived高可用环境部署梳理(主主和主从模式)