Skip to content

Instantly share code, notes, and snippets.

@bdclark
Last active September 27, 2020 17:56
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bdclark/7593bd5c14376b5c563d to your computer and use it in GitHub Desktop.
Save bdclark/7593bd5c14376b5c563d to your computer and use it in GitHub Desktop.
CoreOS cloud-config to bootstrap consul cluster
#cloud-config
coreos:
etcd2:
# generate a token from https://discovery.etcd.io/new?size=3
discovery: https://discovery.etcd.io/<token>
advertise-client-urls: http://$private_ipv4:2379
initial-advertise-peer-urls: http://$private_ipv4:2380
listen-client-urls: http://0.0.0.0:2379
listen-peer-urls: http://$private_ipv4:2380
fleet:
public-ip: $public_ipv4
etcd_servers: http://127.0.0.1:2379
metadata: consul_role=server
flannel:
interface: $public_ipv4
units:
- name: etcd2.service
command: start
- name: fleet.service
command: start
- name: consul-server.service
command: start
content: |
[Unit]
Description=Consul Server Agent
After=etcd2.service
After=fleet.service
After=docker.service
Wants=consul-announce.service
Before=consul-announce.service
[Service]
EnvironmentFile=/etc/environment
Restart=on-failure
RestartSec=240
ExecStartPre=-/usr/bin/docker kill consul-server
ExecStartPre=-/usr/bin/docker rm consul-server
ExecStartPre=/usr/bin/docker pull gliderlabs/consul-server
ExecStart=/bin/sh -c '/usr/bin/docker run --rm \
--name consul-server \
--env SERVICE_IGNORE=true \
--net=host \
--volume /opt/consul:/data \
gliderlabs/consul-server \
-bootstrap-expect 3 \
-advertise $COREOS_PRIVATE_IPV4'
ExecStop=/usr/bin/docker stop consul-server
- name: consul-announce.service
command: start
content: |
[Unit]
Description=Consul Server Announcer
PartOf=consul-server.service
After=consul-server.service
[Service]
EnvironmentFile=/etc/environment
ExecStart=/bin/sh -c '\
while true; do \
etcdctl set /consul.io/bootstrap/machines/$(cat /etc/machine-id) $COREOS_PRIVATE_IPV4 --ttl 60; \
/usr/bin/docker exec consul-server consul join $(for x in $(etcdctl ls /consul.io/bootstrap/machines);do echo -n "$(etcdctl get $x) ";done); \
sleep 45; \
done'
ExecStop=-/bin/sh -c '/usr/bin/etcdctl rm /consul/bootstrap/machines/$(cat /etc/machine-id)'
- name: registrator.service
command: start
content: |
[Unit]
Description=Registrator
PartOf=consul-server.service
After=consul-server.service
[Service]
EnvironmentFile=/etc/environment
Restart=on-failure
ExecStartPre=-/usr/bin/docker kill registrator
ExecStartPre=-/usr/bin/docker rm registrator
ExecStartPre=/usr/bin/docker pull gliderlabs/registrator
ExecStart=/bin/sh -c '/usr/bin/docker run --rm \
--name registrator \
--net=host \
--volume /var/run/docker.sock:/tmp/docker.sock \
gliderlabs/registrator consul://${COREOS_PRIVATE_IPV4}:8500'
ExecStop=/usr/bin/docker stop registrator
- name: dnsmasq.service
command: start
content: |
[Unit]
Description=Dnsmasq
PartOf=consul-server.service
After=consul-server.service
[Service]
EnvironmentFile=/etc/environment
Restart=on-failure
ExecStartPre=/usr/bin/mkdir -p /etc/systemd/resolved.conf.d
ExecStartPre=-/bin/sh -c 'rm /etc/systemd/resolved.conf.d/00-consul-dns.conf && systemctl restart systemd-resolved'
ExecStartPre=-/usr/bin/docker kill dnsmasq
ExecStartPre=-/usr/bin/docker rm dnsmasq
ExecStartPre=/usr/bin/docker pull andyshinn/dnsmasq
ExecStart=/bin/sh -c '/usr/bin/docker run --rm \
--name dnsmasq \
--net=host \
--cap-add=NET_ADMIN \
--env SERVICE_IGNORE=true \
andyshinn/dnsmasq -S /consul/${COREOS_PRIVATE_IPV4}#8600'
ExecStartPost=/bin/sh -c '\
echo -e "[Resolve]\nDNS=${COREOS_PRIVATE_IPV4}" > /etc/systemd/resolved.conf.d/00-consul-dns.conf && \
systemctl restart systemd-resolved;'
ExecStop=/usr/bin/docker stop dnsmasq
ExecStopPost=/bin/sh -c 'rm /etc/systemd/resolved.conf.d/00-consul-dns.conf && systemctl restart systemd-resolved'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment