使用Docker搭建单机版Elasticsearch
前言
如果只是为了开发测试,本地搭建一个单机ES是一个不错的选择,本文总结了如何搭建单机版的ES并测试。
环境:
centos 7.5
Docker version 18.09.7, build 2d0083d
elasticsearch : 7.12.1
搭建步骤
-
下载镜像
docker pull elasticsearch: 7.12.1
这里要注意,一定要指定具体版本。
-
准备基本环境
在宿主机创建 /opt/workspace/work/es/single/,路径根据自己的具体情况配置。export ES_SINGLE=/opt/workspace/work/es/single # es在宿主机的的配置文件 mkdir ${ES_SINGLE}/config/ # es在宿主机的数据存储目录 mkdir ${ES_SINGLE}/data/ vim ${ES_SINGLE}/config/es-single.yml
es-single.yml 写入以下内容,wq:
cluster.name: elasticsearch-single node.name: es-single-node-1 network.bind_host: 0.0.0.0 # 当前主机的地址 network.publish_host: 127.0.0.1 http.port: 9200 transport.tcp.port: 9300 http.cors.enabled: true http.cors.allow-origin: "*" node.master: true node.data: true bootstrap.system_call_filter: false cluster.initial_master_nodes: ["es-single-node-1"]
-
启动
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 -v ${ES_SINGLE}/config/es-single.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v ${ES_SINGLE}/data:/usr/share/elasticsearch/data --name es-single elasticsearch: 7.12.1
ES_JAVA_OPTS 参数指定了ES启动JVM的参数,默认好像是2G吧,所以本地测试调小点就行了。然后正常映射端口,映射路径
-
查看日志
# 查看容器日志 docker logs -f es-single
-
查看运行状态
docker ps
-
查看是否启动成功
# 请求 curl localhost:9200 # 或者浏览器访问 http://ip:9200
返回接过如下,说明正常:
{ "name": "es-single-node-1", "cluster_name": "elasticsearch-single", "cluster_uuid": "kMezbZwlRaKeuDxxxxxxxx", "version": { "number": "7.12.1", "build_flavor": "default", "build_type": "docker", "build_hash": "3186837139b9c6b6d23c32008706xxxxxxxxx", "build_date": "2021-04-20T20:56:39.040728659Z", "build_snapshot": false, "lucene_version": "8.8.0", "minimum_wire_compatibility_version": "6.8.0", "minimum_index_compatibility_version": "6.0.0-beta1" }, "tagline": "You Know, for Search" }
搭建过程的异常
-
Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes
表面上是说容器目录的权限不够,实际是宿主机的权限不够,给宿主机赋予权限。chmod -R 777 ${ES_SINGLE}
-
ERROR: [1] bootstrap checks failed
yml加入,注意es-single-node-1
指的是node.name的配置值,填错了也会报错。bootstrap.system_call_filter: false cluster.initial_master_nodes: ["es-single-node-1"]
-
ERROR: [1] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]- 解决方式增加如下内容
# 编辑以下配置 vi /etc/sysctl.conf # 添加以下内容 vm.max_map_count = 655360 # 查看 sysctl -p # 输出vm.max_map_count = 655360 说明配置正常
- 解决方式增加如下内容