Skip to content

Commit eff97e4

Browse files
tjcelayaJustin Reagor
authored andcommitted
Add CONSUL_DATACENTER_NAME env (#46)
* Resolve #23 by adding `CONSUL_DATACENTER_NAME` env * Mention the environment variable in the `README.md` * Triton datacenter auto detection
1 parent f0bf136 commit eff97e4

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ services:
8282
8383
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.
8484

85+
#### Environment Variables
86+
87+
- `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).
88+
- The following errors will occur if `CONSUL_DEV` is omitted and not enough Consul instances are deployed:
89+
```
90+
[ERR] agent: failed to sync remote state: No cluster leader
91+
[ERR] agent: failed to sync changes: No cluster leader
92+
[ERR] agent: Coordinate update error: No cluster leader
93+
```
94+
- `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).
95+
- If this variable is specified it will be used as-is.
96+
- If not specified, automatic detection of the datacenter will be attempted. See [issue #23](https://github.com/autopilotpattern/consul/issues/23) for more details.
97+
- Consul's default of "dc1" will be used if none of the above apply.
98+
8599
### Clients
86100

87101
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.

bin/consul-manage

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ set -eo pipefail
88
preStart() {
99
_log "Updating consul advertise address"
1010
sed -i "s/CONTAINERPILOT_CONSUL_IP/${CONTAINERPILOT_CONSUL_IP}/" /etc/consul/consul.hcl
11+
12+
if [ -n "$CONSUL_DATACENTER_NAME" ]; then
13+
_log "Updating consul datacenter name (specified: '${CONSUL_DATACENTER_NAME}' )"
14+
sed -i "s/CONSUL_DATACENTER_NAME/${CONSUL_DATACENTER_NAME}/" /etc/consul/consul.hcl
15+
elif [ -f "/native/usr/sbin/mdata-get" ]; then
16+
DETECTED_DATACENTER_NAME=$(/native/usr/sbin/mdata-get sdc:datacenter_name)
17+
_log "Updating consul datacenter name (detected: '${DETECTED_DATACENTER_NAME}')"
18+
sed -i "s/CONSUL_DATACENTER_NAME/${DETECTED_DATACENTER_NAME}/" /etc/consul/consul.hcl
19+
else
20+
_log "Updating consul datacenter name (default: 'dc1')"
21+
sed -i "s/CONSUL_DATACENTER_NAME/dc1/" /etc/consul/consul.hcl
22+
fi
1123
}
1224

1325
#

etc/consul.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bind_addr = "CONTAINERPILOT_CONSUL_IP"
2+
datacenter = "CONSUL_DATACENTER_NAME"
23
data_dir = "/data"
34
client_addr = "0.0.0.0"
45
ports {

local-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ services:
1515
- 8500
1616
environment:
1717
- CONSUL=consul
18+
- CONSUL_DATACENTER_NAME=dc1
1819
command: >
1920
/usr/local/bin/containerpilot

0 commit comments

Comments
 (0)