HAproxy softreload
/usr/lib/systemd/system/haproxy.service
[Unit] Description=HAProxy Load Balancer After=syslog.target network.target [Service] User=root ExecStart=/etc/init.d/haproxy start_systemd ExecStop=/etc/init.d/haproxy stop_systemd ExecReload=/etc/init.d/haproxy reload_systemd #Restart=on-abnormal Type=forking [Install] WantedBy=multi-user.target
/etc/init.d/haproxy
#!/bin/bash -x
function TEMPFILE() {
case $1 in
create) mktemp
;;
delete)
rm -fr $2
;;
esac
}
function SETUP_QDISC() {
# Set up the queuing discipline
tc qdisc add dev lo root handle 1: prio bands 4
tc qdisc add dev lo parent 1:1 handle 10: pfifo limit 1000
tc qdisc add dev lo parent 1:2 handle 20: pfifo limit 1000
tc qdisc add dev lo parent 1:3 handle 30: pfifo limit 1000
# Create a plug qdisc with 1 meg of buffer
nl-qdisc-add --dev=lo --parent=1:4 --id=40: plug --limit 1048576
# Release the plug
nl-qdisc-add --dev=lo --parent=1:4 --id=40: --update plug --release-indefinite
# Set up the filter, any packet marked with "1" will be
# directed to the plug
tc filter add dev lo protocol ip parent 1:0 prio 1 handle 1 fw classid 1:4
}
function SOFT_RELOAD_HA() {
/usr/local/sbin/haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf $(/bin/cat /var/run/haproxy.pid)
}
function RELOAD_IPTABLES() {
nl-qdisc-add --dev=lo --parent=1:4 --id=40: --update plug --buffer
SOFT_RELOAD_HA
nl-qdisc-add --dev=lo --parent=1:4 --id=40: --update plug --release-indefinite
}
function ADD_IP_PLUG_QUEUE() {
iptables -t mangle -I OUTPUT -p tcp -s $1 --syn -j MARK --set-mark 1
}
function KILL_HA_OLDPIDS() {
/bin/sleep 15
/bin/kill $(/bin/ps -ef | /bin/grep "haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf" | /bin/grep -v grep | /bin/awk '{print $2}')
}
function IPTABLES_HA_RELOAD() {
iptables -I INPUT -p tcp --dport 16000 --syn -j DROP
sleep 0.2
SOFT_RELOAD_HA
iptables -D INPUT -p tcp --dport 16000 --syn -j DROP
sleep 1
KILL_HA_OLDPIDS
}
function QDISC_HA_RELOAD() {
nl-qdisc-add --dev=lo --parent=1:4 --id=40: --update plug --buffer &> /dev/null
SOFT_RELOAD_HA
nl-qdisc-add --dev=lo --parent=1:4 --id=40: --update plug--release-indefinite &> /dev/null
sleep 0.100
# KILL_HA_OLDPIDS
}
if [ $# -ne 1 ]
then
/bin/echo erro
exit 1
else
case $1 in
start_systemd)
SETUP_QDISC
ADD_IP_PLUG_QUEUE <local interface ip>
ADD_IP_PLUG_QUEUE <local interface ip>
SOFT_RELOAD_HA
;;
reload_systemd)
QDISC_HA_RELOAD
;;
setup)
SETUP_QDISC
ADD_IP_PLUG_QUEUE <local interface ip>
ADD_IP_PLUG_QUEUE <local interface ip>
;;
stop_systemd)
killall haproxy
;;
start)
systemctl start haproxy
;;
stop)
systemctl stop haproxy
;;
restart)
systemctl restart haproxy
;;
reload)
systemctl restart haproxy
;;
status)
systemctl status haproxy
;;
esac
fi
Convert haproxy.cfg to CSV
#!/bin/bash
function AJUDA() {
echo "
How to use
$0 <haproxy.cfg>
HAPROXY reserverd words:
acl
backend
balance
bind
chroot
daemon
default_backend
defaults
frontend
global
group
http-check
http-request
listen
log
maxconn
mode
nbproc
option
pidfile
retries
server
stats
timeout
use_backend
user
"
}
function PARSER() {
export TAG=$1
case $1 in
frontend)
export CFG_FRONTEND=$2
;;
bind)
export CFG_FRONTEND_BIND=$(echo $2 | awk -F : '{ print $2} ')
;;
default_backend)
export CFG_FRONTEND_DEFAULT_BACK=$2
;;
backend)
export CFG_BACKEND=$2
;;
server)
if [ "$CFG_BACKEND_SERVER" == "0" ]
then
export CFG_BACKEND_SERVER="$2 $3"
else
export CFG_BACKEND_SERVER=$CFG_BACKEND_SERVER,"$2 $3"
fi
;;
esac
}
function CHECK_BEGIN() {
if [ "$CFG_FRONTEND" != "0" ] && [[ "$1" == "frontend" ]]
then
echo 1
else
echo 0
fi
}
function ZERO_ALL(){
export CFG_FRONTEND=0
export CFG_FRONTEND_BIND=0
export CFG_FRONTEND_DEFAULT_BACK=0
export CFG_BACKEND=0
export CFG_BACKEND_SERVER=0
}
export CFG_FRONTEND=0
export CFG_FRONTEND_BIND=0
export CFG_FRONTEND_DEFAULT_BACK=0
export CFG_BACKEND=0
export CFG_BACKEND_SERVER=0
export END=0
if [ $# -ne 1 ]
then
AJUDA
else
echo "FRONTEND;PORT;BACKENDNAME;BACKEND;SERVERS"
cat $1 | \
sed -e "s/^ *//g"| \
egrep -v "^#|^$" | \
while read -r line
do
if [ $(CHECK_BEGIN $line) -eq 0 ]
then
PARSER $line
else
echo $CFG_FRONTEND\;$CFG_FRONTEND_BIND\;$CFG_FRONTEND_DEFAULT_BACK\;$CFG_BACKEND\;$CFG_BACKEND_SERVER
ZERO_ALL
PARSER $line
fi
done
fi