Skip to content

Commit

Permalink
Merge pull request #14 from byjg/dokku
Browse files Browse the repository at this point in the history
Customize the Lookup Label
  • Loading branch information
byjg authored Aug 14, 2022
2 parents 3bea967 + 4ede25e commit b4af1e2
Show file tree
Hide file tree
Showing 16 changed files with 319 additions and 120 deletions.
12 changes: 12 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

tasks:
- init:
make

- command: |
virtualenv -p /usr/bin/python3 venv
source venv/bin/activate
pip install -r requirements.txt
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
]
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.14
FROM alpine:3.16

WORKDIR /scripts

Expand Down
100 changes: 48 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![Build Status](https://github.com/byjg/docker-easy-haproxy/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/byjg/docker-easy-haproxy/actions/workflows/build.yml)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/docker-easy-haproxy/)
[![GitHub license](https://img.shields.io/github/license/byjg/uri.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/uri.svg)](https://github.com/byjg/docker-easy-haproxy/releases/)

Service discovery for HAProxy.

This Docker image will create dynamically the `haproxy.cfg` based on the labels defined in docker containers or from
a simple Yaml instead docker
Expand All @@ -13,7 +18,6 @@ a simple Yaml instead docker
- Discover and setup haproxy redirect from Docker Tag
- Setup HAProxy CFG from a Yaml file.


## Basic Usage

The Easy HAProxy will create the `haproxy.cfg` automatically based on the containers or from a YAML provided.
Expand All @@ -24,7 +28,7 @@ The basic command line to run is:
docker run -d \
--name easy-haproxy-container \
-v /var/run/docker.sock:/var/run/docker.sock \
-e DISCOVER="swarm|docker|static" \
-e EASYHAPROXY_DISCOVER="swarm|docker|static" \
# + Environment Variables \
# + ports mapped to the host \
byjg/easy-haproxy
Expand All @@ -34,17 +38,17 @@ The mapping to `/var/run/docker.sock` is necessary to discover the docker contai

The environment variables will setup the HAProxy.

| Environment Variable | Description |
|----------------------|-------------------------------------------------------------------------------|
| DISCOVER | How `haproxy.cfg` will be created: `static`, `docker` or `swarm` |
| HAPROXY_USERNAME | (Optional) The HAProxy username to the statistics. Default: `admin` |
| HAPROXY_PASSWORD | The HAProxy password to the statistics. If not set disable stats. |
| HAPROXY_STATS_PORT | (Optional) The HAProxy port to the statistics. Default: `1936` |
| HAPROXY_CUSTOMERRORS | (Optional) If HAProxy will use custom HTML errors. true/false. Default: false |

| Environment Variable | Description |
|--------------------------|-------------------------------------------------------------------------------|
| EASYHAPROXY_DISCOVER | How `haproxy.cfg` will be created: `static`, `docker` or `swarm` |
| EASYHAPROXY_LABEL_PREFIX | (Optional) The key will search to match resources. Default: `easyhaproxy`. |
| HAPROXY_USERNAME | (Optional) The HAProxy username to the statistics. Default: `admin` |
| HAPROXY_PASSWORD | The HAProxy password to the statistics. If not set disable stats. |
| HAPROXY_STATS_PORT | (Optional) The HAProxy port to the statistics. Default: `1936` |
| HAPROXY_CUSTOMERRORS. | (Optional) If HAProxy will use custom HTML errors. true/false. Default: false |

The environment variable `EASYHAPROXY_DISCOVER` will define where is located your containers (see below more details):

The environment variable `DISCOVER` will define where is located your containers (see below more details):
- docker
- swarm
- static
Expand All @@ -53,7 +57,7 @@ The environment variable `DISCOVER` will define where is located your containers

Easy HAProxy can discover automatically the container services running in the same network of Docker or in a Docker Swarm cluster.

### DISCOVER: docker
### EASYHAPROXY_DISCOVER: docker

This method will use a regular docker installation to discover the containers and configure the HAProxy.

Expand All @@ -71,7 +75,7 @@ docker run --network easyhaproxy byjg/easyhaproxy
docker run --network easyhaproxy myimage
```

### DISCOVER: swarm
### EASYHAPROXY_DISCOVER: swarm

This method requires a functional Docker Swarm Cluster. The system will search for the labels in all containers on all
swarm nodes.
Expand All @@ -82,16 +86,15 @@ Important: easyhaproxy needs to be in the same network of the containers or othe

### Tags to be attached in the Docker Container (Swarm or Docker)

| Tag | Description | Example |
|------------------------------------|---------------------------------------------------------------------------------------------------------|--------------|
| easyhaproxy.definitions | A Comma delimited list with the definitions. Each name requires the definition of the parameters below. | service,service2 |
| easyhaproxy.mode.[definition] | (Optional) Is this `http` or `tcp` mode in HAProxy. (Defaults to http) | http |
| easyhaproxy.port.[definition] | (Optional) What is the port that the HAProxy will listen to. (Defaults to 80) | 80 |
| easyhaproxy.localport.[definition] | (Optional) What is the port that the container is listening. (Defaults to 80) | 8080 |
| easyhaproxy.host.[definition] | What is the host that the HAProxy will listen to. | somehost.com |
| easyhaproxy.redirect.[definition] | (Optional) Host redirects from connections in the port defined above. | foo.com--https://bla.com,bar.com--https://bar.org |
| easyhaproxy.sslcert.[definition] | (Optional) Cert PEM Base64 encoded. | |
| easyhaproxy.health-check.[definition] | (Optional) `ssl`, enable health check via SSL in `mode tcp` (Defaults to "empty") | |
| Tag | Description | Example |
|---------------------------------------|---------------------------------------------------------------------------------------------------------|--------------|
| easyhaproxy.[definition].mode | (Optional) Is this `http` or `tcp` mode in HAProxy. (Defaults to http) | http |
| easyhaproxy.[definition].port | (Optional) What is the port that the HAProxy will listen to. (Defaults to 80) | 80 |
| easyhaproxy.[definition].localport. | (Optional) What is the port that the container is listening. (Defaults to 80) | 8080 |
| easyhaproxy.[definition].host | What is the host that the HAProxy will listen to. | somehost.com |
| easyhaproxy.[definition].redirect | (Optional) Host redirects from connections in the port defined above. | foo.com--https://bla.com,bar.com--https://bar.org |
| easyhaproxy.[definition].sslcert | (Optional) Cert PEM Base64 encoded. | |
| easyhaproxy.[definition].health-check | (Optional) `ssl`, enable health check via SSL in `mode tcp` (Defaults to "empty") | |

### Defining the labels in Docker Swarm

Expand All @@ -102,62 +105,56 @@ services:
foo:
deploy:
labels:
easyhaproxy.definitions: "service1,service2"
easyhaproxy.my.host: "www.example.org"
easyhaproxy.my.localport: 8080
...
```


### Single Definition:
### Single Definition

```bash
docker run \
-l easyhaproxy.definitions=webapi \
-l easyhaproxy.port.webapi=80\
-l easyhaproxy.host.webapi=byjg.com.br \
-l easyhaproxy.webapi.port=80\
-l easyhaproxy.webapi.host=byjg.com.br \
....
```

### Multiples Definitions on the same container:
### Multiples Definitions on the same container

```bash
docker run \
-l easyhaproxy.definitions=express,admin \

-l easyhaproxy.port.express=80 \
-l easyhaproxy.localport.express=3000 \
-l easyhaproxy.host.express=express.byjg.com.br \
-l easyhaproxy.express.port=80 \
-l easyhaproxy.express.localport=3000 \
-l easyhaproxy.express.host=express.byjg.com.br \

-l easyhaproxy.port.admin=80 \
-l easyhaproxy.localport.admin=3001 \
-l easyhaproxy.host.admin=admin.byjg.com.br \
-l easyhaproxy.admin.port=80 \
-l easyhaproxy.admin.localport=3001 \
-l easyhaproxy.admin.host=admin.byjg.com.br \
.... \
some/myimage
```

### TLS passthrough

Used to pass on SSL-termination to a backend:
Used to pass on SSL-termination to a backend. Alternatively, you can enable health-check via SSL on the backend with the optional `health-check` label:

```bash
docker run \
-l easyhaproxy.defintions=example \
-l easyhaproxy.mode.example=tcp \
-l easyhaproxy.health-check.example=ssl \
-l easyhaproxy.port.example=443
-l easyhaproxy.example.mode=tcp \
-l easyhaproxy.example.health-check=ssl \
-l easyhaproxy.example.port=443
.... \
some/tcp-service
```

- enable health-check via SSL on the backend with the optional `health-check` label

### Redirect Example:
### Redirect Example

```bash
docker run \
-l easyhaproxy.redirect.<defintion>=www.byjg.com.br--http://byjg.com.br,byjg.com--http://byjg.com.br
-l easyhaproxy.[definition].redirect=www.byjg.com.br--http://byjg.com.br,byjg.com--http://byjg.com.br
```

## DISCOVER: static
## EASYHAPROXY_DISCOVER: static

This method expects a YAML file to setup the `haproxy.cfg`

Expand Down Expand Up @@ -206,7 +203,6 @@ docker run \
-d byjg/easy-haproxy
```


## Handling SSL

You can attach a valid SSL certificate to the request.
Expand Down Expand Up @@ -235,7 +231,7 @@ MIIEojCCA4qgAwIBAgIUegW2BimwuL4RzRZ2WYkHA6U5nkAwDQYJKoZIhvcNAQEL
cat single.pem | base64 -w0
```

3. Use this string to define the label `easyhaproxy.sslcert.[definition]`
3. Use this string to define the label `easyhaproxy.[definition].sslcert`

## Setting Custom Errors

Expand All @@ -244,10 +240,10 @@ where ERROR_NUMBER is the http error code (e.g. 503.http)

## Build

```

```bash
docker build -t byjg/easy-haproxy .
```


----
[Open source ByJG](http://opensource.byjg.com)
4 changes: 2 additions & 2 deletions assets/scripts/haproxy-reload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cd /scripts

RELOAD="true"

if [[ "$DISCOVER" == "static" ]]; then
if [[ "$EASYHAPROXY_DISCOVER" == "static" ]]; then
CONTROL_FILE="/etc/haproxy/haproxy.cfg"
touch ${CONTROL_FILE}
cp ${CONTROL_FILE} ${CONTROL_FILE}.old
Expand All @@ -15,7 +15,7 @@ else
mv ${CONTROL_FILE} ${CONTROL_FILE}.old
touch ${CONTROL_FILE}

if [[ "$DISCOVER" == "docker" ]]; then
if [[ "$EASYHAPROXY_DISCOVER" == "docker" ]]; then
CONTAINERS=$(docker ps -q)
LABEL_PATH=".Config.Labels"

Expand Down
2 changes: 2 additions & 0 deletions assets/scripts/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"port": os.getenv("HAPROXY_STATS_PORT") if os.getenv("HAPROXY_STATS_PORT") else "1936",
}

result["lookup_label"] = os.getenv("EASYHAPROXY_LABEL_PREFIX") if os.getenv("EASYHAPROXY_LABEL_PREFIX") else "easyhaproxy"

cfg = HaproxyConfigGenerator(result)
print(cfg.generate(lineList))

Expand Down
Loading

0 comments on commit b4af1e2

Please sign in to comment.