Compare commits

..
29 Commits
Author SHA1 Message Date
ElevenNotes 307e7101f8 add TLS defaults 2024-01-23 22:52:36 +01:00
ElevenNotes 6752ebee1a 0.1.10 2024-01-23 22:08:09 +01:00
ElevenNotes 3c2ab08ba9 0.1.9 2024-01-23 22:07:09 +01:00
ElevenNotes 498c0988e5 0.1.10 2024-01-23 22:01:53 +01:00
ElevenNotes 49801cd521 0.1.10 2024-01-23 22:01:10 +01:00
ElevenNotes 66b5bab4ae add TLS auth 2024-01-23 21:58:09 +01:00
ElevenNotes 070f45fae5 add TLS authentication 2024-01-23 15:34:43 +01:00
ElevenNotes 085de10ca1 add debug info 2024-01-23 09:14:35 +01:00
ElevenNotes 85575ce073 add skip info 2024-01-22 17:09:00 +01:00
ElevenNotes b754494a98 debug 2024-01-22 17:05:18 +01:00
ElevenNotes 6ac866ef5e match all TXT 2024-01-22 16:53:33 +01:00
ElevenNotes 426826532d obfuscate key in logs 2024-01-22 16:50:56 +01:00
ElevenNotes 82b132c68e add debug 2024-01-22 16:42:00 +01:00
ElevenNotes 3f89d3ddd9 add debug 2024-01-22 16:33:25 +01:00
ElevenNotes ff8e1d9cd9 0.1.9 > latest 2024-01-22 15:35:11 +01:00
ElevenNotes 2d5ef49071 add dig 2024-01-22 15:33:28 +01:00
ElevenNotes 76a9a7261f log container layout 2024-01-22 15:06:34 +01:00
ElevenNotes 7e67e971ae add dig for on change checking rfc2136 2024-01-22 15:01:37 +01:00
ElevenNotes fad60b7019 latest 2024-01-21 09:08:56 +01:00
ElevenNotes 2eadac07bb add container info 2024-01-21 09:03:17 +01:00
ElevenNotes e129945fde true JSON with elevenLogJSON 2024-01-18 11:42:57 +01:00
ElevenNotes 2d9bfafffa add debug option 2024-01-18 09:37:47 +01:00
ElevenNotes 823151dc98 fix ? on object 2024-01-18 09:28:42 +01:00
ElevenNotes 7bd574c3a3 latest 2024-01-18 09:12:29 +01:00
ElevenNotes e7c3a83ae2 add correct arch and build 2024-01-18 09:09:18 +01:00
ElevenNotes 24a2e20641 fix URL 2024-01-18 08:56:56 +01:00
ElevenNotes 7404380279 typo 😅 2024-01-16 08:35:51 +01:00
ElevenNotes d0b98b201d add rfc2136 update capabilities 2024-01-16 07:33:49 +01:00
ElevenNotes 33ac5c9e4c multi arch 2024-01-02 01:48:55 +01:00
8 changed files with 290 additions and 54 deletions
+63 -9
View File
@@ -1,13 +1,17 @@
# Alpine :: Traefik Labels
![size](https://img.shields.io/docker/image-size/11notes/traefik-labels/latest?color=0eb305) ![version](https://img.shields.io/docker/v/11notes/traefik-labels?color=eb7a09) ![pulls](https://img.shields.io/docker/pulls/11notes/traefik-labels?color=2b75d6) ![activity](https://img.shields.io/github/commit-activity/m/11notes/docker-traefik-labels?color=c91cb8) ![commit-last](https://img.shields.io/github/last-commit/11notes/docker-traefik-labels?color=c91cb8)
![size](https://img.shields.io/docker/image-size/11notes/traefik-labels/amd64-0.1.10?color=0eb305) ![version](https://img.shields.io/docker/v/11notes/traefik-labels/amd64-0.1.10?color=eb7a09) ![pulls](https://img.shields.io/docker/pulls/11notes/traefik-labels?color=2b75d6) ![activity](https://img.shields.io/github/commit-activity/m/11notes/docker-traefik-labels?color=c91cb8) ![commit-last](https://img.shields.io/github/last-commit/11notes/docker-traefik-labels?color=c91cb8)
Run Traefik Labels based on Alpine Linux. Small, lightweight, secure and fast 🏔️
What can I do with this? Simply put: It will export any traefik labels on a container on the same host as this image runs to a Redis instance. This makes it possible for a centralized Traefik load balancer to update endpoints dynamically by utilizing the docker labels, just like you would on a local installation of Traefik with labels. It is meant as an alternative and simple way to proxy requests from a Traefik load balanacer to multiple docker nodes running in different networks and locations.
## Description
What can I do with this? Simply put: It will export any traefik labels on a container on the same host as this image runs to a Redis instance. This makes it possible for a centralized Traefik load balancer to update endpoints dynamically by utilizing the docker labels, just like you would on a local installation of Traefik with labels. It is meant as an alternative and simple way to proxy requests from a Traefik load balancer to multiple docker nodes running in different networks and locations. The image has the ability to use nsupdate with labels too, so that not only you can add a dynamic proxy to your Traefik, but add DNS entries to your internal or external NS servers. It uses split DNS as WAN and LAN in your labels, so you can update external as well as internal DNS entries on the fly. If a container is removed the image will automatically reverse any `nsupdate update add` to `nsupdate update delete` so entrie are removed too.
In order to use this image, you need to setup Traefik with a Redis provider and then point this image via REDIS_URL to the same Redis instance. Each entry will have an expire timer set in Redis, so that if a container is removed by a server crashing, Redis will automatically remove stale entries as well. Entries are refreshed every 300 seconds or on all docker container events (create, run, kill, stop, restart, ...).
In order to use this image, you need to setup Traefik with a Redis provider and then point this image via REDIS_URL to the same Redis instance. Each entry will have an expire timer set in Redis, so that if a container is removed by a server crashing, Redis will automatically remove stale entries as well. Entries are refreshed every 300 seconds or on all docker container events (create, run, kill, stop, restart, ...). As for nsupdate, you need to setup tsig authentication in your NS servers and add the keys to the zones you want to be able to update, you can restrict the keys by using update-policy if you use BIND.
This image provides the ability to call a webhook for each container for each event or poll after the data was updates in Redis.
This image provides the ability to call a webhook for each container for each event or poll after the data was updates in Redis and or nsupdate.
## Volumes
* **/labels/ssl** - Directory of ssl certificates for TLS<sup>1</sup>
## Run
```shell
@@ -26,9 +30,18 @@ docker run --name traefik-labels \
-d 11notes/traefik-labels:[tag]
```
This is a demo webserver that will start on :8080, all the traefik labels will be exportet to the Redis instance. They follow the exact same [syntax](https://doc.traefik.io/traefik/routing/providers/kv/) as for normal Redis and Traefik, just as labels.
```shell
docker run --name demo \
docker run --name traefik-labels \
-v .../ssl:/labels/ssl \
-e LABELS_REDIS_URL="rediss://foo:bar@10.127.198.254:6379/0" \
-e LABELS_WEBHOOK="https://domain.com/traefik/labels" \
-e LABELS_WEBHOOK_AUTH_BASIC="foo:bar" \
-d 11notes/traefik-labels:[tag]
```
## Examples
```shell
docker run --name traefik-demo \
-p 8080:8080 \
-l "traefik/http/routers/demo.domain.com/service=demo.domain.com" \
-l "traefik/http/routers/demo.domain.com/rule=Host(`demo.domain.com`)" \
@@ -38,6 +51,24 @@ docker run --name demo \
-d 11notes/nginx:stable
```
```shell
docker run --name traefik-rfc2136-demo \
-p 8080:8080 \
-l "traefik/http/routers/demo.domain.com/service=demo.domain.com" \
-l "traefik/http/routers/demo.domain.com/rule=Host(`demo.domain.com`)" \
-l "traefik/http/routers/demo.domain.com/tls=true" \
-l "traefik/http/routers/demo.domain.com/entrypoints=https" \
-l "traefik/http/services/demo.domain.com/loadbalancer/servers/0/url=http://fqdn-of-docker-node:8080" \
-l "rfc2136/WAN/server=ns.domain.com" \
-l "rfc2136/WAN/key=algo:name:secret" \
-l "rfc2136/WAN/nsupdate=update add foo.domain.com 300 A 175.12.41.11" \
-l "rfc2136/WAN/nsupdate=update add foo.domain.com 300 TXT \"hello from traefik-labels!\"" \
-l "rfc2136/LAN/server=ns.domain.local" \
-l "rfc2136/LAN/key=algo:name:secret" \
-l "rfc2136/LAN/nsupdate=update add foo.domain.local 300 A 192.168.12.54" \
-d 11notes/nginx:stable
```
## Defaults
| Parameter | Value | Description |
| --- | --- | --- |
@@ -45,20 +76,29 @@ docker run --name demo \
| `uid` | 1000 | user id 1000 |
| `gid` | 1000 | group id 1000 |
| `home` | /labels | home directory of user docker |
| `api` | https://${IP}:5000 | HTTPS endpoint of Docker registry |
| `ca.crt` | /labels/ssl/ca.crt | Certificate of CA for TLS<sup>1</sup> |
| `labels.crt` | /labels/ssl/labels.crt | Certificate of client for TLS<sup>1</sup> |
| `labels.key` | /labels/ssl/labels.key | Private key of client for TLS<sup>1</sup> |
## Environment
| Parameter | Value | Default |
| --- | --- | --- |
| `TZ` | [Time Zone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) | |
| `DEBUG` | Show debug information | |
| `LABELS_REDIS_URL` | the redis URL to connect, use rediss:// for SSL | rediss://localhost:6379/0 |
| `LABELS_INTERVAL` | in what interval container information is pulled in seconds | 300 |
| `LABELS_TIMEOUT` | how many seconds after an interval the keys should stay till they expire in seconds | 30 |
| `LABELS_WEBHOOK` | URL to call on each event or poll for each container | |
| `LABELS_WEBHOOK_AUTH_BASIC` | Basic authentication to use in the form of "username:password" for the webhook | |
| `LABELS_RFC2136_ONLY_UPDATE_ON_CHANGE` | Only update DNS entries if they are new or changed (will use dig on each call to the set server) | |
| `LABELS_DOCKER_IP` | Used for TLS<sup>1</sup> authentication against dockerd | localhost |
| `LABELS_DOCKER_PORT` | Used for TLS<sup>1</sup> authentication against dockerd | 2376 |
## Parent image
* [11notes/node:stable](https://github.com/11notes/docker-node)
* [11notes/node:stable](https://hub.docker.com/r/11notes/node)
## Built with and thanks to
## Built with (thanks to)
* [npm::redis](https://www.npmjs.com/package/redis)
* [npm::dockerode](https://www.npmjs.com/package/dockerode)
* [nodejs](https://nodejs.org/en)
@@ -67,4 +107,18 @@ docker run --name demo \
## Tips
* Only use rootless container runtime (podman, rootless docker)
* Don't bind to ports < 1024 (requires root), use NAT/reverse proxy (haproxy, traefik, nginx)
* Do not access docker.sock as root
* Do not access docker.sock as root
* Use TLS if you can't access docker.sock as non-root
## Disclaimers
* <sup>1</sup> For TLS to work you need proper certificates in place for your dockerd and your clients. The CN in the certificate needs to match the FQDN or IP you have set on the docker node, you can set multiple by using SAN. See an example of a daemon.json configuration to enable TLS.
```json
{
"tls": true,
"tlsverify": true,
"tlscacert": "/etc/docker/ca.crt",
"tlscert": "/etc/docker/server.crt",
"tlskey": "/etc/docker/server.key",
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2376"]
}
```
+14 -1
View File
@@ -1,3 +1,11 @@
# :: Util
FROM alpine as util
RUN set -ex; \
apk add --no-cache \
git; \
git clone https://github.com/11notes/util.git;
# :: Header
FROM 11notes/node:stable
ENV APP_ROOT=/labels
@@ -7,7 +15,12 @@
# :: prepare image
RUN set -ex; \
mkdir -p ${APP_ROOT};
mkdir -p ${APP_ROOT}/lib; \
apk --no-cache add \
bind-tools; \
apk --no-cache upgrade;
COPY --from=util /util/node/util.js /labels/lib
# :: install
RUN set -ex; \
+14 -1
View File
@@ -1,3 +1,11 @@
# :: Util
FROM alpine as util
RUN set -ex; \
apk add --no-cache \
git; \
git clone https://github.com/11notes/util.git;
# :: Builder
FROM alpine AS qemu
ENV QEMU_URL https://github.com/balena-io/qemu/releases/download/v3.0.0%2Bresin/qemu-3.0.0+resin-aarch64.tar.gz
@@ -13,7 +21,12 @@
# :: prepare image
RUN set -ex; \
mkdir -p ${APP_ROOT};
mkdir -p ${APP_ROOT}/lib; \
apk --no-cache add \
bind-tools; \
apk --no-cache upgrade;
COPY --from=util /util/node/util.js /labels/lib
# :: install
RUN set -ex; \
+4
View File
@@ -0,0 +1,4 @@
#!/bin/bash
curl -Lo manifest-tool https://github.com/estesp/manifest-tool/releases/download/v0.9.0/manifest-tool-linux-amd64
chmod +x manifest-tool
./manifest-tool push from-spec multi-arch-manifest.yaml
+11
View File
@@ -0,0 +1,11 @@
image: 11notes/traefik-labels:latest
manifests:
- image: 11notes/traefik-labels:amd64-0.1.10
platform:
architecture: amd64
os: linux
- image: 11notes/traefik-labels:arm64v8-0.1.10
platform:
architecture: arm64
os: linux
variant: v8
+27
View File
@@ -0,0 +1,27 @@
const { spawn } = require('node:child_process');
const { elevenLogJSON } = require('/labels/lib/util.js');
exports.dig = async(resolver, type, record) => {
return(new Promise((resolve, reject) => {
const dig = spawn('/usr/bin/dig', ['+short', '+answer', type, record, `@${resolver}`]);
const io = {stdout:'', stderr:''};
dig.stderr.on('data', data => {io.stderr += data.toString()});
dig.stdout.on('data', data => {io.stdout += (data.toString()).replace(/[\r\n]*$/ig, '')});
dig.on('error', error => {reject(error)});
dig.on('close', code =>{
elevenLogJSON('debug', {method:'dig()', params:{resolver:resolver, type:type, record:record}, io:io});
switch(true){
case /no servers could be reached/ig.test(io.stdout): io.stderr += io.stdout; break;
}
if(code === 0){
if(io.stderr.length > 0){
reject(io.stderr);
}else{
resolve(io.stdout);
}
}else{
reject(io.stderr);
}
});
}));
}
+124 -43
View File
@@ -1,12 +1,18 @@
process.once('SIGTERM', () => process.exit(0));
process.once('SIGINT', () => process.exit(0));
const fs = require('fs');
const Docker = require('dockerode');
const redis = require('redis');
const { nsupdate } = require('./nsupdate');
const { dig } = require('./dig');
const { elevenLogJSON } = require('/labels/lib/util.js');
const ERROR = 1;
const POLL_INTERVAL = parseInt(process.env?.LABELS_INTERVAL || 300);
const POLL_TIMEOUT = parseInt(process.env?.LABELS_TIMEOUT|| 30);
const ENV_REDIS_INTERVAL = parseInt(process.env?.LABELS_INTERVAL || 300);
const ENV_REDIS_TIMEOUT = parseInt(process.env?.LABELS_TIMEOUT|| 30);
const ENV_LABELS_WEBHOOK = process.env?.LABELS_WEBHOOK;
const ENV_LABELS_WEBHOOK_AUTH_BASIC = process.env?.LABELS_WEBHOOK_AUTH_BASIC;
const ENV_LABELS_RFC2136_ONLY_UPDATE_ON_CHANGE = process.env?.LABELS_RFC2136_ONLY_UPDATE_ON_CHANGE || false;
class Labels{
#docker;
@@ -17,25 +23,33 @@ class Labels{
};
constructor(){
this.#docker = new Docker({socketPath:'/run/docker.sock'});
if(undefined !== process.env.LABELS_WEBHOOK_AUTH_BASIC){
this.#webhook.headers['Authorization'] = 'Basic ' + Buffer.from(process.env.LABELS_WEBHOOK_AUTH_BASIC).toString('base64')
}
}
switch(true){
case fs.existsSync('/run/docker.sock'):
elevenLogJSON('info', 'connect to Docker socket');
this.#docker = new Docker({socketPath:'/run/docker.sock'});
break;
#log(message, type){
console.log(JSON.stringify({
time:new Date().toISOString(),
type:(type === ERROR) ? 'ERROR' : 'INFO',
message:message,
}));
case fs.existsSync(`${process.env?.APP_ROOT}/ssl/ca.crt`):
elevenLogJSON('info', 'connect to Docker API via TLS verify');
this.#docker = new Docker({
protocol:'https',
host:process.env?.LABELS_DOCKER_IP || 'localhost',
port: process.env.LABELS_DOCKER_PORT || 2376,
ca:fs.readFileSync(`${process.env?.APP_ROOT}/ssl/ca.crt`),
cert:fs.readFileSync(`${process.env?.APP_ROOT}/ssl/labels.crt`),
key:fs.readFileSync(`${process.env?.APP_ROOT}/ssl/labels.key`)
});
break;
default:
elevenLogJSON('error', 'No docker API available, add /run/docker.sock (non-root) or use TLS verify authentication!');
}
if(ENV_LABELS_WEBHOOK_AUTH_BASIC){
this.#webhook.headers['Authorization'] = 'Basic ' + Buffer.from(ENV_LABELS_WEBHOOK_AUTH_BASIC).toString('base64')
}
}
async watch(){
if(undefined !== process.env.LABELS_WEBHOOK){
this.#log(`using webhook ${process.env.LABELS_WEBHOOK}`);
}
this.#redis = await redis.createClient({
url:process.env.LABELS_REDIS_URL,
pingInterval:30000,
@@ -46,7 +60,6 @@ class Labels{
this.#redis.connect();
this.#redis.on('ready', ()=>{
this.#log('successfully connected to redis');
(async() => {
await this.dockerPoll();
})();
@@ -54,18 +67,18 @@ class Labels{
});
this.#redis.on('error', error =>{
this.#log(error, ERROR);
elevenLogJSON('error', error);
});
setInterval(async() => {
await this.dockerPoll();
}, POLL_INTERVAL*1000);
}, ENV_REDIS_INTERVAL*1000);
}
dockerEvents(){
this.#docker.getEvents({}, (error, data) => {
if(error){
this.#log(error, ERROR);
elevenLogJSON('error', error);
}else{
data.on('data', async(chunk) => {
const event = JSON.parse(chunk.toString('utf8'));
@@ -89,48 +102,99 @@ class Labels{
}
});
}catch(e){
this.#log(e, ERROR);
elevenLogJSON('error', e);
}finally{
this.#poll = false;
}
}
}
dockerInspect(id, status = null){
async dockerInspect(id, status = null){
return(new Promise((resolve, reject) => {
const container = this.#docker.getContainer(id);
container.inspect(async(error, data) => {
if(!error){
const update = (/start|poll/i.test(status)) ? true : false;
const webHook = {event:status, labels:{}};
let log = false;
const container = {
name:(data?.Name || data?.id).replace(/^\//i, ''),
event:status,
labels:{
traefik:[],
rfc2136:[],
},
};
const rfc2136 = {
WAN:{server:'', key:'', commands:[]},
LAN:{server:'', key:'', commands:[]},
}
elevenLogJSON('info', {container:container.name, event:status, method:'inspect'});
for(const label in data?.Config?.Labels){
if(/traefik\//i.test(label)){
if(!log){
this.#log(`inspect container {${(data?.Name || data?.id).replace(/^\//i, '')}}${(
(null === status) ? '' : ` event[${status}]`
)} traefik labels`);
log = true;
switch(true){
case /traefik\//i.test(label):
if(update){
await this.#redis.set(label, data.Config.Labels[label], {EX:ENV_REDIS_INTERVAL + ENV_REDIS_TIMEOUT});
}else{
await this.#redis.del(label);
}
container.labels.traefik[label] = data.Config.Labels[label];
break;
case /rfc2136\//i.test(label):
container.labels.rfc2136[label] = data.Config.Labels[label];
const type = ((label.match(/rfc2136\/WAN\//i)) ? 'WAN' : 'LAN');
switch(true){
case /rfc2136\/\S+\/server/i.test(label):
rfc2136[type].server = data.Config.Labels[label];
break;
case /rfc2136\/\S+\/key/i.test(label):
rfc2136[type].key = data.Config.Labels[label];
break;
default:
if(!update){
data.Config.Labels[label] = data.Config.Labels[label].replace(/update add/i, 'update delete');
}
rfc2136[type].commands.push(data.Config.Labels[label]);
}
break;
}
}
for(const type in rfc2136){
if(rfc2136[type].commands.length > 0 && rfc2136[type].server && rfc2136[type].key){
if(ENV_LABELS_RFC2136_ONLY_UPDATE_ON_CHANGE){
for(let i=0; i<rfc2136[type].commands.length; i++){
if(await this.rfc2136KnownRecord(rfc2136[type].server, rfc2136[type].commands[i])){
rfc2136[type].commands.splice(i, 1);
}
}
}
if(undefined !== process.env.LABELS_WEBHOOK){
webHook.labels[label] = data?.Config?.Labels[label];
}
if(update){
await this.#redis.set(label, data?.Config?.Labels[label], {EX:POLL_INTERVAL + POLL_TIMEOUT});
}else{
await this.#redis.del(label);
try{
if(rfc2136[type].commands.length > 0){
elevenLogJSON('info', {container:container.name, event:status, method:`nsupdate ${rfc2136[type].server}`});
await nsupdate(rfc2136[type].server, rfc2136[type].key, rfc2136[type].commands);
}else{
if(ENV_LABELS_RFC2136_ONLY_UPDATE_ON_CHANGE){
elevenLogJSON('info', {container:container.name, event:status, method:`nsupdate ${rfc2136[type].server} skipped due to same record data`});
}
}
}catch(e){
elevenLogJSON('error', e);
}
}
}
if(undefined !== process.env.LABELS_WEBHOOK){
if(ENV_LABELS_WEBHOOK){
try{
await fetch(process.env.LABELS_WEBHOOK, {method:(
await fetch(ENV_LABELS_WEBHOOK, {method:(
(update) ? 'PUT' : 'DELETE'
), body:JSON.stringify(webHook), headers:this.#webhook.headers, signal:AbortSignal.timeout(2500)});
), body:JSON.stringify(container), headers:this.#webhook.headers, signal:AbortSignal.timeout(2500)});
}catch(e){
this.#log(e, ERROR);
elevenLogJSON('error', {method:'fetch(webhook)', error:e});
}
}
@@ -139,6 +203,23 @@ class Labels{
});
}));
}
async rfc2136KnownRecord(server, nsupdate){
const matches = nsupdate.match(/update add (\S+) \d+ (\S+) (\S+)/i);
if(matches && matches.length >= 4){
try{
const record = await dig(server, matches[2], matches[1]);
const match = (
(record.match(new RegExp(matches[3], 'ig'))) ? true : false
);
elevenLogJSON('debug', {method:'rfc2136KnownRecord()', params:{server:server, nsupdate:nsupdate}, match:{A:record, B:matches[3], match:match}});
return(match);
}catch(e){
elevenLogJSON('error', e);
return(false);
}
}
}
}
new Labels().watch();
+33
View File
@@ -0,0 +1,33 @@
const { spawn } = require('node:child_process');
const { elevenLogJSON } = require('/labels/lib/util.js');
exports.nsupdate = async(server, key, commands) => {
commands.unshift(`server ${server}`);
commands.push('send');
commands.push('quit');
return(new Promise((resolve, reject) => {
elevenLogJSON('debug', {method:'nsupdate()', params:{server:server, key:'******', commands:commands}});
const nsupdate = spawn('/usr/bin/nsupdate', ['-y', key]);
const io = {stdout:'', stderr:''};
nsupdate.stderr.on('data', data => {io.stderr += data.toString()});
nsupdate.stdout.on('data', data => {io.stdout += (data.toString()).replace(/[\r\n]*$/ig, '')});
nsupdate.on('error', error => {reject(error)});
nsupdate.on('close', code =>{
if(code === 0){
if(io.stderr.length > 0){
reject(io.stderr);
}else{
resolve(io.stdout);
}
}else{
reject(io.stderr);
}
});
for(const command of commands){
nsupdate.stdin.write(`${command}\n`);
}
nsupdate.stdin.end();
}));
}