Skip to content

Instantly share code, notes, and snippets.

@CameronHall
Last active November 9, 2023 04:16
Show Gist options
  • Save CameronHall/c8e58d10dc513a6eb7bdd7d337dfb318 to your computer and use it in GitHub Desktop.
Save CameronHall/c8e58d10dc513a6eb7bdd7d337dfb318 to your computer and use it in GitHub Desktop.
Elasticsearch Config
#!/bin/sh
set -e;
function es_uninstall {
yum remove java-1.8.0-amazon-corretto -y
rpm -e `rpm -qa elasticsearch`
}
function es_install {
# Set Environment Variables
export IMDSv2_TOKEN=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`;
export AWS_REGION=`curl -s -H "X-aws-ec2-metadata-token: ${IMDSv2_TOKEN}" http://169.254.169.254/latest/meta-data/placement/region/`;
export AWS_AVAILABILITY_ZONE=`curl -s -H "X-aws-ec2-metadata-token: ${IMDSv2_TOKEN}" http://169.254.169.254/latest/meta-data/placement/availability-zone/`;
if [[ -z "$AWS_REGION" || -z "$AWS_AVAILABILITY_ZONE" ]]; then
echo "Failed to fetch AWS Region and Availability Zone from IMDSv2."
exit 1
fi
# Install dependency
yum install java-1.8.0-amazon-corretto -y
# Download and extract Elasticsearch
curl -s -o elasticsearch.rpm "https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.4.6/elasticsearch-2.4.6.rpm"
ELASTICSEARCH_SHA1=`curl -s https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.4.6/elasticsearch-2.4.6.rpm.sha1` &&
echo "$ELASTICSEARCH_SHA1 elasticsearch.rpm" | sha1sum -c -
rpm -i elasticsearch.rpm
rm -f elasticsearch.rpm
export ES_CFG=/etc/elasticsearch/elasticsearch.yml
# Pull and inject AZ variables into Elasticsearch config
curl -s -o $ES_CFG https://gist.githubusercontent.com/CameronHall/c8e58d10dc513a6eb7bdd7d337dfb318/raw/elasticsearch.yml
sed -i "s/{{aws_region}}/$AWS_REGION/g" $ES_CFG
sed -i "s/{{aws_availability_zone}}/$AWS_AVAILABILITY_ZONE/g" $ES_CFG
sed -i 's/{{cluster_name}}/absinthe-elasticsearch/g' $ES_CFG
# Install Elasticsearch AWS plugin
yes | /usr/share/elasticsearch/bin/plugin install cloud-aws
# Set the ES_HEAP_SIZE to 50% of the system memory and write to /etc/sysconfig/elasticsearch
memory_info=$(free -m)
total_memory=$(echo "$memory_info" | awk 'FNR == 2 {print $2}')
echo "ES_HEAP_SIZE=$((total_memory / 2))m" >> /etc/sysconfig/elasticsearch
# Update PID path for systemctl
systemctl enable elasticsearch.service
sed -i 's/\/var\/run/\/run/g' /etc/systemd/system/multi-user.target.wants/elasticsearch.service
systemctl daemon-reload
systemctl restart elasticsearch
}
case "$1" in
install)
es_install
;;
uninstall)
es_uninstall
;;
*)
echo "This script will install and configure Elasticsearch 2.4
One of the following arguments must be provided;
- install
- uninstall
"
;;
esac
---
node.awszone: {{aws_availability_zone}}
index.number_of_replicas: 2
cloud:
aws:
region: {{aws_region}}
cluster:
name: {{cluster_name}}
routing.allocation.awareness.attributes: awszone
discovery:
ec2:
tag:
role: search
type: ec2
zen:
minimum_master_nodes: 3
ping:
multicast:
enabled: false
path:
data: /var/lib/elasticsearch
network.host: 0.0.0.0
http.port: 9200
plugin:
mandatory: cloud-aws
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment