forked from a-goryachev/docker-swarm-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es-docker
executable file
·92 lines (78 loc) · 2.98 KB
/
es-docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# Run Elasticsearch and allow setting default settings via env vars
#
# e.g. Setting the env var cluster.name=testcluster
#
# will cause Elasticsearch to be invoked with -Ecluster.name=testcluster
#
# see https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html#_setting_default_settings
es_opts=''
if [ -z ${SERVICE_NAME} ];then
>&2 echo "Environment variable SERVICE_NAME not set. You MUST set it to name of docker swarm service"
exit 3
fi
echo "Discovering other nodes in cluster..."
# Docker swarm's DNS resolves special hostname "tasks.<service_name" to IP addresses of all containers inside overlay network
SWARM_SERVICE_IPs=""
while [ -z "$SWARM_SERVICE_IPs" ]; do
sleep 5
SWARM_SERVICE_IPs=$(dig tasks.${SERVICE_NAME} +short)
done
echo "Nodes of service ${SERVICE_NAME}:"
echo "$SWARM_SERVICE_IPs"
HOSTNAME=$(hostname)
MY_IP=$(dig ${HOSTNAME} +short)
echo "My IP: ${MY_IP}"
OTHER_NODES=""
for NODE_IP in $SWARM_SERVICE_IPs
do
if [ "${NODE_IP}" == "${MY_IP}" ];then
continue;
fi
OTHER_NODES="${OTHER_NODES}${NODE_IP},"
done
if [ -n "${MY_IP}" ];then
echo "Setting network.publish_host=${MY_IP}"
es_opt="-Enetwork.publish_host=${MY_IP}"
es_opts+=" ${es_opt}"
echo "Setting network.host=0.0.0.0"
es_opt="-Enetwork.host=0.0.0.0"
es_opts+=" ${es_opt}"
echo "Setting network.bind_host=0.0.0.0"
es_opt="-Enetwork.bind_host=0.0.0.0"
es_opts+=" ${es_opt}"
fi
if [ -n "$OTHER_NODES" ];then
echo "Setting discovery.zen.ping.unicast.hosts=${OTHER_NODES%,}"
es_opt="-Ediscovery.zen.ping.unicast.hosts=${OTHER_NODES%,}"
es_opts+=" ${es_opt}"
else
echo "There is no another nodes in cluster. I am alone!"
fi
#Configuring elasticsearch using environment variables
while IFS='=' read -r envvar_key envvar_value
do
# Elasticsearch env vars need to have at least two dot separated lowercase words, e.g. `cluster.name`
if [[ "$envvar_key" =~ ^[a-z]+\.[a-z]+ ]]
then
if [[ ! -z $envvar_value ]]; then
es_opt="-E${envvar_key}=${envvar_value}"
es_opts+=" ${es_opt}"
fi
fi
done < <(env)
# The virtual file /proc/self/cgroup should list the current cgroup
# membership. For each hierarchy, you can follow the cgroup path from
# this file to the cgroup filesystem (usually /sys/fs/cgroup/) and
# introspect the statistics for the cgroup for the given
# hierarchy. Alas, Docker breaks this by mounting the container
# statistics at the root while leaving the cgroup paths as the actual
# paths. Therefore, Elasticsearch provides a mechanism to override
# reading the cgroup path from /proc/self/cgroup and instead uses the
# cgroup path defined the JVM system property
# es.cgroups.hierarchy.override. Therefore, we set this value here so
# that cgroup statistics are available for the container this process
# will run in.
export ES_JAVA_OPTS="-Des.cgroups.hierarchy.override=/ $ES_JAVA_OPTS"
echo "Launching command: bin/elasticsearch ${es_opts}"
exec bin/elasticsearch ${es_opts}