0%

Docker搭建Elasticsearch

ElasticSearch安装

Docker镜像拉取

1
2
3
docker search elasticsearch

docker pull elasticsearch:7.7.0

本地创建es挂载目录

1
2
3
4
5
mkdir /data/elasticsearch/data

mkdir /data/elasticsearch/plugins

mkdir /data/elasticsearch/config

赋予各目录777权限

1
chmod 777 /data/elasticsearch/data

进入config目录创建elasticsearch.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
> cd /data/elasticsearch/config
vim elasticsearch.yml

#集群名称
cluster.name: "elasticsearch"
network.host: 0.0.0.0
#跨域设置
http.cors.enabled: true
http.cors.allow-origin: "*"
#http端口
http.port: 9200
#java端口
transport.tcp.port: 9300

运行镜像

1
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -d --restart=always -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms512m -Xmx512m"  -v /data/elasticsearch/data:/usr/share/elasticsearch/data -v /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins -v /data/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml 7ec4f35ab452 (镜像id)
  • –name:表示镜像启动后的容器名称
  • -d: 后台运行容器,并返回容器ID;
  • -e: 指定容器内的环境变量
  • -p: 指定端口映射,格式为:主机(宿主)端口:容器端口
  • -v:标记 在容器中设置了一个挂载点/data(就是容器中的一个目录),并将主机上的/data/test/data 目录中的内容关联到/data下

浏览器访问IP:9200

1、如果是在线上服务器搭建,可以先使用curl 127.0.0.1:9200是否可以访问,如果出现如下则表示安装成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name": "e640052ea9d2",
"cluster_name": "elasticsearch",
"cluster_uuid": "7_4uRzWoRRaWRtcaqithEA",
"version": {
"number": "7.7.0",
"build_flavor": "default",
"build_type": "docker",
"build_hash": "81a1e9eda8e6183f5237786246f6dced26a10eaf",
"build_date": "2020-05-12T02:01:37.602180Z",
"build_snapshot": false,
"lucene_version": "8.5.1",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}

2、如果需要访问线上服务器ip:9200,需要去服务器后台开启9200端口后再访问,如果出现上面内容,表示成功。

安装Elasticsearch-head

镜像拉取

1
2
3
4
5
6
7
8
#拉取镜像
docker pull mobz/elasticsearch-head:5

#创建容器
docker create --name elasticsearch-head -p 9100:9100 --restart=always mobz/elasticsearch-head:5

#启动容器
docker start elasticsearch-head

修改docker中elasticsearch的elasticsearch.yml文件

1
2
docker exec -it elasticsearch /bin/bash (进不去使用容器id进入)
vim config/elasticsearch.yml
1
2
3
# 最下方添加
http.cors.enabled: true
http.cors.allow-origin: "*"

vim修改文件报错 bash: vi: command not found

1
2
3
4
5
apt-get update
apt-get install vim

# 下载vim
yum -y install vim*

退出重启服务

1
2
exit
docker restart 容器id

浏览器访问IP:9100

如果出现elasticsearch-head界面则成功,需要手动输入ip:9200连接

修改Elasticsearch-head配置,解决406错误码(没有报错可以跳过)

在elasticsearch-head界面添加索引报错406,需要如下处理

1
2
3
4
5
#复制vendor.js到外部
docker cp 7ec4f35ab452:/usr/src/app/_site/vendor.js /data/elasticsearch/head/_site

#修改vendor.js
vim vendor.js
1
2
3
4
5
6
7
8
9
修改点
1. 6886行 contentType:"application/x-www-form-urlencoded"
改为
contentType:"application/json;charset=UTF-8"

2. 7574var inspectData = s.contentType === "application/x-www-form-urlencoded" &&
改为
var inspectData = s.contentType === "application/json;charset=UTF-8" &&

修改完成后复制回容器

1
docker cp /data/head/_site/vendor.js 729683e0e0f5:/usr/src/app/_site

重启Elasticsearch-head

安装IK分词器

下载ik6.7.0版本

1
> wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.7.0/elasticsearch-analysis-ik-6.7.0.zip

elasticsearch.yml配置

7.x的elasticsearch.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#集群名称
cluster.name: zyj-es
#本节点名称
node.name: node-1
#是否master节点
node.master: true
#是否存储数据
node.data: true
#跨域设置
http.cors.enabled: true
http.cors.allow-origin: "*"
#http端口
http.port: 9200
#java端口
transport.tcp.port: 9300
#可以访问es集群的ip 0.0.0.0表示不绑定
network.bind_host: 0.0.0.0
#es集群相互通信的ip 0.0.0.0默认本地网络搜索
network.publish_host: 0.0.0.0

#7.x配置
discovery.seed_hosts: ["127.0.0.1:9200"]
cluster.initial_master_nodes: ["node-1"]

6.x的elasticsearch.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#集群名称
cluster.name: zyj-es
#本节点名称
node.name: node-1
#是否master节点
node.master: true
#是否存储数据
node.data: true
#跨域设置
http.cors.enabled: true
http.cors.allow-origin: "*"
#http端口
http.port: 9200
#java端口
transport.tcp.port: 9300
#可以访问es集群的ip 0.0.0.0表示不绑定
network.bind_host: 0.0.0.0
#es集群相互通信的ip 0.0.0.0默认本地网络搜索
network.publish_host: 0.0.0.0

#6.x配置
discovery.zen.minimum_master_nodes: 1
xpack.license.self_generated.type: basic