How to deploy an Elasticsearch cluster easily

Here is a simple sh allowing you to deploy ElasticSearch on multiple servers with dedicated roles: master, slave or monitor.

-Master: can be an Elasticsearch master, acts as load balancer on the cluster, doesn’t store data and can use the http transport.
-Slave: a data node, can not be an Elasticsearch master and can not use the http transport.
-Monitor: doesn’t store data, can not be an Elasticsearch master, hold plugins and can use the http transport. By default, uses the paramedic plugin.

#!/bin/bash

slaves=("ip2" "ip3" "ip5")
monitors=("ip4")
masters=("ip1")

install_path=/home/user/opt/elasticsearch
cluster_name="Our Search"
index_number_of_shards=20
path_data=/data/d1/index
version=0.20.6

es_heap_master=3000
es_heap_monitor=2000
es_heap_slave=3000

#let's do it!
nodes=( ${slaves[@]} ${monitors[@]} ${masters[@]})

echo "Preparing cluster $cluster_name: ${nodes[@]}"

for node in ${nodes[@]}
do
 echo "Preparing node $node"
 ssh root@$node "$install_path/current/bin/service/elasticsearch stop
   rm -rf $install_path;
   mkdir -p $install_path;
   cd $install_path;
   wget -t 7 --waitretry=14 --random-wait -q http://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-${version}.tar.gz;
   tar -xf $install_path/elasticsearch-${version}.tar.gz;
   ln -s $install_path/elasticsearch-${version} $install_path/current;
   cd $install_path/current/bin;
   wget -t 7 --waitretry=14 --random-wait -q https://github.com/elasticsearch/elasticsearch-servicewrapper/archive/master.zip;
   unzip -q master.zip;
   mv elasticsearch-servicewrapper-master/service . ;
   rm -rf elasticsearch-servicewrapper* master.zip*";
 echo "Node $node ready!"
done

gateway_expected_nodes=$((${#slaves[@]} + ${#monitors[@]} + ${#masters[@]}))

discovery_zen_ping_unicast_hosts=$(printf ",\"%s\"" "${masters[@]}")
discovery_zen_ping_unicast_hosts=${discovery_zen_ping_unicast_hosts:1}

#for master
for master in ${masters[@]}
do
 echo "Installing master $master"
 ssh root@$master "echo 'node.name: master ($master)
node.data: false
http.enabled: true
node.master: true
bootstrap.mlockall: true
index.number_of_shards: $index_number_of_shards
cluster.name: $cluster_name
path.data: $path_data
transport.tcp.compress: true
gateway.expected_nodes: $gateway_expected_nodes
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: [$discovery_zen_ping_unicast_hosts]' > $install_path/current/config/elasticsearch.yml"

 ssh root@$master "sed -i 's#^\(set.default.ES_HOME\s*=\s*\).*\$#\1$install_path/current#' $install_path/current/bin/service/elasticsearch.conf"
 ssh root@$master "sed -i 's#^\(set.default.ES_HEAP_SIZE\s*=\s*\).*\$#\1$es_heap_master#' $install_path/current/bin/service/elasticsearch.conf"

 echo "Master $master installed, starting..."

 ssh root@$master "$install_path/current/bin/service/elasticsearch start"
 echo "Master $master started!"
done

#for slaves
for slave in ${slaves[@]}
do
 echo "Installing slave $slave"
 ssh root@$slave "echo 'node.name: data ($slave)
node.data: true
http.enabled: false
node.master: false
bootstrap.mlockall: true
index.number_of_shards: $index_number_of_shards
cluster.name: $cluster_name
path.data: $path_data
transport.tcp.compress: true
gateway.expected_nodes: $gateway_expected_nodes
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: [$discovery_zen_ping_unicast_hosts]' > $install_path/current/config/elasticsearch.yml"

 ssh root@$slave "sed -i 's#^\(set.default.ES_HOME\s*=\s*\).*\$#\1$install_path/current#' $install_path/current/bin/service/elasticsearch.conf"
 ssh root@$slave "sed -i 's#^\(set.default.ES_HEAP_SIZE\s*=\s*\).*\$#\1$es_heap_slave#' $install_path/current/bin/service/elasticsearch.conf"

 echo "Slave $slave installed, starting..."

 ssh root@$slave "$install_path/current/bin/service/elasticsearch start"
 echo "Slave $slave started!"
done

#for monitors
for monitor in ${monitors[@]}
do
 echo "Installing monitor $monitor"

 ssh root@$monitor "$install_path/current/bin/plugin -install karmi/elasticsearch-paramedic"

 ssh root@$monitor "echo 'node.name: monitor ($monitor)
node.data: false
http.enabled: true
node.master: false
bootstrap.mlockall: true
index.number_of_shards: $index_number_of_shards
cluster.name: $cluster_name
path.data: $path_data
transport.tcp.compress: true
gateway.expected_nodes: $gateway_expected_nodes
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: [$discovery_zen_ping_unicast_hosts]' > $install_path/current/config/elasticsearch.yml"

 ssh root@$monitor "sed -i 's#^\(set.default.ES_HOME\s*=\s*\).*\$#\1$install_path/current#' $install_path/current/bin/service/elasticsearch.conf"
 ssh root@$monitor "sed -i 's#^\(set.default.ES_HEAP_SIZE\s*=\s*\).*\$#\1$es_heap_monitor#' $install_path/current/bin/service/elasticsearch.conf"

 echo "Monitor $monitor installed, starting..."

 ssh root@$monitor "$install_path/current/bin/service/elasticsearch start"
 echo "Monitor $monitor started!"
done

Related Posts

Comments (2)

I don't think it's safe to use root in this context

Indeed! This script was initialy written for a dedicated cloud where the only user available on the servers was… root. But you're right, any user with rights on the directory $install_path can do it.

Leave a comment

About privacy:

This site uses Akismet to reduce spam. Learn how your comment data is processed.