Elasticsearch tuning
This guide summarizes the relevant settings that enable Elasticsearch optimization.
Change users' password
Changing the default passwords of Elasticsearch is highly recommended in order to improve security.
All the initial users and roles for Open Distro for Elasticsearch are located in the file /usr/share/elasticsearch/plugins/opendistro_security/securityconfig/internal_users.yml
. We will create a backup of the security configuration and modify the resulting internal_users.yml
file to avoid losing custom security configurations.
Make a backup of your security configuration using the
securityadmin
script placed at/usr/share/elasticsearch/plugins/opendistro_security/tools
. Replace<elasticsearch_ip>
and<backup-directory>
and execute the following commands:# cd /usr/share/elasticsearch/plugins/opendistro_security/tools/ # ./securityadmin.sh -backup <backup-directory> -nhnv -cacert /etc/elasticsearch/certs/root-ca.pem -cert /etc/elasticsearch/certs/admin.pem -key /etc/elasticsearch/certs/admin.key -icl -h <elasticsearch_ip>
To generate a new password hash, Open Distro for Elasticsearch offers an utility called
hash.sh
located at/usr/share/elasticsearch/plugins/opendistro_security/tools
. Replace<new-password>
with the chosen new password and generate a hash for it using thehash.sh
utility:# bash /usr/share/elasticsearch/plugins/opendistro_security/tools/hash.sh -p <new-password>
The generated hash must be placed on the hash section for the user whose password you want to change, for example
admin
, in<backup-directory>/internal_users.yml
:admin: hash: "<newly_generated_hash>" reserved: true backend_roles: - "admin" description: "Demo admin user"
In order to load the changes made, it is necessary to execute the
securityadmin
script to push the modifiedinternal_users.yml
file. Replace<elasticsearch_ip>
and<backup-directory>
and execute the following commands:# cd /usr/share/elasticsearch/plugins/opendistro_security/tools/ # ./securityadmin.sh -f <backup-directory>/internal_users.yml -t internalusers -nhnv -cacert /etc/elasticsearch/certs/root-ca.pem -cert /etc/elasticsearch/certs/admin.pem -key /etc/elasticsearch/certs/admin.key -icl -h <elasticsearch_ip>
Remove files from your
<backup-directory>
.
Note
The password may need to be updated in /etc/filebeat/filebeat.yml
and /etc/kibana/kibana.yml
During the installation of Elasticsearch, the passwords for the different users were automatically generated. These passwords can be changed afterwards using API requests. Replace the following variables and execute the corresponding API call:
<elasticsearch_ip>
: The IP of the Elasticsearch node.
<username>
: The name of the user whose password is going to be changed.
<user_password>
: Current user's password.
<new_password>
: The new password that will be assigned to the<username>
user.
# curl -k -X POST -u <username>:<user_password> "https://<elasticsearch_ip>:9200/_security/user/<username>/_password?pretty" -H 'Content-Type: application/json' -d '
# {
# "password" : "<new_password>"
# }
# '
If the call was successful it returns an empty JSON structure { }
.
Note
The password may need to be updated in /etc/filebeat/filebeat.yml
and /etc/kibana/kibana.yml
.
Memory locking
Elasticsearch malfunctions when the system is swapping memory. It is crucial for the health of the node that none of the JVM is ever swapped out to disk. The following steps show how to set the bootstrap.memory_lock
setting to true so Elasticsearch will lock the process address space into RAM. This prevents any Elasticsearch memory from being swapped out.
Set
bootstrap.memory_lock
:Uncomment or add this line to the
/etc/elasticsearch/elasticsearch.yml
file:bootstrap.memory_lock: true
Edit the limit of system resources:
Where to configure system settings depends on which package and operating system used for the Elasticsearch installation.
In a case where systemd is used, system limits need to be specified via systemd. To do this, create the folder executing the command:
# mkdir -p /etc/systemd/system/elasticsearch.service.d/
Then, in the new directory, add a file called
elasticsearch.conf
and specify any changes in that file:# cat > /etc/systemd/system/elasticsearch.service.d/elasticsearch.conf << EOF [Service] LimitMEMLOCK=infinity EOF
Edit the proper file
/etc/sysconfig/elasticsearch
for RPM or/etc/default/elasticsearch
for Debian:MAX_LOCKED_MEMORY=unlimited
Limit memory:
The previous configuration might cause node instability or even node death with an
OutOfMemory
exception if Elasticsearch tries to allocate more memory than is available. JVM heap limits will help limit memory usage and prevent this situation. Two rules must be applied when setting Elasticsearch's heap size:Use no more than 50% of available RAM.
Use no more than 32 GB.
It is also important to consider the memory usage of the operating system, services and software running on the host. By default, Elasticsearch is configured with a heap of 1 GB. It can be changed via JVM flags using the
/etc/elasticsearch/jvm.options
file:# Xms represents the initial size of total heap space # Xmx represents the maximum size of total heap space -Xms4g -Xmx4g
Warning
The values min
(Xms)
and max(Xmx)
sizes must be the same to prevent JVM heap resizing at runtime as this is a very costly process.Restart Elasticsearch:
# systemctl daemon-reload
# systemctl restart elasticsearch
# service elasticsearch restart
After starting Elasticsearch, run the following request to verify that the setting was successfully changed by checking the value of mlockall
:
# curl "http://localhost:9200/_nodes?filter_path=**.mlockall&pretty"
{
"nodes" : {
"sRuGbIQRRfC54wzwIHjJWQ" : {
"process" : {
"mlockall" : true
}
}
}
}
If the output of the "mlockall"
field is false, the request has failed. In addition, the following line will appear in /var/log/elasticsearch/elasticsearch.log
:
Unable to lock JVM Memory
References: