启动镜像脚本
docker stop elasticsearch && docker rm elasticsearch
docker run --name elasticsearch \
-p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-v /mydata/elasticsearch/config:/usr/share/elasticsearch/config \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /mydata/elasticsearch/logs:/usr/share/elasticsearch/logs \
--privileged=true -d elasticsearch:7.6.1
进入容器
[root@node01 elasic]# docker exec -it elasticsearch bash
[root@85e67cc2908b elasticsearch]# ls
LICENSE.txt NOTICE.txt README.asciidoc bin config data jdk lib logs modules plugins
目录讲解
bin 脚本文件,包括启动 Elasticsearch、安装插件,运行统计数据等。
config elasticsearch.yml 集群配置文件
data path.data 数据文件
plugins 包含所有已安装插件
lib Java 类库
vim config/elasticsearch.yml
cluster.name: elastic-nodes
node.name: node02
path.data: /usr/share/elasticsearch/data
path.logs: /usr/share/elasticsearch/logs
#设置绑定ip 0.0.0.0 任何机器都可以访问节点
network.host: 0.0.0.0
http.port: 9200
#是否开启master角色选举
#node.master: true#
#是否开启数据节点角色
#node.data: true
#以下两项是外部访问http需要开启的项启用跨域资源共享
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.max-age: 1728000
验证elasticsearch是否启动成功linux内用curl访问: curl http://localhost:9200,输入类似如下的信息表示安装成功
{
"name": "node02",
"cluster_name": "elastic-nodes",
"cluster_uuid": "AsDc7IrcTU2JVs7wKJTbzQ",
"version": {
"number": "7.6.1",
"build_flavor": "default",
"build_type": "docker",
"build_hash": "aa751e09be0a5072e8570670309b1f12348f023b",
"build_date": "2020-02-29T00:15:25.529771Z",
"build_snapshot": false,
"lucene_version": "8.4.0",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}
插件安装可以用elasticsearch-plugin install url命令比如安装:elasticsearch-analysis-ik(分词器),Ik分词器版本要和ES和Kibana版本保持一致
下载分词器:
https://github.com/medcl/elasticsearch-analysis-ik
cd /usr/share/elasticsearch/plugins/
下载分词器
https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.1/elasticsearch-analysis-ik-7.6.1.zip
将下载好的分词器上传到服务器
并拷贝到容器内
docker cp elasticsearch-analysis-ik-7.6.1.zip elasticsearch:/usr/share/elasticsearch/plugins
进入容器
docker exec -it elasticsearch /bin/bash
在/usr/share/elasticsearch/plugins 创建ik目录
将分词器压缩包解压到ik文件夹下,并删除压缩包
然后重启容器
安装插件,elasticsearch-analysis-ik版本与elasticsearch保持一致,即7.6.1
重启elastic
docker restart elasticsearch
测试
http://node02:5601/app/kibana#/dev_tools/console
测试分词效果
POST _analyze
{
"analyzer":"standard",
"text":"我爱你中国"
}
POST _analyze
{
"analyzer": "ik_smart",
"text": "中华人民共和国"
}
#ik_smart:会做最粗粒度的拆分
POST _analyze
{
"analyzer":"ik_max_word",
"text":"我爱你中国"
}
#ik_max_word:会将文本做最细粒度的拆分
{
"tokens" : [
{
"token" : "中华人民共和国",
"start_offset" : 0,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "中华人民",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "中华",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "华人",
"start_offset" : 1,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "人民共和国",
"start_offset" : 2,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "人民",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "共和国",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "共和",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "国",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 8
}
]
}
docker stop kibana && docker rm kibana
docker run --name kibana \
-e ELASTICSEARCH_URL=http://node02:9200 \
--link=elasticsearch:elasticsearch \
-p 5601:5601 \
-d kibana:7.6.1
启动以后可以打开浏览器输入http://ip:5601就可以打开kibana的界面了。
http://192.168.36.133:5601/app/kibana
elasticsearch-head是用于Elasticsearch监控的插件
docker stop elasticHead && docker rm elasticHead
docker run -d --name elasticHead \
-p 9100:9100 \
mobz/elasticsearch-head:5
浏览器访问(http://ip:9100/)
温馨提示可能遇到的问题:
1:跨域问题,这个在前面的elasticsearch.yaml配置文件中添加了
http.cors.enabled: true
http.cors.allow-origin: "*"
2:
对于{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}
打开文件夹_site,cd _site/
修改_site目录下vendor.js文件
6886行 contentType: "application/x-www-form-urlencoded 改成 contentType: "application/json;charset=UTF-8"
7574行 var inspectData = s.contentType === "application/x-www-form-urlencoded" &&改成var inspectData = s.contentType === "application/json;charset=UTF-8" &&
直接定位
:7574
修改ip地址
vim _site/app.js
:4328
http://localhost:9200更改 http://node02:9200
可以將容器内的文件拷贝出来修改后映射挂载进去
docker stop elasticHead && docker rm elasticHead
docker run -d --name elasticHead \
-p 9000:9100 \
-v /mydata/elasticHead/app/_site:/usr/src/app/_site \
-v /mydata/elasticHead/app/Gruntfile.js:/usr/src/app/Gruntfile.js \
mobz/elasticsearch-head:5