CentOS 7 uses docker to build elastic search cluster

Original text: https://hjxlog.com/posts/20220215221548.html

1, Foreword

I bought Tencent cloud server a few days ago. I happened to be learning elasticsearch recently, so I wanted to build an es cluster to play. According to the keywords, I searched several blogs on Baidu and found that the content was surprisingly similar. I had a hunch that things were not simple.

Sure enough! No blog can be completely installed. I turned to the official documents to install the official image. My English is not good. I feel uncomfortable and quit. After a afternoon's exploration, we now record the complete installation process.

It would be great if I could help you! If not, please continue to explore, thank you!

2, Server

Server environment: centos7 6, 2-core 4GB

Description: new server environment, install from scratch.

3, Environmental installation

3.1 installing docker

Use the official installation script to install automatically. The installation command is as follows:

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

After successful installation, start the docker service:

service docker start

3.2 pull elasticsearch 7.6.2 image

docker pull elasticsearch:7.6.2

4, Configure elasticsearch

4.1 create directory

cd /
mkdir docker
mkdir /docker/ES
cd /docker/ES
mkdir data1 data2 data3 config

4.2 creating profiles

Create a configuration file in the config folder:

cd /docker/ES/config/
touch es1.yml es2.yml es3.yml

4.3 editing yml files

es1.yml

#ES01 configuration
cluster.name: elasticsearch-cluster  # Cluster name
node.name: es-node-01  # Node name
network.bind_host: 0.0.0.0
network.publish_host: xxx.xxx.xx.xxx  # Your server's extranet ip address is XXX below xxx. xx. XXX means the same
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["xxx.xxx.xx.xxx:9300","xxx.xxx.xx.xxx:9301","xxx.xxx.xx.xxx:9302"]
discovery.zen.minimum_master_nodes: 2
cluster.initial_master_nodes: es-node-01  # Initial node

es2.yml

#ES02 configuration
cluster.name: elasticsearch-cluster
node.name: es-node-02
network.bind_host: 0.0.0.0
network.publish_host: xxx.xxx.xx.xxx
http.port: 9201
transport.tcp.port: 9301
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["xxx.xxx.xx.xxx:9300","xxx.xxx.xx.xxx:9301","xxx.xxx.xx.xxx:9302"]
discovery.zen.minimum_master_nodes: 2
cluster.initial_master_nodes: es-node-01

es3.yml

#ES03 configuration
cluster.name: elasticsearch-cluster
node.name: es-node-03
network.bind_host: 0.0.0.0
network.publish_host: 175.178.87.185
http.port: 9202
transport.tcp.port: 9302
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["xxx.xxx.xx.xxx:9300","xxx.xxx.xx.xxx:9301","xxx.xxx.xx.xxx:9302"]
discovery.zen.minimum_master_nodes: 2
cluster.initial_master_nodes: es-node-01

4.4 add firewall

firewall-cmd --add-port=9300/tcp
firewall-cmd --add-port=9301/tcp
firewall-cmd --add-port=9302/tcp

4.5 modify folder permissions

chmod 777 data1 data2 data3

5, Start container

5.1 start command

docker run -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -p 9200:9200 -p 9300:9300 -v /docker/ES/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml --name ES01 -d elasticsearch:7.6.2

docker run -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -p 9201:9201 -p 9301:9301 -v /docker/ES/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml --name ES02 -d elasticsearch:7.6.2

docker run -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -p 9202:9202 -p 9302:9302 -v /docker/ES/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml --name ES03 -d elasticsearch:7.6.2

5.2 problems found

When following the steps on the Internet, I found the problem when I came here. Through docker ps -a, I found that the es container did not start successfully!

Through the docker command docker logs ES01, we can see that the virtual machine memory is too small!

resolvent:

To modify a profile:

vi /etc/sysctl.conf

Add:

vm.max_map_count=262144

Save and exit.

Execute / sbin/sysctl -p with immediate effect.

Delete the previous container and execute it again. I thought it was OK at this time. Unexpectedly, an IPv4 network error was reported:

WARNING: IPv4 forwarding is disabled. Networking will not work.

resolvent:

To modify a profile:

vim /usr/lib/sysctl.d/00-system.conf

Add:

net.ipv4.ip_forward=1

Restart the network (if not, try restarting the server):

systemctl restart network

Delete the wrong docker container and re execute the three docker run commands.

6, elasticsearch cluster

6.1 check whether the startup is successful

curl 127.0.0.1:9200

6.2 view cluster health status:

curl '127.0.0.1:9200/_cat/health?pretty=true'

6.3 elasticsearch head plug-in

Open the 9200 port on the server and install the elasticsearch head plug-in in the chrome browser to view the data:

complete!

7, Summary

  1. The server does not need to install the Java environment, but the related environment in the es image of docker.
  2. If you find any problems, go to the log at the first time.
  3. Only by groping through difficulties can we grow faster.

reference material

  1. https://www.cnblogs.com/knownfreestyle/p/13225108.html
  2. https://blog.csdn.net/xcc_2269861428/article/details/100186654
  3. https://blog.csdn.net/ezbuy/article/details/82462952

Keywords: Operation & Maintenance Docker ElasticSearch

Added by ahzulfi on Wed, 16 Feb 2022 04:10:01 +0200