Skip to content

Instantly share code, notes, and snippets.

@skippy
Last active September 27, 2020 17:54
Show Gist options
  • Save skippy/94645f8442203dc37b5b to your computer and use it in GitHub Desktop.
Save skippy/94645f8442203dc37b5b to your computer and use it in GitHub Desktop.
starting a consul cluster via fleet. 3 servers are started, and clients fill out the rest of the fleet. If you want to get specific, add `MachineMetadata` to specifically place where consul-servers will spend up
#!/bin/bash
# set -eo pipefail
export CONSUL_NUM_INSTANCES=${CONSUL_NUM_INSTANCES:-3}
echo " => stopping consul"
fleetctl stop -block-attempts=10 \
$(eval echo "consul-server@{1..$CONSUL_NUM_INSTANCES}.service") \
consul-agent.service
# FIXME: sometimes it takes awhile for systemd to stop things...
# if we go right to destroy this can leave services running...
echo " => destroying consul"
fleetctl destroy \
$(eval echo "consul-server@{1..$CONSUL_NUM_INSTANCES}.service") \
consul-agent.service
echo " => sleeping and then submitting consul"
SCRIPT=`readlink -f "$0"`
SCRIPTPATH=`dirname "$SCRIPT"`
sleep 5
fleetctl load \
$(eval echo "$SCRIPTPATH/consul-server@{1..$CONSUL_NUM_INSTANCES}.service") \
"$SCRIPTPATH/consul-agent.service"
echo " => starting consul"
fleetctl start \
$(eval echo "consul-server@{1..$CONSUL_NUM_INSTANCES}.service") \
consul-agent.service
[Unit]
Description=Consul Agent
Requires=docker.service
Requires=fleet.service
After=docker.service
After=fleet.service
[Service]
TimeoutStartSec=10m
TimeoutStopSec=90s
Restart=on-failure
RestartSec=10s
# Get CoreOS environmental variables
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/docker kill %p
ExecStartPre=-/usr/bin/docker rm %p
ExecStartPre=/usr/bin/docker pull progrium/consul
ExecStart=/bin/bash -c '\
log_tag="%p"; \
if $(systemctl list-units | grep consul-server 2>&1 >/dev/null); then \
logger -t "$log_tag" "skipping host as it already has a consul server running"; \
exit 0; \
else \
primary_ip=$(fleetctl list-units --fields=unit,sub,machine | grep -E "consul-server@1.service" | sed -r "s/.*\/(.+?)/\1/"); \
logger -t "$log_tag" "starting consul client. Connecting to $primary_ip"; \
$(docker run --rm progrium/consul cmd:run ${COREOS_PRIVATE_IPV4}::$primary_ip::client --name %p); \
fi; '
ExecStop=-/usr/bin/docker stop -t 30 %p
ExecStop=-/usr/bin/docker rm %p
[X-Fleet]
Global=true
[Unit]
Description=Consul Server
Requires=docker.service
Requires=fleet.service
After=docker.service
After=fleet.service
[Service]
TimeoutStartSec=10m
TimeoutStopSec=90s
Restart=always
RestartSec=10s
# Get CoreOS environmental variables
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/docker kill %p
ExecStartPre=-/usr/bin/docker rm %p
ExecStartPre=/usr/bin/docker pull progrium/consul
ExecStart=/bin/bash -c '\
log_tag="%p"; \
if [ %i -eq 1 ]; then \
logger -t "$log_tag" "starting initial consul server at ${COREOS_PRIVATE_IPV4}"; \
$(docker run --rm -e EXPECT=3 progrium/consul cmd:run ${COREOS_PRIVATE_IPV4} --name %p); \
else \
primary_ip=$(fleetctl list-units --fields=unit,sub,machine | grep -E "%p@1.service" | sed -r "s/.*\/(.+?)/\1/"); \
logger -t "$log_tag" "starting secondary consul server: $primary_ip"; \
$(docker run --rm progrium/consul cmd:run ${COREOS_PRIVATE_IPV4}::$primary_ip --name %p); \
fi; \
'
ExecStop=-/usr/bin/docker exec %p consul leave
ExecStop=-/usr/bin/docker stop -t 30 %p
ExecStop=-/usr/bin/docker rm %p
[X-Fleet]
Conflicts=%p@*.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment