Skip to content

Instantly share code, notes, and snippets.

@psi-4ward
Created April 14, 2015 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psi-4ward/bfec77f1702e4dd67ff1 to your computer and use it in GitHub Desktop.
Save psi-4ward/bfec77f1702e4dd67ff1 to your computer and use it in GitHub Desktop.
CoreOS - Consul - Registrator
  • /opt/bin/etcdctl2 is a wrapper for etcdctl to add certs
  • copy the consul web-ui into /DATA/infra/consul/ui
[Unit]
Description=infra-consul-registrator
After=infra-consul.service
Requires=infra-consul.service
[Service]
ExecStartPre=-/usr/bin/docker kill %p
ExecStartPre=-/usr/bin/docker rm %p
ExecStartPre=/usr/bin/docker pull progrium/registrator
ExecStart=/usr/bin/docker run \
--rm \
--env SERVICE_NAME=infra-consul-registrator \
--env SERVICE_TAGS=infra,discovery \
--name %p \
--volume /var/run/docker.sock:/tmp/docker.sock \
--hostname %H \
--link infra-consul:consul \
progrium/registrator:master \
-internal \
consul://consul:8500
ExecStop=/usr/bin/docker stop %p
[X-Fleet]
Global=true
[Unit]
Description=infra-consul-sidekick
Requires=infra-consul.service
BindsTo=infra-consul.service
After=infra-consul.service
[Service]
Type=simple
Restart=always
RestartSec=30
EnvironmentFile=/etc/environment
ExecStart=/bin/sh -c " \
while true; do \
SRVIP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' infra-consul) ; \
/opt/bin/etcdctl2 set --ttl 60 /services/infra/consul/nodes/%H $SRVIP ; \
\
[ \"%H\" = \"node02\" ] && /opt/bin/etcdctl2 set --ttl 120 \
/services/infra/haproxy/backends/infra-consul/servers/%H \
\"{\\\"ip\\\":\\\"$SRVIP\\\",\\\"port\\\":8500}\" \
1>/dev/null ; \
sleep 45 ; \
done"
ExecStop=/bin/sh -c "/opt/bin/etcdctl2 rm /services/infra/consul/nodes/%H"
[X-Fleet]
Global=true
[Unit]
Description=infra-consul
Requires=docker.service etcd.service flanneld.service
After=docker.service etcd.service flanneld.service
Wants=infra-consul-sidekick.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"
# Start the container
ExecStart=/bin/sh -c "\
/usr/bin/docker run \
--rm \
--env SERVICE_NAME=infra-consul \
--env SERVICE_TAGS=infra,discovery \
--env SERVICE_8500_NAME=infra-consul-ui \
--env SERVICE_8500_TAGS=infra,discovery,http-protected-sysops \
-p 53:8600 -p 53:8600/udp \
--name=%p \
--hostname=%H \
--env=GOMAXPROCS=10 \
-v /DATA/infra/consul/ui:/ui \
gliderlabs/consul-server:latest \
-server \
-bootstrap-expect 3 \
-dc dc1 \
-client 0.0.0.0 \
-node %H \
-ui-dir=/ui \
-encrypt QXNvZHVnYXZpNDU0VWt1Zw=="
# one-time self publish
ExecStartPost=/bin/sh -c "\
sleep 1 ; \
SRVIP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' infra-consul) ; \
for i in `seq 1 20`; do \
ncat --recv-only --send-only -w 1 $SRVIP 8500 &>/dev/null && break ; \
echo Waiting for Consul to start on $SRVIP:8500; sleep 1 ; \
done ; \
/opt/bin/etcdctl2 set --ttl 60 /services/infra/consul/nodes/%H $SRVIP"
# Join the cluster
ExecStartPost=/bin/sh -c "\
for i in `seq 1 10`; do \
JOIN_NODE=$(/opt/bin/etcdctl2 ls /services/infra/consul/nodes | grep -v %H | tail -1) ; \
[ ! \"$JOIN_NODE\" = '' ] \
&& JOIN_IP=$(/opt/bin/etcdctl2 get $JOIN_NODE) \
&& [ ! \"$JOIN_IP\" = '' ] \
&& /usr/bin/docker exec infra-consul consul join $JOIN_IP \
&& exit 0 ; \
sleep 10 ; \
done ; \
>&2 echo Could not find any other Consul Node in etcd! ; exit 1"
ExecStop=/usr/bin/docker stop %p
[X-Fleet]
Global=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment