Skip to content

Instantly share code, notes, and snippets.

@dbaba
Last active December 2, 2015 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbaba/743c8babffa69e805678 to your computer and use it in GitHub Desktop.
Save dbaba/743c8babffa69e805678 to your computer and use it in GitHub Desktop.
A bash script for running HAProxy as a Docker container with hot reloading support
#!/usr/bin/env bash
# A bash script for running HAProxy as a Docker container with hot reloading support
#
# For example:
# $ docker run --name lb -p 80:8080 -p 443:8443 \
# --log-driver=json-file --log-opt max-size=100M \
# --log-opt max-file=1 \
# -v $(pwd):/usr/local/etc/haproxy -tid \
# haproxy:1.6 /usr/local/etc/haproxy/start_haproxy.sh
echo "$(date) : Starting HA Proxy..."
HAPROXY=/usr/local/etc/haproxy
# Restart haproxy every ${SLEEP_SEC} seconds (5 minutes by default)
SLEEP_SEC=${SLEEP_SEC:-300}
# Run HAProxy as a daemon
rm -f ${HAPROXY}/haproxy.pid
haproxy -D -f ${HAPROXY}/haproxy.cfg -p ${HAPROXY}/haproxy.pid
RET=$?
if [ "${RET}" == "0" ]; then
cp -f ${HAPROXY}/haproxy.cfg ${HAPROXY}/haproxy.cfg.bak
else
rm -f ${HAPROXY}/haproxy.cfg.bak
fi
echo "$(date) : Starting Configuration File Hot Reloading..."
while [ 1 ]; do
diff -q ${HAPROXY}/haproxy.cfg ${HAPROXY}/haproxy.cfg.bak >/dev/null 2>&1
RET=$?
if [ "${RET}" != "0" ]; then
haproxy -D -f ${HAPROXY}/haproxy.cfg -p ${HAPROXY}/haproxy.pid -sf $(cat ${HAPROXY}/haproxy.pid)
RET=$?
if [ "${RET}" == "0" ]; then
echo "$(date) : Hot Reloading Successful"
cp -f ${HAPROXY}/haproxy.cfg ${HAPROXY}/haproxy.cfg.bak
else
echo "$(date) : Hot Reloading Failed. Correct the cfg file and save it."
fi
fi
sleep ${SLEEP_SEC}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment