Last active
September 27, 2020 17:54
-
-
Save yaronr/aa5e9d1871f047568c84 to your computer and use it in GitHub Desktop.
Bootstrap Consul on CoreOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)