commit 80b49c6185e675ce99ee6a2ca0fa9084e7b5b7c0 Author: Joao Gilberto Magalhaes Date: Sun Sep 16 21:03:17 2018 -0500 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3023c68 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +*~ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..dd78ed7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM haproxy:1.8-alpine + +RUN apk add --no-cache bash + +COPY entrypoint.sh / + +ENTRYPOINT [ "/entrypoint.sh" ] +CMD ["haproxy", "-f", "/usr/local/etc/haproxy/haproxy.cfg"] \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..92dcb69 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Joao Gilberto Magalhães + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d16b01c --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# Easy HAProxy + +This Docker image will create dynamically the `haproxy.cfg` based on the arguments defined in the command line. + +## Features + +- Enable or disable Stats on port 1936 with custom password +- Add a mapping from a host to another + +## Usages + +The most common use is to use on Docker Swarm environment when you don't want to expose +the container ports and use a Load Balance to deliver to this component in the same +overlay network + +## Basic Usage + +You have to define 3 variables (LB_STATS_USER, LB_STATS_PASS, LB_HOSTS). + +Example: +- HAProxy with statistics and password "mypassowrd" for the user "admin" +- Domain `my.domain.com` routing to container/host `container1` on port 8080 +- Domain `other.domain.com` routing to container/host `other` on port 7000 + + +```bash +docker run \ + -e LB_STATS_USER=admin \ + -e LB_STATS_PASS=mypassword \ + -e LB_HOSTS="my.domain.com container1:8080 other.domain.com other:7000" \ + --name easy-haproxy-instance \ + -t -d byjg/easy-haproxy +``` + + +## Build + +``` +docker build -t byjg/easy-haproxy . +``` diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..b0e66f8 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,118 @@ +#!/bin/bash +set -e + +echo " ______ _ _ _____ " +echo "| ____| | | | | /\ | __ \ " +echo "| |__ __ _ ___ _ _ | |__| | / \ | |__) | __ _____ ___ _ " +echo "| __| / _\` / __| | | | | __ | / /\ \ | ___/ '__/ _ \ \/ / | | |" +echo "| |___| (_| \__ \ |_| | | | | |/ ____ \| | | | | (_) > <| |_| |" +echo "|______\__,_|___/\__, | |_| |_/_/ \_\_| |_| \___/_/\_\\__, |" +echo " __/ | __/ |" +echo " |___/ |___/ " +echo "" + +defaults() { +echo " +defaults + timeout connect 3s + timeout client 10s + timeout server 10m + +global + log 127.0.0.1 local0 notice + maxconn 2000 +" +} + +stats() { +echo " +frontend stats + bind *:1936 + mode http + stats enable + stats hide-version + stats realm Haproxy\ Statistics + stats uri / + stats auth $1:$2 +# acl is_proxystats hdr_dom(host) -i some.host.com +# default_backend srv_stats + use_backend srv_stats if is_proxystats + +backend srv_stats + mode http + server Local 127.0.0.1:1936 +" +} + +frontend() { + echo " +frontend http_in + bind *:80 + mode http +" + + COUNT=1 + for FEACL in `echo $1` + do + echo " acl is_rule_$COUNT hdr_dom(host) -i -m end $FEACL" + COUNT=$((COUNT + 1)) + done + echo + COUNT=1 + for FEACL in `echo $1` + do + echo " use_backend srv_80_$COUNT if is_rule_$COUNT" + COUNT=$((COUNT + 1)) + done +} + +backend () { + COUNT=1 + for BESRV in `echo $1` + do + echo " +backend srv_80_$COUNT + balance roundrobin + mode http + option forwardfor + http-request set-header X-Forwarded-Port %[dst_port] + http-request add-header X-Forwarded-Proto https if { ssl_fc } +" + echo " server srv $BESRV check weight 1" + COUNT=$((COUNT + 1)) + done +} + + +HAPROXY_CFG="/usr/local/etc/haproxy/haproxy.cfg" +FRONTEND="" +BACKEND="" +TURN="FB" +for LB_HOST in `echo ${LB_HOSTS}`; +do + if [ "$TURN" == "FB" ] + then + FRONTEND="$FRONTEND $LB_HOST" + TURN="BE" + continue + fi + + if [ "$TURN" == "BE" ] + then + BACKEND="$BACKEND $LB_HOST" + TURN="FB" + continue + fi +done + +# Write +defaults > $HAPROXY_CFG +if [ ! -z "${LB_STATS_USER}" ] +then + stats ${LB_STATS_USER} ${LB_STATS_PASS} >> $HAPROXY_CFG +fi +frontend "$FRONTEND" >> $HAPROXY_CFG +backend "$BACKEND" >> $HAPROXY_CFG + +/docker-entrypoint.sh "$@" +