Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
psi-4ward committed Apr 16, 2015
0 parents commit 29372a0
Show file tree
Hide file tree
Showing 10 changed files with 732 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tmp
.idea
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Christoph Wiechert <[email protected]>

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.
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# docker-etcd-registrator

Docker service registrator for etcd and skydns (and CoreOS).
The very end of `sidekick.service`

* Startup synchronization: bring etcd up to date
* Add already running containers
* Remove stopped but registred container
* Realtime: Listening for docker events
* Registers all ports
* defined via `EXPOSE` in the `Dockerfile`
* exposed via `-p` commandline argument
* Supports secured etcd
* Service config using ENV
* Written in Javascript
* for (but not limited to) CoreOS, see [fleet-unit-files](https://github.com/psi-4ward/docker-etcd-registrator/tree/master/fleet-unit-files)

(thanks to [gliderlabs/registrator](https://github.com/gliderlabs/registrator) for the some ideas)*

### TODO / Planned

* [Vulcanproxy](vulcanproxy.com) support
* Some general info logging to stdout
* Configuration using commandline arguments
* Support for publicIPs and `--net=host`
* Improve docu


## Install &amp; Config

* For now its only possible to configure docker-etcd-registrator using environment variables
* Make sure the app can read/write to `DOCKER_HOST` (default: `/var/run/docker.sock`)

```shell
git clone https://github.com/psi-4ward/docker-etcd-registrator.git
npm install
ETCD_ENDPOINTS=http://10.1.0.1:4001 node app.js
```

### Config parameters

All params are optional

* `HOSTNAME`: Hostname of the system
* `SKYDNS_ETCD_PREFIX`: `/skydns/local/skydns`
<br>
* `DOCKER_HOST`: `/var/run/docker.sock` or `tcp://localhost:2376`
* `DOCKER_TLS_VERIFY` from docker-modem
* `DOCKER_CERT_PATH`: Directory containing `ca.pem`, `cert.pem`, `key.pem` (filenames hardcoded)
<br>
* `ETCD_ENDPOINTS`: `http://127.0.0.1:4001`
* `ETCD_CAFILE`
* `ETCD_CERTFILE`
* `ETCD_KEYFILE`

### Debug
Enable debugging using `DEBUG` env var: `DEBUG=docker,skydns,service node app.js`

flag | description
---------|-----------------------------
* | print every debug message |
docker | docker related messages |
service | container-inspect => service transformation |
skydns | skydns etcd data population |


## Service Discovery Configration

* Use env vars to configure a specific container / service
* Everything is optional
* Name is received from `SERVICE_NAME` or `--name` or the container ID
* Services with `SERVICE_IGNORE` are not observed

```
$ docker run -d --name mariadb \
-e "SERVICE_NAME=mysql" \
-e "SERVICE_TAGS=database,customers" \
mariadb
```

### Multiple Services per Container

You can specify a service identified by a given port `SERVICE_<PORT>_<FLAG>`:
```
$ docker run -p 80:80 -p 443:443 -p 9000:9000 \
-e "SERVICE_80_NAME=http-proxy" \
-e "SERVICE_443_NAME=https-proxy" \
-e "SERVICE_9000_IGNORE=yes" \
docker/image
```


## Authors

* Christoph Wiechert



## License

[MIT](LICENSE)
89 changes: 89 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var _ = require('lodash');
var util = require('util');
var execSync = require('child_process').execSync;
var serviceFactory = require('./lib/service');
var Docker = require('./lib/Docker');
var SkydnsBE = require('./lib/SkydnsBE');

/*******************/
/* Config defaults */
/*******************/

_.defaults(process.env, {
HOSTNAME: execSync('hostname').toString().trim(),

//DOCKER_HOST: '/var/run/docker.sock',
//DOCKER_HOST: 'tcp://localhost:2376',
//DOCKER_TLS_VERIFY
//DOCKER_CERT_PATH

//ETCD_ENDPOINTS: 'http://127.0.0.1:4001',
//ETCD_CAFILE: undefined,
//ETCD_CERTFILE: undefined,
//ETCD_KEYFILE: undefined,

//SKYDNS_ETCD_PREFIX: '/skydns/local/skydns'
});


/**********/
/* Docker */
/**********/

var docker = new Docker();

// Docker socket gone?
docker.on('eventstream_close', function() {
console.error('Error: Lost connection to docker daemon');
process.exit(2);
});

// Error speaking with docker daemon
docker.on('error', function(err) {
if(err.code === 'ECONNREFUSED') {
console.error('Error: Connection to ' + process.env.DOCKER_HOST + ' refused!');
process.exit(2);
}
console.error(util.inspect(err, {showHidden: false, depth: 4}));
});


/**********/
/* SkyDNS */
/**********/

var skydnsBE = new SkydnsBE();

function err(method, err) {
if(!err) return;
console.error('Error: ' + method);
console.error(util.inspect(err, {showHidden: false, depth: 4}));
}

// Docker started a container
docker.on('start', function(data) {
var service = serviceFactory(data);
if(!service) return;

service.byPorts().forEach(function(portService) {
skydnsBE.addService(portService, err.bind('addService'));
});
});

// A container died (kill, crash, stop, ...)
docker.on('die', function(cid) {
skydnsBE.removeServiceByCid(cid);
});

// Remove services which dont have running containers
// Add services for container already running
docker.getRunning(function(err, data) {
var portServices = _(data)
.map(serviceFactory)
.compact()
.invoke('byPorts')
.flatten()
.value();

skydnsBE.sync(portServices);
});
43 changes: 43 additions & 0 deletions fleet-unit-files/skydns.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[Unit]
Description=infra-skydns
Requires=docker.service etcd.service
After=docker.service etcd.service

[Service]
Restart=always
RestartSec=5s
TimeoutStartSec=120
TimeoutStopSec=25

EnvironmentFile=/etc/environment

# remove old container
ExecStartPre=/bin/sh -c "docker ps -a | grep %p 1>/dev/null && docker rm %p || true"

# initial write config (only if /skydns/config does not exist
ExecStartPre=/bin/sh -c "\
/opt/bin/etcdctl2 ls /skydns/config &> /dev/null \
|| \
/opt/bin/etcdctl2 set /skydns/config \
'{\"domain\":\"local.\",\"hostmaster\":\"[email protected]\",\"nameservers\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"ttl\":60}' \
"

# Start the container
ExecStart=/bin/sh -c "\
/usr/bin/docker run \
--rm \
--name=%p \
-p 0.0.0.0:53:53 -p 0.0.0.0:53:53/udp \
--env ETCD_MACHINES=https://${COREOS_PUBLIC_IPV4}:4001 \
--env ETCD_TLSKEY=/etc/ssl/etcd/node.key \
--env ETCD_TLSPEM=/etc/ssl/etcd/node.crt \
--env ETCD_CACERT=/etc/ssl/etcd/ca.crt \
--env SKYDNS_ADDR=0.0.0.0:53 \
-v /etc/ssl/etcd:/etc/ssl/etcd \
skynetservices/skydns"

ExecStop=/usr/bin/docker stop %p


[X-Fleet]
Global=true
Loading

0 comments on commit 29372a0

Please sign in to comment.