Create two Elasticsearch hosts in Docker

I tried creating two elasticsearch 8 virtual machines in Docker, but I end up having two problems:

  • One of the machines always stops
  • The server always has a high CPU consumption

I did it this way:

docker pull docker.elastic.co/elasticsearch/elasticsearch:8.2.3

docker network create elastic
docker run --name es01 --net elastic -p 9200:9200 -p 9300:9300 -it docker.elastic.co/elasticsearch/elasticsearch:8.2.3

docker network create elastic02
docker run --name es02 --net elastic02 -p 9201:9200 -p 9301:9300 -it docker.elastic.co/elasticsearch/elasticsearch:8.2.3

As soon as I start the second docker run command the CPU goes from a Load Average of 0.2 to 30.00

And this is not a production environment, there are only 4 people using it.

First why such an older version?

What are your Docker resources? If Docker resources are set to unlimited on the machine, both containers will try to claim 50% of the host memory and will most likely fail.

Peraps look at the docs here that describe exactly what you are seeing

BTW those containers will each start as their own elasticsearch cluster... not as part of a single cluster..

So perhaps you should use docker compose... that is in the docs as well...

1 Like

Edit: I would definitely check out the link Stephen shared for best practices. My post is just an expansion of what you have to get you up and running.

Hello, there's a couple things to try that might get both ES running. I can verify that your current docker commands won't work for what you want.

  • Both containers must be on the same Docker network (elastic) to communicate.
  • Set discovery.seed_hosts to point to the other nodes in the cluster.
  • Define cluster.initial_master_nodes to include all master-eligible nodes when starting a new cluster.
  • You can disable security features for a non-production environment by setting xpack.security.enabled=false. (Not recommended for production.)
  • If you prefer to keep security enabled, you need to configure SSL certificates and authentication settings as per the Elasticsearch security documentation.
    -Set the ES_JAVA_OPTS environment variable to allocate less memory. Documentation here
# Create a single network for both containers
docker network create elastic

# Start the first node
docker run --name es01 --net elastic -p 9200:9200 -p 9300:9300 \
  -e "node.name=es01" \
  -e "cluster.name=es-docker-cluster" \
  -e "discovery.seed_hosts=es02" \
  -e "cluster.initial_master_nodes=es01,es02" \
  -e "xpack.security.enabled=false" \
  -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
  -it docker.elastic.co/elasticsearch/elasticsearch:8.2.3

# Start the second node
docker run --name es02 --net elastic -p 9201:9200 -p 9301:9300 \
  -e "node.name=es02" \
  -e "cluster.name=es-docker-cluster" \
  -e "discovery.seed_hosts=es01" \
  -e "cluster.initial_master_nodes=es01,es02" \
  -e "xpack.security.enabled=false" \
  -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
  -it docker.elastic.co/elasticsearch/elasticsearch:8.2.3

Then run a quick health status check of your cluster:

curl -X GET "localhost:9200/_cluster/health?pretty"

I was able to get two instances running and verified on my end. Let me know if this works!

2 Likes

That was the problem. Restricting RAM works.

1 Like