1. 程式人生 > >docker 安裝ElasticSearch(6.x版本)

docker 安裝ElasticSearch(6.x版本)

安裝ElasticSearch

拉取映象,選擇版本為6.5.0

$ docker pull elasticsearch:6.5.0

檢視映象

$ docker images

啟動一個容器

$ docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -p 9200:9200 -p 9300:9300 elasticsearch:6.5.0
42d639a089348b393b0cc912141ef357b6565996c5c4863e363f8729da229d7d

然後訪問 GET localhost:9200 ,發現未啟動成功,檢視日誌

$ docker logs -f 42d6
[2018-12-05T06:07:06,546][INFO ][o.e.b.BootstrapChecks    ] [IHubvTB] bound or publishing to a non-loopback address, enforcing bootstrap checks
ERROR: [1] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2018-12-05T06:07:06,630][INFO ][o.e.n.Node               ] [IHubvTB] stopping ...
[2018-12-05T06:07:06,939][INFO ][o.e.n.Node               ] [IHubvTB] stopped
[2018-12-05T06:07:06,939][INFO ][o.e.n.Node               ] [IHubvTB] closing ...
[2018-12-05T06:07:06,973][INFO ][o.e.n.Node               ] [IHubvTB] closed

這裡提示:vm.max_map_count [65530] is too low, increase to at least [262144],說max_map_count的值太小了,需要設大到262144

檢視max_map_count的值

$ cat /proc/sys/vm/max_map_count
65530

重新設定max_map_count的值

$ sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144

再次啟動容器

$ docker start 42d6

再次訪問 GET localhost:9200

{
    "name": "IHubvTB",
    "cluster_name": "docker-cluster",
    "cluster_uuid": "edN3o1PkTAKF5C4N-Uw7tQ",
    "version": {
        "number": "6.5.0",
        "build_flavor": "default",
        "build_type": "tar",
        "build_hash": "816e6f6",
        "build_date": "2018-11-09T18:58:36.352602Z",
        "build_snapshot": false,
        "lucene_version": "7.5.0",
        "minimum_wire_compatibility_version": "5.6.0",
        "minimum_index_compatibility_version": "5.0.0"
    },
    "tagline": "You Know, for Search"
}

安裝成功。

head外掛安裝

5.x版本head外掛安裝說明

1.x版本和2.x版本,可以直接使用plugin命令來安裝head外掛

5.x以上版本,無法通過plugin命令來安裝。

https://github.com/mobz/elasticsearch-head 文件說明:

Running as a plugin of Elasticsearch (deprecated)
for Elasticsearch 5.x: site plugins are not supported. Run as a standalone server
for Elasticsearch 2.x: sudo elasticsearch/bin/plugin install mobz/elasticsearch-head
for Elasticsearch 1.x: sudo elasticsearch/bin/plugin -install mobz/elasticsearch-head/1.x
for Elasticsearch 0.x: sudo elasticsearch/bin/plugin -install mobz/elasticsearch-head/0.9
open http://localhost:9200/_plugin/head/

需要藉助node來跑一個服務

Running with built in server
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start
open http://localhost:9100/
This will start a local webserver running on port 9100 serving elasticsearch-head

但是還有一種安裝方式,就是通過docker安裝,不同的是,訪問地址變為http://localhost:9100/

Running with docker
for Elasticsearch 5.x: docker run -p 9100:9100 mobz/elasticsearch-head:5
for Elasticsearch 2.x: docker run -p 9100:9100 mobz/elasticsearch-head:2
for Elasticsearch 1.x: docker run -p 9100:9100 mobz/elasticsearch-head:1
for fans of alpine there is mobz/elasticsearch-head:5-alpine
open http://localhost:9100/

使用docker安裝head外掛

拉取映象(這裡選擇國內映象源)

$ docker pull hub.c.163.com/riveryang/elasticsearch-head-5.x

啟動容器

$ docker run -d -p 9100:9100 --name elasticsearch-head5 hub.c.163.com/riveryang/elasticsearch-head-5.x:5

然後瀏覽器訪問 localhost:9200

出現這個介面,說明head外掛安裝成功。

但是發現健康值為:未連線?

開啟瀏覽器除錯,發現報錯資訊:

Access to XMLHttpRequest at 'http://localhost:9200/' from origin 'http://localhost:9100' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

這裡也就是跨域錯誤

ElasticSearch跨域設定

進入elasticsearch容器

$ docker exec -it 42d639a08934 /bin/bash

找到配置檔案的目錄

$ /usr/share/elasticsearch/config
$ ls
elasticsearch.keystore  ingest-geoip  log4j2.properties  roles.yml  users_roles
elasticsearch.yml       jvm.options   role_mapping.yml   users

修改elasticsearch.yml,需要安裝vim(參考 https://my.oschina.net/yimingkeji/blog/2978974)

如果提示apt-get不存在,使用yum安裝vim

$ yum install -y vim

安裝後編輯elasticsearch.yml

$ vim elasticsearch.yml
# ----新增內容----
# head外掛設定
http.cors.enabled: true
http.cors.allow-origin: "*"

重啟容器

$ docker restart 42d639a08934

再次訪問外掛

安裝完成。