Starting the Discovery Script
This commit is contained in:
parent
7f6f30b6f9
commit
d3687d7632
6 changed files with 126 additions and 34 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1,3 +1,4 @@
|
||||||
.idea
|
.idea
|
||||||
*~
|
*~
|
||||||
venv
|
venv
|
||||||
|
.docker_data
|
||||||
|
|
@ -5,6 +5,8 @@ RUN apk add --no-cache bash python3 py-yaml
|
||||||
COPY entrypoint.* /
|
COPY entrypoint.* /
|
||||||
COPY conf.d /etc/haproxy/conf.d
|
COPY conf.d /etc/haproxy/conf.d
|
||||||
COPY assets/errors-custom/* /etc/haproxy/errors-custom/
|
COPY assets/errors-custom/* /etc/haproxy/errors-custom/
|
||||||
|
RUN pip install --upgrade pip \
|
||||||
|
&& pip install -r requirements.txt
|
||||||
|
|
||||||
ENTRYPOINT [ "/entrypoint.sh" ]
|
ENTRYPOINT [ "/entrypoint.sh" ]
|
||||||
CMD ["haproxy", "-f", "/etc/haproxy/haproxy.cfg"]
|
CMD ["haproxy", "-f", "/etc/haproxy/haproxy.cfg"]
|
||||||
83
README.md
83
README.md
|
|
@ -5,53 +5,57 @@ This Docker image will create dynamically the `haproxy.cfg` based on very simple
|
||||||
# Features
|
# Features
|
||||||
|
|
||||||
- Enable or disable Stats on port 1936 with custom password
|
- Enable or disable Stats on port 1936 with custom password
|
||||||
- Simple mapping host => host:port
|
- Discover and setup haproxy from Docker Tag
|
||||||
- Simple redirect host => host:port
|
- Discover and setup haproxy redirect from Docker Tag
|
||||||
|
|
||||||
|
|
||||||
# Basic Usage
|
# Basic Usage
|
||||||
|
|
||||||
Create a yaml file in your machine called `easyconfig.cfg` and put the contents:
|
Docker run:
|
||||||
|
|
||||||
```yaml
|
|
||||||
stats:
|
|
||||||
username: admin
|
|
||||||
password: password
|
|
||||||
port: 1936 # Optional (default 1936)
|
|
||||||
|
|
||||||
customerrors: true # Optional (default false)
|
|
||||||
|
|
||||||
easymapping:
|
|
||||||
- port: 80
|
|
||||||
hosts:
|
|
||||||
host1.com.br: container:5000
|
|
||||||
host2.com.br: other:3000
|
|
||||||
redirect:
|
|
||||||
www.host1.com.br: http://host1.com.br
|
|
||||||
|
|
||||||
- port: 443
|
|
||||||
ssl_cert: /etc/easyconfig/mycert.pem
|
|
||||||
hosts:
|
|
||||||
host1.com.br: container:80
|
|
||||||
|
|
||||||
- port: 8080
|
|
||||||
hosts:
|
|
||||||
host3.com.br: domain:8181
|
|
||||||
```
|
|
||||||
|
|
||||||
Then run (remember to enable the proper ports):
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run \
|
docker run \
|
||||||
-v /path/to/local:/etc/easyconfig \
|
|
||||||
-p 80:80 \
|
-p 80:80 \
|
||||||
-p 8080:8080 \
|
-p 8080:8080 \
|
||||||
-p 1936:1936 \
|
-p 1936:1936 \
|
||||||
--name easy-haproxy-instance \
|
--name easy-haproxy-instance \
|
||||||
|
-e DISCOVER=swarm \
|
||||||
|
-e HAPROXY_USERNAME=admin \
|
||||||
|
-e HAPROXY_PASSWORD=password \
|
||||||
|
-e HAPROXY_STATS_PORT=1936 \
|
||||||
|
-e REFRESH_STATE=1 \
|
||||||
-d byjg/easy-haproxy
|
-d byjg/easy-haproxy
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The `DISCOVER` environment variable will define where is located your containers (see below more details):
|
||||||
|
- swarm
|
||||||
|
- awsecs
|
||||||
|
|
||||||
|
# Tags to defined:
|
||||||
|
|
||||||
|
| Tag | Description |
|
||||||
|
|--------------------------------------------|---------------------------------------------------------------------------------------------------------|
|
||||||
|
| com.byjg.easyhaproxy.definitions | A Comma delimited list with the definitions. Each name requires the definition of the parameters below. |
|
||||||
|
| com.byjg.easyhaproxy.port.<definition> | What is the port that the HAProxy will listen to. |
|
||||||
|
| com.byjg.easyhaproxy.host.<definition> | What is the host that the HAProxy will listen to. |
|
||||||
|
| com.byjg.easyhaproxy.redirect.<definition> | Host redirects from connections in the port defined above. |
|
||||||
|
| com.byjg.easyhaproxy.sslcert.<definition> | Cert PEM Base64 encoded. |
|
||||||
|
|
||||||
|
E.g.
|
||||||
|
|
||||||
|
```
|
||||||
|
docker run \
|
||||||
|
-l com.byjg.easyhaproxy.definitions=http \
|
||||||
|
-l com.byjg.easyhaproxy.port.http=80\
|
||||||
|
-l com.byjg.easyhaproxy.host.http=byjg.com.br \
|
||||||
|
....
|
||||||
|
```
|
||||||
|
|
||||||
|
## Redirect Example:
|
||||||
|
|
||||||
|
```text
|
||||||
|
www.byjg.com.br=>http://byjg.com.br,byjg.com=>http://byjg.com.br
|
||||||
|
```
|
||||||
|
|
||||||
# Mapping custom .cfg files
|
# Mapping custom .cfg files
|
||||||
|
|
||||||
|
|
@ -114,3 +118,18 @@ is the http error code (e.g. 503.http)
|
||||||
docker build -t byjg/easy-haproxy .
|
docker build -t byjg/easy-haproxy .
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Docker Swarm
|
||||||
|
|
||||||
|
|
||||||
|
# AWS
|
||||||
|
|
||||||
|
Easy HAProxy will try to get tasks from the tasks running in the CLUSTER.
|
||||||
|
It is important that easy haproxy run in the ECS Cluster and can connect to the containers.
|
||||||
|
|
||||||
|
|
||||||
|
You'll have to pass also the `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY` or setup a role in the instance in order and
|
||||||
|
the `ECS_CLUSTER` with the arn of the cluster
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
39
discover.py
Normal file
39
discover.py
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
|
path = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
with open(path + "/.docker_data", 'r') as content_file:
|
||||||
|
lineList = content_file.readlines()
|
||||||
|
|
||||||
|
result = dict()
|
||||||
|
|
||||||
|
for line in lineList:
|
||||||
|
line = line.strip()
|
||||||
|
i = line.find("=")
|
||||||
|
container = line[:i]
|
||||||
|
jsonStr = line[i+1:]
|
||||||
|
d = json.loads(jsonStr)
|
||||||
|
|
||||||
|
if "com.byjg.easyhaproxy.definitions" in d.keys():
|
||||||
|
definitions = d["com.byjg.easyhaproxy.definitions"].split(",")
|
||||||
|
|
||||||
|
for definition in definitions:
|
||||||
|
port = d["com.byjg.easyhaproxy.port." + definition] if "com.byjg.easyhaproxy.port." + definition in d else "80"
|
||||||
|
if port not in result:
|
||||||
|
result[port] = dict()
|
||||||
|
result[port]["hosts"] = dict()
|
||||||
|
result[port]["redirect"] = dict()
|
||||||
|
result[port]["ssl_cert"] = None
|
||||||
|
|
||||||
|
result[port]["hosts"][d["com.byjg.easyhaproxy.host." + definition] if "com.byjg.easyhaproxy.host." + definition in d else ""] = container + ":" + (d["com.byjg.easyhaproxy.localport." + definition] if "com.byjg.easyhaproxy.localport." + definition in d else "80")
|
||||||
|
|
||||||
|
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("=>")
|
||||||
|
result[port]["redirect"][r_parts[0]] = r_parts[1]
|
||||||
|
|
||||||
|
|
||||||
|
print(result)
|
||||||
|
# print(jsonStr)
|
||||||
|
|
||||||
|
|
||||||
30
discover.sh
Normal file
30
discover.sh
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
|
||||||
|
#docker run \
|
||||||
|
# -l com.byjg.easyhaproxy.definitions=http \
|
||||||
|
# -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 \
|
||||||
|
#-d byjg/php:7.3-fpm-nginx
|
||||||
|
|
||||||
|
|
||||||
|
touch ./.docker_data ./docker_data_old
|
||||||
|
|
||||||
|
mv ./.docker_data ./docker_data_old
|
||||||
|
|
||||||
|
CONTAINERS=$(docker node ps $(docker node ls -q) --format "{{ .Name }}" --filter desired-state=running | cut -d. -f1 | sort | uniq)
|
||||||
|
|
||||||
|
for container in $CONTAINERS; do
|
||||||
|
docker inspect --format "{{ json .Spec.Labels }}" $container | xargs -I % echo $container=% >> ./.docker_data
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
if cmp -s ./.docker_data ./.docker_data_old ; then
|
||||||
|
echo "Nothing changed"
|
||||||
|
else
|
||||||
|
echo "Something changed"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# byjg_site={"com.byjg.easyhaproxy.definition":"http","com.byjg.easyhaproxy.host.http":"byjg.com.br","com.byjg.easyhaproxy.port.http":"80","com.byjg.easyhaproxy.redirect.http":"byjg.com.br%http://www.byjg.com.br,byjg.com%http://www.byjg.com.br","maintainer":"Joao Gilberto Magalhaes"}
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
pyyaml
|
pyyaml
|
||||||
|
docker
|
||||||
Loading…
Add table
Add a link
Reference in a new issue