Reading from Docker and Swarm
This commit is contained in:
parent
b196905320
commit
b7ec691e8f
7 changed files with 63 additions and 14 deletions
|
|
@ -23,14 +23,13 @@ docker run \
|
|||
-e HAPROXY_USERNAME=admin \
|
||||
-e HAPROXY_PASSWORD=password \
|
||||
-e HAPROXY_STATS_PORT=1936 \
|
||||
-e REFRESH_STATE=1 \
|
||||
-v /var/run/docker.sock:/var/run/docker.sock \
|
||||
-d byjg/easy-haproxy
|
||||
```
|
||||
|
||||
The `DISCOVER` environment variable will define where is located your containers (see below more details):
|
||||
- swarm
|
||||
- awsecs
|
||||
- static (maps to /etc/haproxy/easyconfig.yml)
|
||||
|
||||
# Tags to defined:
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,48 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
echo "Reloading..."
|
||||
cd /scripts
|
||||
|
||||
RELOAD="true"
|
||||
|
||||
if [[ "$DISCOVER" == "static" ]]; then
|
||||
CONTROL_FILE="/etc/haproxy/haproxy.cfg"
|
||||
cp ${CONTROL_FILE} ${CONTROL_FILE}.old
|
||||
python3 static.py /etc/haproxy/easyconfig.yml > ${CONTROL_FILE}
|
||||
else
|
||||
CONTROL_FILE="/tmp/.docker_data"
|
||||
touch ${CONTROL_FILE}
|
||||
mv ${CONTROL_FILE} ${CONTROL_FILE}.old
|
||||
touch ${CONTROL_FILE}
|
||||
|
||||
if [[ "$DISCOVER" == "docker" ]]; then
|
||||
CONTAINERS=$(docker ps -q)
|
||||
LABEL_PATH=".Config.Labels"
|
||||
else
|
||||
CONTAINERS=$(docker node ps $(docker node ls -q) --format "{{ .Name }}" --filter desired-state=running | cut -d. -f1 | sort | uniq)
|
||||
LABEL_PATH=".Spec.Labels"
|
||||
fi
|
||||
|
||||
for container in ${CONTAINERS}; do
|
||||
docker inspect --format "{{ json $LABEL_PATH }}" ${container} | xargs -I % echo ${container}=% >> ${CONTROL_FILE}
|
||||
done
|
||||
|
||||
if cmp -s ${CONTROL_FILE} ${CONTROL_FILE}.old ; then
|
||||
RELOAD="false"
|
||||
else
|
||||
python3 swarm.py > /etc/haproxy/haproxy.cfg
|
||||
fi
|
||||
fi
|
||||
|
||||
if cmp -s ${CONTROL_FILE} ${CONTROL_FILE}.old ; then
|
||||
RELOAD="false"
|
||||
fi
|
||||
|
||||
if [[ ! -z "$1" ]]; then
|
||||
echo "Initial configuration"
|
||||
RELOAD="false"
|
||||
fi
|
||||
|
||||
if [[ "$RELOAD" == "true" ]]; then
|
||||
echo "Reloading..."
|
||||
/usr/local/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -x /var/run/haproxy.sock -sf $(cat /run/haproxy.pid) &
|
||||
fi
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
source /scripts/haproxy-reload.sh initial
|
||||
|
||||
/usr/local/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /var/run/haproxy.sock
|
||||
|
||||
while true; do
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ import sys
|
|||
from easymapping import HaproxyConfigGenerator
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
print("You need to pass the easyconfig.cfg path")
|
||||
print("You need to pass the easyconfig.yml path")
|
||||
exit(1)
|
||||
|
||||
|
||||
with open(sys.argv[1], 'r') as content_file:
|
||||
parsed = yaml.load(content_file.read())
|
||||
parsed = yaml.load(content_file.read(), Loader=yaml.FullLoader)
|
||||
|
||||
cfg = HaproxyConfigGenerator(parsed)
|
||||
print(cfg.generate())
|
||||
|
|
|
|||
13
swarm.py
13
swarm.py
|
|
@ -2,13 +2,19 @@ import os
|
|||
import json
|
||||
from easymapping import HaproxyConfigGenerator
|
||||
|
||||
path = os.path.dirname(os.path.realpath(__file__))
|
||||
with open(path + "/.docker_data", 'r') as content_file:
|
||||
# path = os.path.dirname(os.path.realpath(__file__))
|
||||
with open("/tmp/.docker_data", 'r') as content_file:
|
||||
lineList = content_file.readlines()
|
||||
|
||||
result = {
|
||||
"easymapping": []
|
||||
}
|
||||
if os.getenv("HAPROXY_USERNAME"):
|
||||
result["stats"] = {
|
||||
"username": os.getenv("HAPROXY_USERNAME"),
|
||||
"password": os.getenv("HAPROXY_PASSWORD"),
|
||||
"port": os.getenv("HAPROXY_STATS_PORT"),
|
||||
}
|
||||
|
||||
for line in lineList:
|
||||
line = line.strip()
|
||||
|
|
@ -33,7 +39,7 @@ for line in lineList:
|
|||
|
||||
redirect = d["com.byjg.easyhaproxy.redirect." + definition] if "com.byjg.easyhaproxy.redirect." + definition in d else ""
|
||||
for r in redirect.split(","):
|
||||
r_parts = r.split("=>")
|
||||
r_parts = r.split("--")
|
||||
data["redirect"][r_parts[0]] = r_parts[1]
|
||||
|
||||
result["easymapping"].append(data)
|
||||
|
|
@ -42,7 +48,6 @@ for line in lineList:
|
|||
cfg = HaproxyConfigGenerator(result)
|
||||
print(cfg.generate())
|
||||
|
||||
print(result)
|
||||
# print(jsonStr)
|
||||
|
||||
|
||||
|
|
|
|||
6
swarm.sh
6
swarm.sh
|
|
@ -5,13 +5,13 @@
|
|||
# -l com.byjg.easyhaproxy.port.http=80 \
|
||||
# -l com.byjg.easyhaproxy.port.localport=80 \
|
||||
# -l com.byjg.easyhaproxy.host.http=byjg.com.br \
|
||||
# -l com.byjg.easyhaproxy.redirect.http=byjg.com.br%http://www.byjg.com.br,byjg.com%http://www.byjg.com.br \
|
||||
# -l com.byjg.easyhaproxy.redirect.http=byjg.com.br--http://www.byjg.com.br,byjg.com--http://www.byjg.com.br \
|
||||
#-d byjg/php:7.3-fpm-nginx
|
||||
|
||||
|
||||
touch ./.docker_data ./docker_data_old
|
||||
touch /tmp/.docker_data /tmp/docker_data_old
|
||||
|
||||
mv ./.docker_data ./docker_data_old
|
||||
cp ./.docker_data ./docker_data_old
|
||||
|
||||
CONTAINERS=$(docker node ps $(docker node ls -q) --format "{{ .Name }}" --filter desired-state=running | cut -d. -f1 | sort | uniq)
|
||||
|
||||
|
|
|
|||
|
|
@ -63,6 +63,6 @@ backend srv_{{ host }}
|
|||
option forwardfor
|
||||
http-request set-header X-Forwarded-Port %[dst_port]
|
||||
http-request add-header X-Forwarded-Proto https if { ssl_fc }
|
||||
server srv {{ k }} check weight 1
|
||||
server srv {{ o["hosts"][k] }} check weight 1
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue