background

[bug Mc-10841] – If the server is down or hacked, our index storage and query services will be unavailable. To avoid this sad story, we need to increase the high availability of our ElasticSearch environment. Two words, “cluster”. Create a cluster environment for ElasticSearch.

The principle of

Elasticsearch stores data one index at a time. An index is equivalent to a database. When creating an index, fragmentation is configured for the index. Indexes are not stored together, but are stored in fragments. By default, they are divided into five fragments (you can customize the amount of data in the fragments). In a cluster environment, fragments are evenly distributed among nodes according to the algorithm as far as possible. In this way, a piece of data is divided into multiple pieces and stored on different servers. Elasticsearch creates a shard on one server, which is called the original shard. By default, elasticSearch generates a copy of the shard on another host. We can still find the corresponding data from a copy of the other host. So from the outside, the data doesn’t make any difference.

Cluster installation

Install multiple ElasticSearches

How to install ElasticSearch on a standalone server

Elasticsearch (part 1

You can install elasticSearch on a single server using different ports.

In a production environment, you are advised to install it on multiple servers to improve fault tolerance and high availability. Because sharding and replicas are meaningless on a single server, and if one host goes down, they all go down.

Modify the configuration

Elasticsearch – 6.6.2 / config/elasticsearch. Yml

Master node configuration file:

Cluster. name: my-es node.name: node0 node.master: true network.host: 0.0.0.0 http.port: 9200 transport.tcp.port: 9300 discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300","127.0.0.1:9301"," 127.0.0.1:9302 "] discovery.zen.minimum_master_nodes: 2 http.cers.enabled: true http.cors.allow-origin: "*"Copy the code

Working node configuration file:

Cluster. name: my-es node.name: node1 node.master: false network.host: 0.0.0.0 http.port: 9200 transport.tcp.port: 9301 discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300","127.0.0.1:9301"," 127.0.0.1:9302 "] discovery.zen.minimum_master_nodes: 2 http.cers.enabled: true http.cors.allow-origin: "*"Copy the code
Cluster. name: my-es node.name: node2 node.master: false network.host: 0.0.0.0 http.port: 9200 transport.tcp.port: 9302 discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300","127.0.0.1:9301"," 127.0.0.1:9302 "] discovery.zen.minimum_master_nodes: 2 http.cers.enabled: true http.cors.allow-origin: "*"Copy the code

Problems you might encounter

[node0] failed to send join request to master

Solution: Delete the file under elasticSearch -6.6.2/data

Start the cluster

Start all three nodes separately:

bin/elasticsearch -d

Cluster Health Check

http://cluster address :9200/_cat/health? V

(Green: normal; Yellow: abnormal; Red: error)