Skip to content

Instantly share code, notes, and snippets.

@digital-wonderland
Last active April 6, 2022 17:22
Show Gist options
  • Save digital-wonderland/e0bd8e0d4c91a7fec2c7 to your computer and use it in GitHub Desktop.
Save digital-wonderland/e0bd8e0d4c91a7fec2c7 to your computer and use it in GitHub Desktop.
Unit files to deploy an ElasticSearch cluster on CoreOS via Fleet

What

Unit files to deploy an ElasticSearch cluster on CoreOS via Fleet.

Service discovery & registration is done via etcd.

[email protected] provides a dumb discovery service by registering an elasticsearch host if it should be up. [email protected] registers the service only if it is running.

A service & timer unit for elasticsearch curator is provided which does some housekeeping.

Usage

First load the necessary units:

$ fleetctl load elasticsearch-data@{1,2,3}.service elasticsearch-discovery@{1,2,3}.service elasticsearch@{1,2,3}.service

then start the first unit:

$ fleetctl start [email protected]

after the unit is up and running start the others:

$ fleetctl start elasticsearch@{2,3}.service
[Unit]
Description=Elasticsearch Curator Service
Documentation=https://github.com/elasticsearch/curator
After=docker.service
Requires=docker.service
[Service]
TimeoutSec=0
EnvironmentFile=/etc/environment
ExecStartPre=/usr/bin/docker pull digitalwonderland/elasticsearch-curator
ExecStart=/bin/sh -c '\
/usr/bin/docker run --rm digitalwonderland/elasticsearch-curator --host ${COREOS_PRIVATE_IPV4} optimize --prefix logstash- --older-than 1 --max_num_segments 1; \
/usr/bin/docker run --rm digitalwonderland/elasticsearch-curator --host ${COREOS_PRIVATE_IPV4} delete --prefix logstash- --older-than 30; \
/usr/bin/docker run --rm digitalwonderland/elasticsearch-curator --host ${COREOS_PRIVATE_IPV4} optimize --prefix .marvel- --older-than 1 --max_num_segments 1; \
/usr/bin/docker run --rm digitalwonderland/elasticsearch-curator --host ${COREOS_PRIVATE_IPV4} delete --prefix .marvel- --older-than 30'
[X-Fleet]
# Wildcards are not supported: https://github.com/coreos/fleet/issues/494
#MachineOf=elasticsearch@*.service
[Unit]
Description=Run Elasticsearch Curator Daily
[Timer]
OnBootSec=12h
OnUnitActiveSec=1d
[Install]
WantedBy=timers.target
[X-Fleet]
MachineOf=elasticsearch-curator.service
[Unit]
Description=Elasticsearch Data Volume
After=docker.service
Requires=docker.service
Before=elasticsearch@%i.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/sh -c "docker inspect %p-%i >/dev/null 2>&1 || docker run --name %p-%i -v /var/lib/elasticsearch busybox chmod -R 0777 /var/lib/elasticsearch"
[X-Fleet]
Conflicts=%p@*.service
MachineOf=elasticsearch@%i.service
[Unit]
Description=Elasticsearch Discovery Service
After=etcd.service
Requires=etcd.service
Before=elasticsearch@%i.service
BindsTo=elasticsearch@%i.service
[Service]
EnvironmentFile=/etc/environment
ExecStart=/bin/bash -c '\
while true; do \
etcdctl set /services/elasticsearch/${COREOS_PRIVATE_IPV4} \'{"http_port": 9200, "transport_port": 9300}\' --ttl 60; \
sleep 45; \
done'
ExecStop=/usr/bin/etcdctl rm /services/elasticsearch/${COREOS_PRIVATE_IPV4}
[X-Fleet]
MachineOf=elasticsearch@%i.service
[Unit]
Description=Elasticsearch Discovery Service
After=etcd.service
Requires=etcd.service
After=elasticsearch@%i.service
BindsTo=elasticsearch@%i.service
[Service]
EnvironmentFile=/etc/environment
ExecStart=/bin/bash -c '\
while true; do \
curl -fSs ${COREOS_PRIVATE_IPV4}:9200 > /dev/null; \
if [ "$?" = "0" ]; then \
etcdctl set /services/elasticsearch/${COREOS_PRIVATE_IPV4} \'{"http_port": 9200, "transport_port": 9300}\' --ttl 60; \
else \
etcdctl rm /services/elasticsearch/${COREOS_PRIVATE_IPV4}; \
fi; \
sleep 45; \
done'
ExecStop=/usr/bin/etcdctl rm /services/elasticsearch/${COREOS_PRIVATE_IPV4}
[X-Fleet]
MachineOf=elasticsearch@%i.service
[Unit]
Description=Elasticsearch Service
Documentation=http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/index.html
After=docker.service
Requires=docker.service
Requires=elasticsearch-data@%i.service
Requires=elasticsearch-discovery@%i.service
[Service]
TimeoutSec=0
Restart=always
RestartSec=1m
EnvironmentFile=/etc/environment
SuccessExitStatus=255
ExecStartPre=-/usr/bin/docker kill %p-%i
ExecStartPre=-/usr/bin/docker rm %p-%i
ExecStartPre=/usr/bin/docker pull digitalwonderland/elasticsearch
ExecStart=/bin/bash -c '\
curl -f ${COREOS_PRIVATE_IPV4}:4001/v2/keys/services/elasticsearch; \
if [ "$?" = "0" ]; then \
UNICAST_HOSTS=$(etcdctl ls --recursive /services/elasticsearch \
| sed "s/\/services\/elasticsearch\///g" \
| sed "s/$/:9300/" \
| paste -s -d","); \
else \
UNICAST_HOSTS=""; \
fi; \
/usr/bin/docker run \
--rm \
--name %p-%i \
--publish 9200:9200 \
--publish 9300:9300 \
--volumes-from elasticsearch-data-%i \
-e ELASTICSEARCH_CLUSTER_NAME=logstash \
digitalwonderland/elasticsearch \
-Des.node.name=%p-%i \
-Des.network.publish_host=${COREOS_PRIVATE_IPV4} \
-Des.discovery.zen.ping.multicast.enabled=false \
-Des.discovery.zen.ping.unicast.hosts=$UNICAST_HOSTS'
ExecStop=/usr/bin/docker stop %p-%i
ExecStopPost=-/usr/bin/docker rm %p-%i
[X-Fleet]
Conflicts=%p@*.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment