Docker安装ElasticSearch环境

基本步骤和以前几篇文章同样,请参考前面的相关文章html

ElasticSearch安装node

  • 1.安装ES
  • 2.安装head,bigdesk插件
  • 3.安装ik插件
  • 4.配置ES集群

安装ES(本文使用的是elasticsearch的1.7.2版本)python

# 下载ES
$ curl -O https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.tar.gz

# 解压ES压缩包
$ tar -zxvf elasticsearch-1.7.2.tar.gz

# 启动ES
$ sudo ./elasticsearch -d
  •  

安装head,bigdesk,HQ,kopf插件(可选择安装,建议安装head和bigdesk)git

$ ${ES_HOME}/bin/plugin --install mobz/elasticsearch-head
# 安装完成访问:http://localhost:9200/_plugin/head/

$ ${ES_HOME}/bin/plugin --install lukas-vlcek/bigdesk
# 安装完成访问:http://localhost:9200/_plugin/bigdesk/#nodes

$ ${ES_HOME}/bin/plugin -install royrusso/elasticsearch-HQ
# 安装完成访问:http://localhost:9200/_plugin/HQ/

$ ${ES_HOME}/bin/plugin -install lmenezes/elasticsearch-kopf
# 安装完成访问:http://localhost:9200/_plugin/kopf/#!/cluster
  •  

安装ik插件github

# 由于我使用的是老版本的ES,因此ik插件也使用的是对应老版本的
$ git clone https://github.com/medcl/elasticsearch-analysis-ik
$ cd elasticsearch-analysis-ik
$ mvn clean
$ mvn compile
$ mvn package
$ cd target
$ cp elasticsearch-analysis-ik-xxx.jar ${ES_HOME}/plugins/ik/

$ cd elasticsearch-analysis-ik
$ cp config/ik ${ES_HOME}/config/
  •  

配置elasticsearch.yml文件,在文件的最后添加下面的配置docker

index:
  analysis:
    analyzer:
      ik:
          alias: [ik_analyzer]
          type: org.elasticsearch.index.analysis.IkAnalyzerProvider
      ik_max_word:
          type: ik
          use_smart: false
      ik_smart:
          type: ik
          use_smart: true

index.analysis.analyzer.default.type: ik
index.store.type: niofs
  •  

设置ES的内存大小apache

$ {ES_HOME}/bin
$ vi elasticsearch

# 设置ES的最大内存,最小内存
ES_MIN_MEM=4g
ES_MAX_MEM=4g
  •  

注意tomcat

若是这里不设置ES的内存大小,后面整合ELK环境的时候,Logstash会没法批量在ES建立索引而报错,具体错误信息以下。因此在ES启动时设置ES的内存大小,建议是实际内存的一半bash

Got error to send bulk of actions: Connection reset {:level=>:error}
  •  

这个内存配置不能随便设置,必须根据实际状况进行设置,不然设置以后ES就启动不起来了,报内存溢出的错误,建议尽可能不要随意修改此配置app

Dockerfile文件

############################################
# version : birdben/elasticsearch:v1
# desc : 当前版本安装的elasticsearch
############################################
# 设置继承自咱们建立的 jdk7 镜像
FROM birdben/jdk7:v1

# 下面是一些建立者的基本信息
MAINTAINER birdben (191654006@163.com)

# 设置环境变量,全部操做都是非交互式的
ENV DEBIAN_FRONTEND noninteractive

# 添加 supervisord 的配置文件,并复制配置文件到对应目录下面。(supervisord.conf文件和Dockerfile文件在同一路径)
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

RUN echo "export LC_ALL=C"

# 设置 ES 的环境变量,若读者有其余的环境变量须要设置,也能够在这里添加。
ENV ES_HOME /software/elasticsearch-1.7.2

# 复制 elasticsearch-1.7.2 文件到镜像中(elasticsearch-1.7.2文件夹要和Dockerfile文件在同一路径)
ADD elasticsearch-1.7.2 /software/elasticsearch-1.7.2

# 容器须要开放ES的9200和9300端口
EXPOSE 9200
EXPOSE 9300

# 执行supervisord来同时执行多个命令,使用 supervisord 的可执行路径启动服务。
CMD ["/usr/bin/supervisord"]
  •  

Dockerfile源文件连接:

https://github.com/birdben/birdDocker/blob/master/elasticsearch/Dockerfile

supervisor配置文件内容

# 配置文件包含目录和进程
# 第一段 supervsord 配置软件自己,使用 nodaemon 参数来运行。
# 第二段包含要控制的 2 个服务。每一段包含一个服务的目录和启动这个服务的命令。

[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D

# 修改supervisor配置方式以下,修改成不自动重启ES,而且改为非daemon,DFOREGROUND的方式运行,supervisor就能够监控到了
[program:elasticsearch]
command=/bin/bash -c "exec ${ES_HOME}/bin/elasticsearch -DFOREGROUND"
  •  

注意supervisor配置

# 以前一直在supervisor使用以下配置来启动ES,可是仔细观察Docker的控制台输出会发现以下的错误
[program:elasticsearch]
command=/{ES_HOME}/bin/elasticsearch -d

INFO success: elasticsearch entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
INFO exited: elasticsearch (exit status 1; not expected)
INFO spawned: 'elasticsearch' with pid 12
INFO exited: elasticsearch (exit status 1; not expected)
INFO spawned: 'elasticsearch' with pid 13
INFO exited: elasticsearch (exit status 1; not expected)
INFO gave up: elasticsearch entered FATAL state, too many start retries too quickly

# 这里使用supervisorctl status查看supervisor监控的全部服务,就会发现ES没有处于被监控状态
$ supervisorctl status
elasticsearch                    FATAL      Exited too quickly (process log may have details)
sshd                             RUNNING    pid 6, uptime 0:01:49
tomcat                           RUNNING    pid 8, uptime 0:01:49

# 因此这里修改supervisor配置方式以下,修改成不自动重启ES,而且改为非daemon,DFOREGROUND的方式运行,supervisor就能够监控到了
[program:elasticsearch]
startsecs = 0
autorestart = false
command=/bin/bash -c "exec ${ES_HOME}/bin/elasticsearch -DFOREGROUND"

# 这里说明一下supervisor启动多个服务,要求全部启动的服务都是非daemon的方式启动,不然就会遇到如上的问题,autorestart设置为false只是为了让supervisor启动报错的时候不会重复启动,只要改为非daemon的方式启动ES,能够设置autorestart为true
  •  

控制台终端

# 构建镜像
$ docker build -t="birdben/elasticsearch:v1" .
# 执行已经构件好的镜像
$ docker run -p 9999:22 -p 9200:9200 -p 9300:9300 -t -i 'birdben/elasticsearch:v1'
  •  

访问ElasticSearch的插件测试

http://10.211.55.4:9200/_plugin/head/
http://10.211.55.4:9200/_plugin/bigdesk/#nodes
  •  

User索引的mapping

{
    "mappings": {
      "user": {
        "dynamic" : "strict",
        "properties": {
          "id": {
            "type": "string",
            "index": "not_analyzed"
          },
          "name": {
            "type": "string",
            "index_analyzer": "ik",
            "search_analyzer": "ik_smart"
          },
          "age": {
            "type": "integer"
          },
          "job": {
            "type": "string",
            "index_analyzer": "ik",
            "search_analyzer": "ik_smart"
          },
          "createTime": {
            "type": "long"
          }
        }
      }
    }
}
  •  

新建索引测试ik分词

# 尝试建立user索引
$ curl -XPOST 'http://10.211.55.4:9200/user?pretty' -d '{
    "mappings": {
      "user": {
        "dynamic" : "strict",
        "properties": {
          "id": {
            "type": "string",
            "index": "not_analyzed"
          },
          "name": {
            "type": "string",
            "index_analyzer": "ik",
            "search_analyzer": "ik_smart"
          },
          "age": {
            "type": "integer"
          },
          "job": {
            "type": "string",
            "index_analyzer": "ik",
            "search_analyzer": "ik_smart"
          },
          "createTime": {
            "type": "long"
          }
        }
      }
    }
}'

# 若是遇到下面的错误,缘由是${ES_HOME}/lib/下须要引入httpclient-4.5.jar, httpcore-4.4.1.jar
{
  "error" : "IndexCreationException[[user] failed to create index]; nested: NoClassDefFoundError[org/apache/http/client/ClientProtocolException]; nested: ClassNotFoundException[org.apache.http.client.ClientProtocolException]; ",
  "status" : 500
}

# 建立索引成功后,查看索引信息
$ curl -XGET 'http://10.211.55.4:9200/_cat/indices?pretty'
green open user 5 1 0 0 970b 575b

# 测试ik分词效果
$ curl -XGET 'http://10.211.55.4:9200/user/_analyze?analyzer=ik&pretty=true' -d '{"text":"世界如此之大"}'

# 测试ik_smart分词效果
$ curl -XGET 'http://10.211.55.4:9200/user/_analyze?analyzer=ik_smart&pretty=true' -d '{"text":"世界如此之大"}'
  •  

参考文章:

相关文章
相关标签/搜索