Skip to content

Commit

Permalink
Add CONSUL_DATACENTER_NAME env (#46)
Browse files Browse the repository at this point in the history
* Resolve #23 by adding `CONSUL_DATACENTER_NAME` env

* Mention the environment variable in the `README.md`

* Triton datacenter auto detection
  • Loading branch information
tjcelaya authored and Justin Reagor committed Dec 15, 2017
1 parent f0bf136 commit eff97e4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ services:
In our experience, including a Consul cluster within a project's `docker-compose.yml` can help developers understand and test how a service should be discovered and registered within a wider infrastructure context.

#### Environment Variables

- `CONSUL_DEV`: Enable development mode, allowing a node to self-elect as a cluster leader. Consul flag: [`-dev`](https://www.consul.io/docs/agent/options.html#_dev).
- The following errors will occur if `CONSUL_DEV` is omitted and not enough Consul instances are deployed:
```
[ERR] agent: failed to sync remote state: No cluster leader
[ERR] agent: failed to sync changes: No cluster leader
[ERR] agent: Coordinate update error: No cluster leader
```
- `CONSUL_DATACENTER_NAME`: Explicitly set the name of the data center in which Consul is running. Consul flag: [`-datacenter`](https://www.consul.io/docs/agent/options.html#datacenter).
- If this variable is specified it will be used as-is.
- If not specified, automatic detection of the datacenter will be attempted. See [issue #23](https://github.com/autopilotpattern/consul/issues/23) for more details.
- Consul's default of "dc1" will be used if none of the above apply.

### Clients

ContainerPilot utilizes Consul's [HTTP Agent API](https://www.consul.io/api/agent.html) for a handful of endpoints, such as `UpdateTTL`, `CheckRegister`, `ServiceRegister` and `ServiceDeregister`. Connecting ContainerPilot to Consul can be achieved by running Consul as a client to a cluster (mentioned above). It's easy to run this Consul client agent from ContainerPilot itself.
Expand Down
12 changes: 12 additions & 0 deletions bin/consul-manage
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ set -eo pipefail
preStart() {
_log "Updating consul advertise address"
sed -i "s/CONTAINERPILOT_CONSUL_IP/${CONTAINERPILOT_CONSUL_IP}/" /etc/consul/consul.hcl

if [ -n "$CONSUL_DATACENTER_NAME" ]; then
_log "Updating consul datacenter name (specified: '${CONSUL_DATACENTER_NAME}' )"
sed -i "s/CONSUL_DATACENTER_NAME/${CONSUL_DATACENTER_NAME}/" /etc/consul/consul.hcl
elif [ -f "/native/usr/sbin/mdata-get" ]; then
DETECTED_DATACENTER_NAME=$(/native/usr/sbin/mdata-get sdc:datacenter_name)
_log "Updating consul datacenter name (detected: '${DETECTED_DATACENTER_NAME}')"
sed -i "s/CONSUL_DATACENTER_NAME/${DETECTED_DATACENTER_NAME}/" /etc/consul/consul.hcl
else
_log "Updating consul datacenter name (default: 'dc1')"
sed -i "s/CONSUL_DATACENTER_NAME/dc1/" /etc/consul/consul.hcl
fi
}

#
Expand Down
1 change: 1 addition & 0 deletions etc/consul.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bind_addr = "CONTAINERPILOT_CONSUL_IP"
datacenter = "CONSUL_DATACENTER_NAME"
data_dir = "/data"
client_addr = "0.0.0.0"
ports {
Expand Down
1 change: 1 addition & 0 deletions local-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ services:
- 8500
environment:
- CONSUL=consul
- CONSUL_DATACENTER_NAME=dc1
command: >
/usr/local/bin/containerpilot

0 comments on commit eff97e4

Please sign in to comment.