全文检索示例:基于docker搭建Elasticsearch环境(2)

简介

本文介绍如何用自己构建的elasticsearch docker镜像搭建Elasticsearch环境,由于我的电脑配置不太好,因此搭建单节点环境。

  • 环境介绍
    系统:centos7.6 ,IP:192.168.1.14
软件 版本
docker 1.3.1
docker-compose 1.18.0
自建elasticsearch镜像 6.8.3-ik
ik插件 6.8.3

系统设置

max_map_count

ES使用mmapfs来保存索引,系统默认的mmap数太小,可能引发内存溢出

# root用户操作
# 编辑配置文件
vim /etc/sysctl.conf
# 添加以下内容
vm.max_map_count = 262144

# 加载配置文件
sysctl -p
# 验证配置是否生效
sysctl vm.max_map_count

编写compose文件

# 创建存放docker-compose.yml文件的目录
[root@localhost ~]# mkdir -vp /root/docker-compose/elasticsearch
mkdir: 已创建目录 "/root/docker-compose/elasticsearch"
# 创建并编辑docker-compose.yml文件
vim /root/docker-compose/elasticsearch/docker-compose.yml

docker-compose.yml文件内容如下

version: '2.2'
services:
  es:
    image: huanqingdong/elasticsearch:6.8.3-ik
    container_name: es01
    hostname: docker-14
    restart: always
    environment:
      - node.name=es01
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms1g -Xmx1g"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - net
    extra_hosts:
      - "ikserver:192.168.1.14"
volumes:
  data:
    driver: local

networks:
  net:

启动ES

# 进入docker-compose.yml文件所在目录
[root@localhost ~]# cd /root/docker-compose/elasticsearch/
# 先使用前台启动,观察日志
[root@localhost elasticsearch]# docker-compose up
Starting es01 ... done
Attaching to es01
es01  | OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
es01  | [2019-10-14T14:41:43,570][INFO ][o.e.e.NodeEnvironment    ] [es01] using [1] data paths, mounts [[/usr/share/elasticsearch/data (/dev/mapper/centos-root)]], net usable_space [47.1gb], net total_space [49.9gb], types [xfs]
es01  | [2019-10-14T14:41:43,572][INFO ][o.e.e.NodeEnvironment    ] [es01] heap size [990.7mb], compressed ordinary object pointers [true]
es01  | [2019-10-14T14:41:43,575][INFO ][o.e.n.Node               ] [es01] node name [es01], node ID [fX_12uFXRDaAGC9qifMIYA]
es01  | [2019-10-14T14:41:43,575][INFO ][o.e.n.Node               ] [es01] version[6.8.3], pid[1], build[default/docker/0c48c0e/2019-08-29T19:05:24.312154Z], OS[Linux/3.10.0-1062.1.2.el7.x86_64/amd64], JVM[Oracle Corporation/OpenJDK 64-Bit Server VM/12.0.2/12.0.2+10]
es01  | [2019-10-14T14:41:43,575][INFO ][o.e.n.Node               ] [es01] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -Des.networkaddress.cache.ttl=60, -Des.networkaddress.cache.negative.ttl=10, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Djava.io.tmpdir=/tmp/elasticsearch-6588431321837350855, -XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=data, -XX:ErrorFile=logs/hs_err_pid%p.log, -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m, -Djava.locale.providers=COMPAT, -XX:UseAVX=2, -Des.cgroups.hierarchy.override=/, -Xms1g, -Xmx1g, -Des.path.home=/usr/share/elasticsearch, -Des.path.conf=/usr/share/elasticsearch/config, -Des.distribution.flavor=default, -Des.distribution.type=docker]
es01  | [2019-10-14T14:41:45,123][INFO ][o.e.p.PluginsService     ] [es01] loaded module [aggs-matrix-stats]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [analysis-common]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [ingest-common]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [ingest-geoip]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [ingest-user-agent]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [lang-expression]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [lang-mustache]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [lang-painless]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [mapper-extras]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [parent-join]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [percolator]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [rank-eval]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [reindex]
es01  | [2019-10-14T14:41:45,124][INFO ][o.e.p.PluginsService     ] [es01] loaded module [repository-url]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [transport-netty4]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [tribe]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-ccr]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-core]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-deprecation]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-graph]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-ilm]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-logstash]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-ml]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-monitoring]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-rollup]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-security]
es01  | [2019-10-14T14:41:45,125][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-sql]
es01  | [2019-10-14T14:41:45,126][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-upgrade]
es01  | [2019-10-14T14:41:45,126][INFO ][o.e.p.PluginsService     ] [es01] loaded module [x-pack-watcher]
es01  | [2019-10-14T14:41:45,126][INFO ][o.e.p.PluginsService     ] [es01] loaded plugin [analysis-ik]
es01  | [2019-10-14T14:41:48,262][INFO ][o.e.x.s.a.s.FileRolesStore] [es01] parsed [0] roles from file [/usr/share/elasticsearch/config/roles.yml]
es01  | [2019-10-14T14:41:48,792][INFO ][o.e.x.m.p.l.CppLogMessageHandler] [es01] [controller/88] [Main.cc@109] controller (64 bit): Version 6.8.3 (Build 7ace96ffff9215) Copyright (c) 2019 Elasticsearch BV
es01  | [2019-10-14T14:41:49,453][INFO ][o.e.d.DiscoveryModule    ] [es01] using discovery type [zen] and host providers [settings]
es01  | [2019-10-14T14:41:50,324][INFO ][o.e.n.Node               ] [es01] initialized
es01  | [2019-10-14T14:41:50,324][INFO ][o.e.n.Node               ] [es01] starting ...
es01  | [2019-10-14T14:41:50,524][INFO ][o.e.t.TransportService   ] [es01] publish_address {172.19.0.2:9300}, bound_addresses {[::]:9300}
es01  | [2019-10-14T14:41:50,540][INFO ][o.e.b.BootstrapChecks    ] [es01] bound or publishing to a non-loopback address, enforcing bootstrap checks
es01  | [2019-10-14T14:41:53,661][INFO ][o.e.c.s.MasterService    ] [es01] zen-disco-elected-as-master ([0] nodes joined), reason: new_master {es01}{fX_12uFXRDaAGC9qifMIYA}{mMfuxv8GRTKlkUpkljdQCA}{172.19.0.2}{172.19.0.2:9300}{ml.machine_memory=1907814400, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true}
es01  | [2019-10-14T14:41:53,666][INFO ][o.e.c.s.ClusterApplierService] [es01] new_master {es01}{fX_12uFXRDaAGC9qifMIYA}{mMfuxv8GRTKlkUpkljdQCA}{172.19.0.2}{172.19.0.2:9300}{ml.machine_memory=1907814400, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true}, reason: apply cluster state (from master [master {es01}{fX_12uFXRDaAGC9qifMIYA}{mMfuxv8GRTKlkUpkljdQCA}{172.19.0.2}{172.19.0.2:9300}{ml.machine_memory=1907814400, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true} committed version [1] source [zen-disco-elected-as-master ([0] nodes joined)]])
es01  | [2019-10-14T14:41:53,787][INFO ][o.e.h.n.Netty4HttpServerTransport] [es01] publish_address {172.19.0.2:9200}, bound_addresses {[::]:9200}
es01  | [2019-10-14T14:41:53,788][INFO ][o.e.n.Node               ] [es01] started
es01  | [2019-10-14T14:41:53,796][WARN ][o.e.x.s.a.s.m.NativeRoleMappingStore] [es01] Failed to clear cache for realms [[]]
es01  | [2019-10-14T14:41:53,855][INFO ][o.e.g.GatewayService     ] [es01] recovered [0] indices into cluster_state
es01  | [2019-10-14T14:41:53,957][INFO ][o.w.a.d.Monitor          ] [es01] try load config from /usr/share/elasticsearch/config/analysis-ik/IKAnalyzer.cfg.xml
es01  | [2019-10-14T14:41:54,161][INFO ][o.w.a.d.Monitor          ] [es01] [Dict Loading] http://ikserver/ik/my_extra.dic
es01  | [2019-10-14T14:41:54,361][INFO ][o.w.a.d.Monitor          ] [es01] [Dict Loading] http://ikserver/ik/my_stopword.dic
es01  | [2019-10-14T14:41:54,506][INFO ][o.e.c.m.MetaDataIndexTemplateService] [es01] adding template [.watch-history-9] for index patterns [.watcher-history-9*]
es01  | [2019-10-14T14:41:54,535][INFO ][o.e.c.m.MetaDataIndexTemplateService] [es01] adding template [.watches] for index patterns [.watches*]
es01  | [2019-10-14T14:41:54,568][INFO ][o.e.c.m.MetaDataIndexTemplateService] [es01] adding template [.triggered_watches] for index patterns [.triggered_watches*]
es01  | [2019-10-14T14:41:54,600][INFO ][o.e.c.m.MetaDataIndexTemplateService] [es01] adding template [.monitoring-logstash] for index patterns [.monitoring-logstash-6-*]
es01  | [2019-10-14T14:41:54,637][INFO ][o.e.c.m.MetaDataIndexTemplateService] [es01] adding template [.monitoring-es] for index patterns [.monitoring-es-6-*]
es01  | [2019-10-14T14:41:54,662][INFO ][o.e.c.m.MetaDataIndexTemplateService] [es01] adding template [.monitoring-alerts] for index patterns [.monitoring-alerts-6]
es01  | [2019-10-14T14:41:54,690][INFO ][o.e.c.m.MetaDataIndexTemplateService] [es01] adding template [.monitoring-beats] for index patterns [.monitoring-beats-6-*]
es01  | [2019-10-14T14:41:54,718][INFO ][o.e.c.m.MetaDataIndexTemplateService] [es01] adding template [.monitoring-kibana] for index patterns [.monitoring-kibana-6-*]
es01  | [2019-10-14T14:41:54,790][INFO ][o.e.l.LicenseService     ] [es01] license [790a7359-89a5-46f0-a95b-ed94a3ec486d] mode [basic] - valid
es01  | [2019-10-14T14:42:04,412][INFO ][o.w.a.d.Monitor          ] [es01] 重新加载词典...
es01  | [2019-10-14T14:42:04,414][INFO ][o.w.a.d.Monitor          ] [es01] try load config from /usr/share/elasticsearch/config/analysis-ik/IKAnalyzer.cfg.xml
es01  | [2019-10-14T14:42:04,607][INFO ][o.w.a.d.Monitor          ] [es01] [Dict Loading] http://ikserver/ik/my_extra.dic
es01  | [2019-10-14T14:42:04,612][INFO ][o.w.a.d.Monitor          ] [es01] [Dict Loading] http://ikserver/ik/my_stopword.dic
es01  | [2019-10-14T14:42:04,620][INFO ][o.w.a.d.Monitor          ] [es01] 重新加载词典完毕...
es01  | [2019-10-14T14:42:04,623][INFO ][o.w.a.d.Monitor          ] [es01] 重新加载词典...
es01  | [2019-10-14T14:42:04,623][INFO ][o.w.a.d.Monitor          ] [es01] try load config from /usr/share/elasticsearch/config/analysis-ik/IKAnalyzer.cfg.xml
es01  | [2019-10-14T14:42:04,755][INFO ][o.w.a.d.Monitor          ] [es01] [Dict Loading] http://ikserver/ik/my_extra.dic
es01  | [2019-10-14T14:42:04,761][INFO ][o.w.a.d.Monitor          ] [es01] [Dict Loading] http://ikserver/ik/my_stopword.dic
es01  | [2019-10-14T14:42:04,767][INFO ][o.w.a.d.Monitor          ] [es01] 重新加载词典完毕...


# 当出现下面行时,说明启动成功
es01  | [2019-10-14T14:41:53,788][INFO ][o.e.n.Node               ] [es01] started
# 从上面的日志中可以看到有加载IK词典的日志输出

# 使用ctrl+c停掉上面启动的es
^CGracefully stopping... (press Ctrl+C again to force)
Stopping es01 ... done

# 然后通过下面命令后台启动
[root@localhost elasticsearch]# docker-compose up -d 
Starting es01 ... done

验证ES

# 访问es地址
[root@localhost elasticsearch]# curl http://192.168.1.14:9200
{
  "name" : "es01",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "G0FRNYK4SzyIIIYV9oeiXA",
  "version" : {
    "number" : "6.8.3",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "0c48c0e",
    "build_date" : "2019-08-29T19:05:24.312154Z",
    "build_snapshot" : false,
    "lucene_version" : "7.7.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

# 从返回信息中可以看出,ES已经启动成功,版本为6.8.3,节点名为es01

你可能感兴趣的:(全文检索示例)