Skip to content

Instantly share code, notes, and snippets.

@jwreagor
Last active November 29, 2017 03:05
Show Gist options
  • Save jwreagor/14976fa5da7b86168102d1adadd479e5 to your computer and use it in GitHub Desktop.
Save jwreagor/14976fa5da7b86168102d1adadd479e5 to your computer and use it in GitHub Desktop.
Example ContainerPilot 3 Config
{
consul: '{{ if .CONSUL_AGENT }}localhost{{ else }}{{ .CONSUL | default "consul"}}{{ end }}:8500',
logging: {
level: '{{ .LOG_LEVEL | default "INFO" }}'
},
jobs: [
{
name: "preStart",
exec: ["/usr/bin/nomad-manage", "preStart"],
{{ if .CONSUL_AGENT }}
when: {
source: "consul-agent",
once: "healthy",
}
{{ end }}
}, {
name: "nomad",
port: 4646,
interfaces: ["inet"],
restarts: "unlimited",
exec: [
"/usr/bin/nomad", "agent",
"-dev",
"-config=/etc/nomad",
],
when: {
source: "preStart",
once: "exitSuccess",
},
health: {
exec: ["/usr/bin/nomad-manage", "health"],
interval: 10,
ttl: 25,
}
}, {
name: "preStop",
exec: ["/usr/bin/nomad-manage", "preStop"],
when: {
source: "nomad",
once: "stopping",
}
},{{ if .CONSUL_AGENT }}{
name: "consul-agent",
restarts: "unlimited",
exec: [
"/usr/bin/consul", "agent",
"-data-dir=/data/consul",
"-config-dir=/etc/consul",
"-log-level=err",
"-rejoin",
"-retry-join", '{{ .CONSUL | default "consul" }}',
"-retry-max", "10",
"-retry-interval", "10s",
],
health: {
exec: "curl -so /dev/null http://localhost:8500",
interval: 10,
ttl: 25,
}
}{{ end }}
]
}

Walk through

  • CONSUL is the address of our Consul cluster. This defaults to just "consul" if used by local Docker linked containers.

  • Only if the env var CONSUL_AGENT is available do we want to run the consul-agent job. This runs the consul agent process inside our container which our containerpilot process will use to communicate with our external Consul cluster. We also want consul-agent to have a health check as well.

  • Run the preStart job. This can be used to preconfigure the "main" service of a container like setting an IP address inside a configuration file.

  • Caveat: If we're given CONSUL_AGENT than our preStart will run after the consul-agent job is healthy.

  • Run our nomad job once our preStart job has run. This job is configured with service registration at port 4646 on interface inet. We want this job to restart an unlimited amount of times. We also have a health check setup as well given our interval and ttl settings.

  • When the nomad is stopping we run our preStop job. This script can handle any clean-up that is required when shutting down our nomad job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment