Cosmonaut monitors containers, and maintains a HAProxy config based on their states. Cosmonaut works with Cosmos to achieve this.
How does it work?
When Cosmonaut is run on a machine, it does the following:
- Listens to events from the machine's docker daemon.
- When an event relevant to Cosmonaut occurs such as starting or stopping a service container, it retrieves a HAProxy configuration from Cosmos based on the services running and the event's subject.
- It uses the retrieved HAProxy to reload the machine's HAProxy container.
- Docker 1.11 or higher.
- Weave Net: monitored containers must belong to a weave network.
- Java 7 or higher.
- HAProxy: I use rstiller/haproxy. If you are using a different HAProxy, be sure to update the config reload strategy in
Cosmonaut uses a keyword to determine if the container tied to an event is significant. This limits the containers that are included in the HAProxy config to the ones you are interested in. Cosmonaut performs a case insensitive search for the keyword in a container's name. The default keyword is
Make sure you have a Cosmos instance running.
git clonethis repo.
Ensure Docker and Weave Net are running, and the Weave Net environment is configured, so that containers launched via the Docker command line are automatically attached to the Weave network. This is normally done by running the following commands:
$ weave launch $ eval $(weave env)
Set the required
DOCKER_CERT_PATHenvironment variables. For example:
$ export DOCKER_HOST="tcp://192.168.99.100:2376" $ export DOCKER_CERT_PATH="/Users/<user>/.docker/machine/machines/dev"
$ export DOCKER_HOST=unix:///var/run/docker.sock
$ ./gradlew -PcosmosUrl=<url> -PcosmosTable=<table_name>
cosmosUrl is the URL of your Cosmos endpoint and
cosmosTable is the name of the DynamoDB table Cosmos will create and use to persist the state of the cosmonaut machine's services. Cosmos uses the persisted state to ensure the HAProxy config data for running services can be reproduced. Also, you can pass in a keyword by running:
$ ./gradlew -PcosmosUrl=<url> -PcosmosTable=<table_name> -Pkeyword=<keyword_value>
To enable debug logging, set the environment variable
debug before starting Cosmonaut, e.g.
$ export COMSONAUT_LOG=debug
Simply run a docker container with the required environment variables:
$ docker run -d -ti -e CONFIG_MODE=[host|path] -e SERVICE_NAME=<service_name> -e PREDICATE=<predicate> -e COOKIE=<cookie_name> --name <container_name> <image>
$ eval "$(weave env)" $ docker run -d -ti -e CONFIG_MODE=host -e SERVICE_NAME=app1 -e PREDICATE=first.example.com -e COOKIE=JSESSIONID --name a1-asteroid ubuntu
Note that Cosmonaut requires Weave to be used as an overlay network on the host, and containers must be run within the Weave network, hence the
eval $(weave env) command.
- CONFIG_MODE: type of routing. It can be either
host.  In
pathmode, the URL path is used to determine which backend to forward the request to.  In
hostmode, the HTTP host header is used to determine which backend to forward the request to. Defaults to
- SERVICE_NAME: name of service the container belongs to.
- PREDICATE: value used along with mode to determine which service a request will be forwarded to.
acl <cluster> url_beg /<predicate>. 
acl <cluster> hdr(host) -i <predicate>.
- COOKIE: name of cookie to be used for sticky sessions. If not defined, sticky sessions will not be configured.
- PORT: port number where the service on the container is listening on.