Docker 安装 ElasticSearch
获取镜像(elasticsearch:7.16.3)
# 拉取镜像
docker pull elasticsearch:7.16.3
# 查看本地镜像
docker images
启动容器
启动参数:
-d 后台启动运行 --name 容器名称 restart=always docker启动后保持运行状态 -p 9200:9200 http访问端口映射 -p 9300:9300 服务间访问端口映射 -e ES_JAVA_OPTS="-Xms512m -Xmx512m" JVM内存大小 -e "discovery.type=single-node" 单节点启动 -v 指定挂载目录,需准备好挂载路径:`/gonait/docker-mount/elastic-search`,并需要指定文件夹读写权限`chmod -R 777 elastic-search` elasticsearch:7.16.3 指定启动镜像
首次启动不指定挂载目录
docker run -d \
--name=elastic-search \
--restart=always \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-p 9200:9200 \
-p 9300:9300 \
elasticsearch:7.16.3
# 查看容器是否启动成功
docker ps | grep elastic-search
# 进入容器查看
docker exec -it elastic-search /bin/bash
# 复制容器中的配置文件
docker cp -a elastic-search:/usr/share/elasticsearch/config /gonait/docker-mount/elastic-search/config
# 修改配置文件:/gonait/docker-mount/elastic-search/config/elasticsearch.yml
vim elasticsearch.yml
# elasticsearch.yml 配置文件
cluster.name: "elastic-search"
network.host: 0.0.0.0
#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically
# generated to configure Elasticsearch security features on 16-10-2023 14:13:22
#
# --------------------------------------------------------------------------------
# Enable security features
# xpack.security.enabled: true 改为 xpack.security.enabled: false,这样就可以直接 使用http访问,并且不>需要账号密码鉴权,这个设置看个人情况,如果是生产环境建议开始开启 https和账号密码鉴权
# xpack.security.enabled: true
xpack.security.enabled: false
xpack.security.enrollment.enabled: true
# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
enabled: true
keystore.path: certs/http.p12
# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
enabled: true
verification_mode: certificate
keystore.path: certs/transport.p12
truststore.path: certs/transport.p12
#----------------------- END SECURITY AUTO CONFIGURATION -------------------------
http.host: 0.0.0.0
path.repo: /usr/share/elasticsearch/data/elastic-backup
# 强制删除正在运行的容器
docker rm -f elastic-search
再次启动指定挂载目录
docker run -d \
--name=elastic-search \
--restart=always \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-p 9200:9200 \
-p 9300:9300 \
-v /gonait/docker-mount/elastic-search/data:/usr/share/elasticsearch/data \
-v /gonait/docker-mount/elastic-search/plugins:/usr/share/elasticsearch/plugins \
-v /gonait/docker-mount/elastic-search/config:/usr/share/elasticsearch/config \
-v /gonait/docker-mount/elastic-search/logs:/usr/share/elasticsearch/logs \
elasticsearch:7.16.3
查看容器是否启动成功
# 宿主机浏览器访问
http://{linux_ip}:9200/
# 正常响应结果
{
"name": "df8caecf562d",
"cluster_name": "docker-cluster",
"cluster_uuid": "wiwctpzhTtu0F3LWsCyinA",
"version": {
"number": "7.16.3",
"build_flavor": "default",
"build_type": "docker",
"build_hash": "4e6e4eab2297e949ec994e688dad46290d018022",
"build_date": "2022-01-06T23:43:02.825887787Z",
"build_snapshot": false,
"lucene_version": "8.10.1",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}
访问失败可能原因:
- 查看docker容器是否启动成功,
docker logs elastic-search
,如果出现以下报错日志,说明文件夹logs
读写权限没配置,chmod -R 777 elastic-search
[0.001s][error][logging] Error opening log file 'logs/gc.log': Permission denied [0.001s][error][logging] Initialization of output 'file=logs/gc.log' using options 'filecount=32,filesize=64m' failed. error: Invalid -Xlog option '-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m', see error log for details. Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. at org.elasticsearch.tools.launchers.JvmOption.flagsFinal(JvmOption.java:119) at org.elasticsearch.tools.launchers.JvmOption.findFinalOptions(JvmOption.java:81) at org.elasticsearch.tools.launchers.JvmErgonomics.choose(JvmErgonomics.java:38) at org.elasticsearch.tools.launchers.JvmOptionsParser.jvmOptions(JvmOptionsParser.java:135) at org.elasticsearch.tools.launchers.JvmOptionsParser.main(JvmOptionsParser.java:86)
- 查看是否关闭防火墙:
systemctl status firewalld
,如果不是以下状态,则需要关闭系统防火墙,systemctl stop firewalld
[root@easy-cloud-node-206 config]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1)
- 其他可能情况,需结合实际查看相应解决办法
安装elastic-search插件及其他相关组件
- ik分词插件:
- 下载地址:elasticsearch-analysis-ik-7.16.3
- 更多版本官网地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
- 将zip压缩包解压至plugins的ik目录下
# 切换到指定目录 cd /gonait/docker-mount/elastic-search/plugins/ik # 解压文件 unzip elasticsearch-analysis-ik-7.16.3.zip # 删除压缩包 rm -f elasticsearch-analysis-ik-7.16.3.zip
- 可视化操作工具:kibana
【Docker】docker 安装 kibana - 数据导入导出工具:elasticdump
【Docker】docker 安装 elasticdump