Elasticsearch is installed and started with Docker in the virtual machine, but the browser cannot access it

The process of learning springCloud and its pain, I have been following the dark horse video, but with the update of the technology stack, the configuration of many technologies will be different from the dark horse video, and then I will encounter some headaches, sometimes a It takes a long time to find the cause and solution of the problem, so record these problems so as not to forget (since the problem was solved during the trial process, there are no screenshots, but it is still detailed).

question:

When I learned about Elasticsearch, I used docker to install and start Elasticsearch. The initial process went smoothly, but something went wrong when I used the browser to access it. I couldn’t access it anyway, and it prompted access denied.

Solution process:

1. Firewall problem:

Most of the Internet is talking about the problem of firewalls, but when I first learned docker, I turned off the firewall and prohibited booting, and provide the following commands for those who need it:

firewall-cmd --state # 查看防火墙状态
systemctl stop firewalld.service # 停止firewall
systemctl disable firewalld.service # 禁止firewall开机启动
reboot # 重启虚拟机

2.max_map_count is too small:

The second argument is that max_map_count is too small, but the problem is still not solved after I modified it. The command is as follows:

First check the max_map_count value (usually 65530, but if it is 262144, you don’t need to change it):

cat /proc/sys/vm/max_map_count
65530

Modify 65530 to 262144:

#临时修改
sysctl -w vm.max_map_count=262144

#永久修改
vm.max_map_count=262144​

3. The memory of the virtual machine is not enough to allocate to ES:

There is also a saying that ES takes up a lot of memory. If the virtual machine memory is not enough to allocate to ES, it will cause startup failure. The solution:

#查看ES的容器id:
docker ps -a
 
#删除ES容器:
docker rm + 容器id

#新建ES容器(重点加上-e "ES_JAVA_OPTS=-Xms512m -Xmx512m"):
docker run -d \
--name es \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-v es-plugins:/usr/share/elasticsearch/plugins \
-v /path/to/data/dir:/usr/share/elasticsearch/data \
--network es-net \
--privileged \
-p 9200:9200 \
-p 9300:9300 \
elasticsearch:7.12.1

This is mentioned in the dark horse video, so I added it too, it doesn't help my problem.

Positive solution: Mount point directory problem:

1. Check the log:

When I was looking for a way, I stumbled upon this command to view the logs of Elasticsearch:

# 查看最新日志(默认情况下使用-f选项)
docker logs -f +容器id或者镜像名

# 查看特定时间段内的日志:
docker logs --since 2022-01-01 +容器id或者镜像名

# 仅查看错误日志:
docker logs --since 1d --grep ERROR +容器id或者镜像名

Then I checked my logs and found that when my browser accesses ip:9200, this error will appear and my container will be automatically deleted at this time:

ElasticsearchException[failed to bind service]; nested: FileSystemException[/usr/share/elasticsearch/data/nodes/0: Not a directory];
Likely root cause: java.nio.file.FileSystemException: /usr/share/elasticsearch/data/nodes/0: Not a directory

2. Try to fix:

The general meaning is that my mount directory does not exist, but after I create the directory separately, an error will still be reported:


uncaught exception in thread [main]
ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];
Likely root cause: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes

Then I did a series of methods I could think of for the directory, but none of them solved the problem.

3. Correct method:

In desperation, I can only completely delete the image and container of Elasticsearch, and reinstall, but create a mount point file directory before installing!

Here are the complete steps to install Elasticsearch for docker:

(1) Create a network for later deployment of kibana:

docker network create es-net

(2) docker pulls Elasticsearch, I don’t know why when pulling Elasticsearch, you must add tag, you can’t use latest directly:

# 必须选取一个tag,以7.12.1为例:
docker pull elasticsearch:7.12.1

(3) Create a loading directory (important!!! Many tutorials do not)

mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data

# 将http.host: 0.0.0.0写入到es配置文件中,代表能被远程的任何机器访问:
echo "http.host: 0.0.0.0" > /mydata/elasticsearch/config/elasticsearch.yml

(4) Create a container:

docker run -d \
--name es \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-v es-plugins:/usr/share/elasticsearch/plugins \
-v /path/to/data/dir:/usr/share/elasticsearch/data \
--network es-net \
--privileged \
-p 9200:9200 \
-p 9300:9300 \
elasticsearch:7.12.1

explain:

-e "cluster.name=es-docker-cluster": set the cluster name

-e "http.host=0.0.0.0": the listening address, which can be accessed from the external network

-e "ES_JAVA_OPTS=-Xms512m -Xmx512m": memory size

-e "discovery.type=single-node": non-cluster mode

-v es-data:/usr/share/elasticsearch/data: Mount the logical volume, bind the data directory of es

-v es-plugins:/usr/share/elasticsearch/plugins: Mount the logical volume, bind the plugin directory of es

--privileged: Grant logical volume access

--network es-net : Join a network named es-net

-p 9200:9200: Port mapping configuration
(5) Browser access: virtual machine ip:9200, the following page appears to indicate successful access (the edge browser appears in json format):

 problem solved! ! !

Summarize:

The main problem this time is that the mount point directory was not prepared in advance. In fact, the dark horse video and many tutorials did not have this step, so it took a lot of detours and took a long time. Another reason is: I did not check in time Logs, in fact, the first thing a programmer should do when encountering an error is to check the logs, and I first searched for the answer blindly, which can be regarded as a long memory.

The log error may be different from mine. You can post it in the comment area. I will answer as long as I know it.

Guess you like

Origin blog.csdn.net/m0_56680022/article/details/129911541