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":"世界如此之大"}'
  •  

參考文章:

相關文章
相關標籤/搜索