How to install elasticsearch cluster on docker


Node 1 configuration

Modify max_map_count
echo vm.max_map_count=262144 >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf
Create folder
mkdir -p /home/docker/es/{data,conf,logs}
Modify folder permissions
chmod 777 /home/docker/es/data
chmod 777 /home/docker/es/logs
Open the temporary elasticsearch container
sudo docker run -dit --name=es elasticsearch:7.12.1 /bin/bash
Enter the container
sudo docker exec -it es /bin/bash
Generate ca
./bin/elasticsearch-certutil ca
Issue a certificate
./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
Exit the container
exit
Copy certificate
sudo docker cp es:/usr/share/elasticsearch/elastic-certificates.p12 /home/docker/es/conf
Modify permissions
chmod 777 /home/docker/es/conf/elastic-certificates.p12
Generate configuration file
echo 'cluster.name: "es-cluster"
network.host: 0.0.0.0
xpack.security.enabled: true
xpack.security.audit.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12' >> /home/docker/es/conf/elasticsearch.yml
Start the container
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 \
-v /home/docker/es/data:/usr/share/elasticsearch/data \
-v /home/docker/es/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /home/docker/es/conf/elastic-certificates.p12:/usr/share/elasticsearch/config/elastic-certificates.p12 \
-v /home/docker/es/logs:/usr/share/elasticsearch/logs \
-e "cluster.name=es-cluster" \
-e "network.publish_host=192.168.20.140" \
-e "discovery.seed_hosts=192.168.20.141" \
-e "cluster.initial_master_nodes=es-1,es-2" \
-e "node.name=es-1" \
-e ES_JAVA_OPTS="-Xms1g -Xmx1g" \
--add-host es-1:192.168.20.140 \
--add-host es-2:192.168.20.141 \
elasticsearch:7.12.1

Node 2 configuration


Modify max_map_count
echo vm.max_map_count=262144 >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf
Create folder
mkdir -p /home/docker/es/{data,conf,logs}
Set file permissions
chmod 777 /home/docker/es/data
chmod 777 /home/docker/es/logs
Copy node 1 certificate
scp root@192.168.20.140:/home/docker/es/conf/elastic-certificates.p12 /home/docker/es/conf/
Set certificate permissions
chmod 777 /home/docker/es/conf/elastic-certificates.p12
Generate configuration file
echo 'cluster.name: "es-cluster"
network.host: 0.0.0.0
xpack.security.enabled: true
xpack.security.audit.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12' >> /home/docker/es/conf/elasticsearch.yml
Start the container
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 \
-v /home/docker/es/data:/usr/share/elasticsearch/data \
-v /home/docker/es/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /home/docker/es/conf/elastic-certificates.p12:/usr/share/elasticsearch/config/elastic-certificates.p12 \
-v /home/docker/es/logs:/usr/share/elasticsearch/logs \
-e "cluster.name=es-cluster" \
-e "network.publish_host=192.168.20.141" \
-e "discovery.seed_hosts=192.168.20.140" \
-e "cluster.initial_master_nodes=es-1,es-2,es-3" \
-e "node.name=es-2" \
-e ES_JAVA_OPTS="-Xms1g -Xmx1g" \
--add-host es-1:192.168.20.140 \
--add-host es-2:192.168.20.141 \
elasticsearch:7.12.1
Enter node 1 or node 2 container
docker exec -it elasticsearch /bin/bash 
Generate password
./bin/elasticsearch-setup-passwords auto
Store generated password information
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
The passwords will be randomly generated and printed to the console.
Please confirm that you would like to continue [y/N]y


Changed password for user apm_system
PASSWORD apm_system = wn8H44hPmWQVzPuQXb8E

Changed password for user kibana_system
PASSWORD kibana_system = 8e9cjYbMO60ZiqYlmQPk

Changed password for user kibana
PASSWORD kibana = 8e9cjYbMO60ZiqYlmQPk

Changed password for user logstash_system
PASSWORD logstash_system = G9TYtAg7KVKeHn5FiIOk

Changed password for user beats_system
PASSWORD beats_system = L2sq6wjjmxlShbDSOaS5

Changed password for user remote_monitoring_user
PASSWORD remote_monitoring_user = CU7VN22jaAaVlC5QqueS

Changed password for user elastic
PASSWORD elastic = vSWGRIaXttTZ1olmvG61
You can view the cluster status through the browser
http://192.168.20.140:9200/_cat/nodes?v


Was this helpful?

I hope to help every developer quickly find the commands they need. Of course, you can also add your own commonly used commands for easy searching.