preface

Why ElasticSearch? Our applications often need to add Search capabilities, and open source Elastic Search is currently the preferred full-text Search engine. It can quickly store, search and analyze huge amounts of data. ElasticSearch is a distributed search framework that provides a RestfulAPI based on Lucene, ensures data security by using multiple shards (sharding), and provides automatic resharding. Elasticsearch: authoritative guide (in Chinese) : www.elastic.co/guide/cn/el…

Pull the ElasticSearch image

In the centos window, run the following command:

Docker pull elasticsearch: 5.6.8Copy the code

Current ES image version:

{ "name" : "WlwFyqU", "cluster_name" : "elasticsearch", "cluster_uuid" : "78UDZtviQqiWmzmenGpSrQ", "version" : {"number" : "5.6.8", "build_hash" : "cfe3d9f", "build_date" : "2018-09-10T20:12:43.732z ", "build_snapshot" : False, "lucene_version" : "6.6.1"}, "tagline" : "You Know, for Search"}Copy the code

Create the data to hang in the directory as well as configure the ElasticSearch cluster profile

1. Create a directory for mounting data files and open communication ports

In the centos window, perform the following operations:

[root@localhost soft]# pwd
/root/soft
[root@localhost soft]# mkdir -p ES/config
[root@localhost soft]# cd  ES 
[root@localhost ES]# mkdir data1
[root@localhost ES]# mkdir data2
[root@localhost ES]# mkdir data3
[root@localhost ES]# cd ES/config/
[root@localhost ES]# firewall-cmd --add-port=9300/tcp
success
[root@localhost ES]# firewall-cmd --add-port=9301/tcp
success
[root@localhost ES]# firewall-cmd --add-port=9302/tcp
success
Copy the code

Data1 datA2 data3 777 permission => chmod 777 data1 datA2 data3

2. Create ElasticSearch profile #

In the centos window, run the vim command to create es1.yml,es2.yml,es3.yml respectively

es1.yml

Cluster. name: my-elasticSearch node.name: es-node1 network.bind_host: 0.0.0.0 network.publish_host: 192.168.70.129 http.port: 9200 transport.tcp.port: 9300 http.cors.enabled: true http.cors.allow-origin: 192.168.70.129 Http. port: 9200 transport.tcp.port: 9300 http.cors.enabled: true http.cors.allow-origin: 192.168.70.129 "*" node.master: true node.data: true discovery.zen.ping.unicast.hosts: [" 192.168.70.129:9300 ", "192.168.70.129:9301", "192.168.70.129:9302]" discovery. Zen. Minimum_master_nodes: 2Copy the code

es2.yml

Cluster. name: my-elasticSearch node.name: es-node2 net. bind_host: 0.0.0.0 net. publish_host: my-elasticSearch node.name: es-node2 net. bind_host: 0.0.0.0 net. publish_host: 192.168.70.129 http.port: 9201 transport.tcp.port: 9301 http.cors.enabled: true http.cors.allow-origin: 192.168.70.129 "*" node.master: true node.data: true discovery.zen.ping.unicast.hosts: [" 192.168.70.129:9300 ", "192.168.70.129:9301", "192.168.70.129:9302]" discovery. Zen. Minimum_master_nodes: 2Copy the code

es3.yml

Cluster. name: my-elasticSearch node.name: es-node3 network.bind_host: 0.0.0.0 network.publish_host: 192.168.70.129 http.port: 9202 transport.tcp.port: 9302 http.cors.enabled: true http.cors.allow-origin: 192.168.70.129 Http. port: 9202 transport.tcp.port: 9302 http.cors.enabled: true http.cors.allow-origin: 192.168.70.129 "*" node.master: true node.data: true discovery.zen.ping.unicast.hosts: [" 192.168.70.129:9300 ", "192.168.70.129:9301", "192.168.70.129:9302]" discovery. Zen. Minimum_master_nodes: 2Copy the code

Note: The IP address of the local VM is 192.168.70.129

3. Increase the JVM thread limit

In the centos window, modify the sysctl.conf configuration

vim /etc/sysctl.conf
Copy the code

Add the following:

vm.max_map_count=262144 
Copy the code

Enable configuration:

sysctl -p
Copy the code

Note: This step is to prevent the following error when starting the container:

bootstrap checks failed max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]

Copy the code

Start the ElasticSearch cluster container

Start the ElasticSearch cluster container. In the centos window, run the following command:

docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 -v /root/soft/ES/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v / root/soft/ES/data1: / usr/share/elasticsearch/data - the name ES01 elasticsearch: 5.6.8 docker run - e ES_JAVA_OPTS = "- Xms256m -Xmx256m" -d -p 9201:9201 -p 9301:9301 -v /root/soft/ES/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml - v/root/soft/ES/data2: / usr/share/elasticsearch/data - the name ES02 elasticsearch: 5.6.8 docker run - e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9202:9202 -p 9302:9302 -v /root/soft/ES/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v / root/soft/ES/data3: / usr/share/elasticsearch/data - the name ES03 elasticsearch: 5.6.8Copy the code

Note: Settings – e ES_JAVA_OPTS = “- Xms256m – Xmx256m” because the/etc/elasticsearch/JVM options by default the JVM maximum minimum memory is 2 gb, readers after start the container Available docker stats command to view

Verify that the establishment is successful

Visit http://192.168.70.129:9200/_cat/nodes? in your browser’s address bar Pretty View node status

Note: The node name belt indicates the primary node *

Description of ElasticSearch configuration file

Cluster. name: my-elasticSearch node.name: es-node1 network.bind_host: 0.0.0.0 network.publish_host: 192.168.70.129 http.port: 9200 transport.tcp.port: 9300 http.cors.enabled: true http.cors.allow-origin: 192.168.70.129 Http. port: 9200 transport.tcp.port: 9300 http.cors.enabled: true http.cors.allow-origin: 192.168.70.129 "*" node.master: true node.data: true discovery.zen.ping.unicast.hosts: [" 192.168.70.129:9300 ", "192.168.70.129:9301", "192.168.70.129:9302]" discovery. Zen. Minimum_master_nodes: 2Copy the code

Parameter Description:

Cluster. name: uniquely identifies a cluster. Different clusters have different cluster.name and all nodes with the same cluster name automatically form a cluster. If no attribute is configured, the default value is elasticSearch. Node. name: specifies the node name. By default, a random name is specified in the name list. Number_of_shards node names must be unique in the cluster. Number_of_replicas: Sets the default number of replicas for each index. The default value is 1. Publish_host: sets the IP address that other nodes use to interact with this node. If this is not set, it will automatically judge. Port: set the HTTP port for external services. The default value is 9200 transport.tcp.port: set the TCP port for communication between nodes. Whether cross-domain REST requests are allowed http.cers. allow-origin: Where REST requests are allowed to come from Node. master: true Configures this node to be eligible to be elected as the primary node (candidate primary node) for processing requests and managing the cluster. If a node is not qualified to be a primary node, it will never be a primary node. If a node qualifies as a primary node, it becomes a primary node only after it has been recognized and elected as a primary node by other candidate primary nodes. Node. data: true Configures this node as a data node, which is used to save data and perform data-related operations (CRUD, Aggregation). Automatic discovery. Zen. Minimum_master_nodes: / / find the smallest number of the master node, if the master node in the cluster configuration came in less than this amount, es log will be submitted to the master node number. (The default value is 1.) To avoid split-brain, comply with this formula => (Totalnumber of master-eligible nodes / 2 + 1). * fissure refers to the main switch, due to inadequate switch or other reasons, led to the client and Slave mistake appear two active master, eventually making the entire cluster in a mess * discovery in zen. Ping. Unicast. Hosts: The IP addresses of each node in the cluster can also be named es-Node, which can be resolved by each nodeCopy the code

The last

I have compiled a Docker related information document and knowledge graph, Java systematic information, (including Java core knowledge points, interview topics and the latest Internet real questions in 20 years, e-books, etc.) friends who need to pay attention to the public number [procedure Yuan Xiao Wan] can obtain.