Step-by-Step Guide to Deploying an Elasticsearch Cluster with Docker on CentOS
This article provides a comprehensive tutorial on why to use Elasticsearch, how to pull the Docker image, set up data directories, configure cluster and node settings, adjust system limits, launch three Elasticsearch containers, and verify the cluster using REST APIs and the elasticsearch‑head UI.
Why use Elasticsearch? The article begins by explaining that Elasticsearch, built on Lucene, is a distributed search engine offering fast storage, search, and analysis of massive data, with RESTful APIs, sharding, and automatic resharding.
1. Pull the Elasticsearch Docker Image
Run the following command in a CentOS terminal: docker pull elasticsearch:5.6.8 The pulled image version information is displayed as JSON.
2. Create Data Directories and Open Ports
Execute the following commands to create mount points and open communication ports:
[root@localhost soft]# pwd
/home/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
successNote: If using ELK 6.x, set 777 permissions on data directories.
3. Create Elasticsearch Configuration Files
Using vim, create three YAML files (es1.yml, es2.yml, es3.yml) with the following contents:
es1.yml
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 192.168.9.219
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: ["192.168.9.219:9300","192.168.9.219:9301","192.168.9.219:9302"]
discovery.zen.minimum_master_nodes: 2es2.yml
cluster.name: elasticsearch-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 192.168.9.219
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: ["192.168.9.219:9300","192.168.9.219:9301","192.168.9.219:9302"]
discovery.zen.minimum_master_nodes: 2es3.yml
cluster.name: elasticsearch-cluster
node.name: es-node3
network.bind_host: 0.0.0.0
network.publish_host: 192.168.9.219
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: ["192.168.9.219:9300","192.168.9.219:9301","192.168.9.219:9302"]
discovery.zen.minimum_master_nodes: 2Replace the IP address with the actual host IP if needed.
4. Increase JVM Thread Limits
Edit /etc/sysctl.conf and add: vm.max_map_count=262144 Apply the change: sysctl -p This prevents the "bootstrap checks failed max virtual memory areas" error when starting containers.
5. Launch Elasticsearch Cluster Containers
Run the following Docker commands:
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 -v /home/soft/ES/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/soft/ES/data1:/usr/share/elasticsearch/data --name ES01 elasticsearch:5.6.8
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9201 -p 9301:9301 -v /home/soft/ES/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/soft/ES/data2:/usr/share/elasticsearch/data --name ES02 elasticsearch:5.6.8
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9202:9202 -p 9302:9302 -v /home/soft/ES/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/soft/ES/data3:/usr/share/elasticsearch/data --name ES03 elasticsearch:5.6.8The -e ES_JAVA_OPTS="-Xms256m -Xmx256m" option reduces the default 2 GB JVM heap to 256 MB, which can be verified with docker stats.
6. Verify the Cluster
Check node status via a browser: http://192.168.9.219:9200/_cat/nodes?pretty Use the elasticsearch-head UI for a visual overview:
docker pull mobz/elasticsearch-head:5
docker run -d -p 9100:9100 --name es-manager mobz/elasticsearch-head:5Then open http://192.168.9.219:9100/ in a browser.
7. Additional Settings and Tips
Adjust index settings (shards, replicas) via the REST API:
curl -XPUT 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{"index.number_of_replicas":"1","index.number_of_shards":"10"}'Key configuration parameters explained: cluster.name: unique identifier for the cluster. node.name: unique node name. index.number_of_shards: default number of primary shards (default 5). index.number_of_replicas: default number of replica shards (default 1). network.bind_host / network.publish_host: IP addresses for binding and publishing; can be replaced by network.host. http.port and transport.tcp.port: ports for HTTP and inter‑node communication. http.cors.enabled and http.cors.allow-origin: enable CORS for REST calls. node.master and node.data: designate master‑eligible and data nodes. discovery.zen.minimum_master_nodes: prevents split‑brain scenarios (formula: floor(total_master_nodes/2)+1). discovery.zen.ping.unicast.hosts: list of node IPs for cluster discovery.
Appendix
Useful commands:
View container memory usage: docker stats $(docker ps --format={{.Names}}) View container logs: docker logs <em>container_name_or_id</em> Inspect Elasticsearch configuration file entries (cluster name, node name, network settings, etc.).
The article concludes with a reminder to adjust shard and replica settings according to workload and to consider memory‑lock settings for production stability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
