Skip to content

Instantly share code, notes, and snippets.

@philips
Last active September 16, 2020 14:19
Show Gist options
  • Save philips/56fa3f5dae9060fbd100 to your computer and use it in GitHub Desktop.
Save philips/56fa3f5dae9060fbd100 to your computer and use it in GitHub Desktop.
Running consul on top of CoreOS utilizing etcd for automatic bootstrapping.
#!/bin/bash
source /etc/environment
name=$(cat /etc/machine-id)
if [ ! -f /opt/consul ]; then
mkdir /opt
mkdir /var/lib/consul
curl http://storage.googleapis.com/ifup-org-tilde/consul-0.2.0-linux-x86-64.lzma | lzcat > /opt/consul
chmod +x /opt/consul
fi
FLAGS="-help "
if etcdctl mk /consul.io/bootstrap/started true; then
flags="${flags} -bootstrap"
else
echo "This cluster has already been bootstrapped"
fi
etcdctl set /consul.io/bootstrap/machines/${name} ${COREOS_PRIVATE_IPV4}
flags=$(etcdctl ls /consul.io/bootstrap/machines | while read line; do
ip=$(etcdctl get ${line})
echo ${flags} -join ${ip}
done)
echo ${flags}
/opt/consul agent -data-dir /var/lib/consul -server ${flags}
#cloud-config
coreos:
etcd:
discovery: https://discovery.etcd.io/0db899fa78e7485b9bd3b5af73a91f7b
addr: $public_ipv4:4001
peer-addr: $public_ipv4:7001
units:
- name: etcd.service
command: start
- name: consul.service
command: start
runtime: no
content: |
[Unit]
Description=fleet
[Service]
ExecStartPre=-/usr/bin/mkdir /opt
ExecStartPre=-/usr/bin/wget --retry-connrefused -t 0 -nc -O /opt/bootstrap-consul https://gist.githubusercontent.com/philips/56fa3f5dae9060fbd100/raw/b0190c7a01a3500e12f92946e6bbf77bc418c3ca/gistfile1.txt
ExecStartPre=-/usr/bin/chmod +x /opt/bootstrap-consul
ExecStart=/opt/bootstrap-consul
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment