Skip to content

Instantly share code, notes, and snippets.

@philipz
Forked from digital-wonderland/README.md
Last active August 29, 2015 14:09
Show Gist options
  • Save philipz/46adb1ec86024820fd6c to your computer and use it in GitHub Desktop.
Save philipz/46adb1ec86024820fd6c to your computer and use it in GitHub Desktop.

What

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

Service discovery & registration is done via etcd.

elasticsearch-discovery@.service provides a dumb discovery service by registering an elasticsearch host if it should be up. elasticsearch-intelligent-discovery@.service 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 elasticsearch@1.service

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=docker.service
Requires=docker.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=docker.service
Requires=docker.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