Skip to content

Instantly share code, notes, and snippets.

@yaronr
Last active September 27, 2020 17:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save yaronr/aa5e9d1871f047568c84 to your computer and use it in GitHub Desktop.
Save yaronr/aa5e9d1871f047568c84 to your computer and use it in GitHub Desktop.
Bootstrap Consul on CoreOS
#!/bin/bash
source /etc/environment
hostname=$(cat /etc/machine-id)
machines=$(etcdctl ls /consul.io/bootstrap/machines)
#If there are 'machines' then the cluster has been initialised.
if [ -z "$machines" ]
then
flags="${flags} -bootstrap"
else
echo "This cluster has already been bootstrapped"
# set -join flags
flags=$(etcdctl ls /consul.io/bootstrap/machines | while read line; do
ip=$(etcdctl get ${line})
echo ${flags} -join ${ip}
done)
fi
echo "Flags are:" $flags
#set 'this' machine in etcd (so that others could peer)
etcdctl set /consul.io/bootstrap/machines/${hostname} ${COREOS_PRIVATE_IPV4}
/usr/bin/docker run --name consul \
-h ${hostname} \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301/udp \
-p 8302:8302 \
-p 8302:8302/udp \
-p 8400:8400 \
-p 8500:8500 \
-p 53:53/udp \
-v /opt/consul/data:/data \
progrium/consul:latest -server -advertise ${COREOS_PRIVATE_IPV4} ${flags}
[Unit]
Description=Consul agent bootstrap
After=docker.service
Requires=docker.service
[Service]
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/mkdir -p /opt/consul/data
ExecStartPre=-/usr/bin/wget --retry-connrefused -t 5 -N -P /opt/consul/ https://gist.githubusercontent.com/yaronr/aa5e9d1871f047568c84/raw/bootstrap-consul.sh
ExecStartPre=-/usr/bin/chmod +x /opt/consul/bootstrap-consul.sh
ExecStartPre=-/usr/bin/docker kill consul
ExecStartPre=-/usr/bin/docker rm consul
#Make sure this ip is not there already
ExecStartPre=-etcdctl rm /consul.io/bootstrap/machines/$(cat /etc/machine-id)
ExecStart=/opt/consul/bootstrap-consul.sh
ExecStop=-/usr/bin/docker stop consul
ExecStopPost=-etcdctl rm /consul.io/bootstrap/machines/$(cat /etc/machine-id)
[Install]
WantedBy=multi-user.target
[X-Fleet]
Global=true
@yaronr
Copy link
Author

yaronr commented Sep 28, 2014

Done: 'started' should be implicit on existance of /bootstrap/macines values
TODO: /bootstrap/machines should have TTL
TODO: while the service is up, /bootstrap/machines should be updated (to prevent TTL timeout)

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