Skip to content

Instantly share code, notes, and snippets.

@kurtharriger
Last active February 9, 2016 22:19
Show Gist options
  • Save kurtharriger/29aa906998761fc15f12 to your computer and use it in GitHub Desktop.
Save kurtharriger/29aa906998761fc15f12 to your computer and use it in GitHub Desktop.
Install elasticsearch on ec2 node
#! /bin/bash
set -e
AWS_ACCESS_KEY=$1
AWS_SECRET_KEY=$2
if [ -z "$AWS_SECRET_KEY" ]; then
echo "usge: $0 <aws_access_key> <aws_secret_key"
exit 2
fi
if [ "$(id -u)" != 0 ]; then
echo "this script must be run as root"
exit 2
fi
# format and mount ephemeral storage
# default fstab mounts to /media/ephemeral0
if ! mount | grep "/media/ephemeral0"; then
mkfs.ext4 /dev/xvdb
fi
if ! mount | grep "/media/ephemeral1"; then
if lsblk | grep xvdc; then
mkfs.ext4 /dev/xvdc
mkdir -p /media/ephemeral1
echo "/dev/xvdc /media/ephemeral1 auto defaults,nofail,comment=cloudconfig 0 2" >> /etc/fstab
fi
fi
mount -a
# install elasticsearch
if ! rpm -q elasticsearch; then
if [ ! -f elasticsearch-2.2.0.rpm ]; then
wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.2.0/elasticsearch-2.2.0.rpm
fi
rpm -Uvh elasticsearch-2.2.0.rpm
fi
# install plugins
cd /usr/share/elasticsearch
bin/plugin install -b license
bin/plugin install -b marvel-agent
bin/plugin install -b lmenezes/elasticsearch-kopf
bin/plugin install -b mobz/elasticsearch-head
bin/plugin install -b cloud-aws
# setup data dir
mkdir -p /media/ephemeral0/elasticsearch
chown elasticsearch:elasticsearch /media/ephemeral0/elasticsearch
PATH_DATA=/media/ephemeral0/elasticsearch
if mount | grep "/media/ephemeral1"; then
mkdir -p /media/ephemeral1/elasticsearch
chown elasticsearch:elasticsearch /media/ephemeral1/elasticsearch
PATH_DATA=/media/ephemeral0/elasticsearch,/media/ephemeral1/elasticsearch
fi
# backup and create new elasticsearch configuration
if [ ! -f /etc/elasticsearch/elasticsearch.yml ]; then
mv /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.orig
fi
cat <<END > /etc/elasticsearch/elasticsearch.yml
cluster.name: hellfish-elastic-spike
path.data: $PATH_DATA
bootstrap.mlockall: true
network.host: $(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
node.name: $(hostname)
cloud.aws.access_key: $AWS_ACCESS_KEY
cloud.aws.secret_key: $AWS_SECRET_KEY
cloud.aws.region: us-west-2
discovery.type: ec2
discovery.ec2.groups: hellfish-elastic-spike
discovery.zen.ping.multicast.enabled: false
script.inline: on
script.indexed: on
END
# update sysconfig/elasticsearch memroy settings
if [ ! -f /etc/sysconfig/elasticsearch.orig ]; then
mv /etc/sysconfig/elasticsearch /etc/sysconfig/elasticsearch.orig
fi
# Memory should be 50% of available (up to 32g)
cat <<END > /etc/sysconfig/elasticsearch
ES_HEAP_SIZE=$(free -g | grep Mem | awk '{printf "%.0fg", $2 * .5}')
MAX_LOCKED_MEMORY=unlimited
END
service elasticsearch start
# setup elasticsearch to start after reboot
chkconfig --add elasticsearch
yum install -y sysstat || true
echo "Installation of elasticsearch completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment