安裝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 文檔說明:
插件不支持,需要藉助node來跑一個服務
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/
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
或者安裝google插件
Running as a Chrome extension
Install ElasticSearch Head from the Chrome Web Store.
Click the extension icon in the toolbar of your web browser.
但是還有一種安裝方式,就是通過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 mobz/elasticsearch-head
啓動容器
$ docker run -d -p 9100:9100 --name elasticsearch-head elasticsearch-head
然後瀏覽器訪問 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.
這裏也就是跨域錯誤
https://github.com/mobz/elasticsearch-head 文檔中也有說明:
Enable CORS in elasticsearch
When not running as a plugin of elasticsearch (which is not even possible from version 5) you must enable CORS in elasticsearch otherwise your browser will rejects requests which appear insecure.
In elasticsearch configuration;
add http.cors.enabled: true
you must also set http.cors.allow-origin because no origin allowed by default. http.cors.allow-origin: "*" is valid value, however it’s considered as a security risk as your cluster is open to cross origin from anywhere.
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
再次訪問插件
安裝完成。