一 安装Elasticsearch
1. 安装前准备,容器数据卷映射路径创建以及集群配置文件设置
容器数据卷映射路径创建
# 递归创建所有路径 集群有三台节点 就创建三个目录
mkdir -p /home/docker/container/volume/elasticsearch
# es数据备份路径
mkdir -p /home/docker/container/volume/elasticsearch/node1/data
mkdir -p /home/docker/container/volume/elasticsearch/node2/data
mkdir -p /home/docker/container/volume/elasticsearch/node3/data
# es elasticsearch.yml 配置文件容器数据卷
mkdir -p /home/docker/container/volume/elasticsearch/node1/config
mkdir -p /home/docker/container/volume/elasticsearch/node2/config
mkdir -p /home/docker/container/volume/elasticsearch/node3/config
# 挂载elasticsearch插件 容器数据卷 三个节点共用一个文件 这样一个节点安装了,其他节点也有插件
mkdir -p /home/docker/container/volume/elasticsearch/plugin
# elasticsearch-head插件 的容器数据卷映射
mkdir -p /home/docker/container/volume/elasticsearch/head
# 改变目录权限 要执行这个命令, 否则会导致启动集群时, docker容器无法挂载数据卷
chmod -r 777 /home/docker/container/volume/elasticsearch
修改集群配置文件
配置文件还有一些其他参数的设置, 具体需要查看ES的官网
# 编辑 节点一的配置文件
vim /home/docker/container/volume/elasticsearch/node1/config/elasticsearch.yml
# 编辑 节点二的配置文件
vim /home/docker/container/volume/elasticsearch/node2/config/elasticsearch.yml
# 编辑 节点三的配置文件
vim /home/docker/container/volume/elasticsearch/node3/config/elasticsearch.yml
#节点1
# es集群名称
cluster.name: elasticsearch-cluster
# 节点名称
node.name: es-node1
network.bind_host: 0.0.0.0
# 填写宿主机的IP
network.publish_host: 192.168.222.132
http.port: 9200
transport.tcp.port: 9300
# 跨域设置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Content-Type,Accept,Authorization, x-requested-with
node.master: true
node.data: true
# 设置集群通信IP
discovery.zen.ping.unicast.hosts: ["192.168.222.132:9300","192.168.222.132:9301","192.168.222.132:9302"]
# es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master
cluster.initial_master_nodes: es-node1
#节点2
# es集群名称
cluster.name: elasticsearch-cluster
# 节点名称
node.name: es-node2
network.bind_host: 0.0.0.0
# 填写宿主机的IP
network.publish_host: 192.168.222.132
http.port: 9200
transport.tcp.port: 9300
# 跨域设置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Content-Type,Accept,Authorization, x-requested-with
node.master: true
node.data: true
# 设置集群通信IP
discovery.zen.ping.unicast.hosts: ["192.168.222.132:9300","192.168.222.132:9301","192.168.222.132:9302"]
# es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master
cluster.initial_master_nodes: es-node1
#节点3
# es集群名称
cluster.name: elasticsearch-cluster
# 节点名称
node.name: es-node3
network.bind_host: 0.0.0.0
# 填写宿主机的IP
network.publish_host: 192.168.222.132
http.port: 9200
transport.tcp.port: 9300
# 跨域设置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Content-Type,Accept,Authorization, x-requested-with
node.master: true
node.data: true
# 设置集群通信IP
discovery.zen.ping.unicast.hosts: ["192.168.222.132:9300","192.168.222.132:9301","192.168.222.132:9302"]
# es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master
cluster.initial_master_nodes: es-node1
ES分词器安装包准备
下载中文分词器https://github.com/medcl/elasticsearch-analysis-ik
, 这里选择 7.8.0版本
https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.8.0/elasticsearch-analysis-ik-7.8.0.zip
, 下载后将zip包放入/home/docker/container/volume/elasticsearch/plugin
目录中,并解压;
# 先创建ik目录
mkdir /home/docker/container/volume/elasticsearch/plugin/ik
# 将zip包移动到ik目录下解压
unzip elasticsearch-analysis-ik-7.8.0.zip
或者 在容器启动后 通过命令安装分词器插件 (可能因为github访问太慢导致失败)
# 进入容器后 执行命令
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.8.0/elasticsearch-analysis-ik-7.8.0.zip
2. 下载镜像, 这里选择tag为7.8.0版本的, 也可选择其他版本;
docker pull elasticsearch:7.8.0
3. 启动三个ES节点
根据GitHub上ES项目(GitHub-ES7-Dockerfile)的Dockerfile 文件中 WORKDIR: /usr/share/elasticsearch
得知 es的工作空间目录是 /usr/share/elasticsearch
,
因此数据容器卷的挂载是
-v /xxx/elasticsearch/node1/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
-v /xxx/elasticsearch/node1/data:/usr/share/elasticsearch/data
-e ES_JAVA_OPTS="-Xms256m -Xmx256m"
限制ES启动占用的内存空间, 生产环境以具体情况而定;
#节点1
docker run -d -p 9200:9200 -p 9300:9300 --privileged=true -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -v /home/docker/container/volume/elasticsearch/node1/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/docker/container/volume/elasticsearch/node1/data:/usr/share/elasticsearch/data -v /home/docker/container/volume/elasticsearch/plugins:/usr/share/elasticsearch/plugins --name es_node1 elasticsearch:7.8.0
#节点2
docker run -d -p 9201:9200 -p 9301:9300 --privileged=true -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -v /home/docker/container/volume/elasticsearch/node2/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/docker/container/volume/elasticsearch/node2/data:/usr/share/elasticsearch/data -v /home/docker/container/volume/elasticsearch/plugins:/usr/share/elasticsearch/plugins --name es_node2 elasticsearch:7.8.0
#节点3
docker run -d -p 9202:9200 -p 9302:9300 --privileged=true -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -v /home/docker/container/volume/elasticsearch/node3/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/docker/container/volume/elasticsearch/node3/data:/usr/share/elasticsearch/data -v /home/docker/container/volume/elasticsearch/plugins:/usr/share/elasticsearch/plugins --name es_node3 elasticsearch:7.8.0
4. 集群启动后查看集群状态: 出现下图则表示集群启动成功;
curl http://192.168.222.132:9200/_cat/health?v
**查看中文分词插件安装情况:**请求参数中 analyzer:ik_max_word 表示使用 ik分词器, 成功返回数据没有报错则表示分词器插件安装成功
# 发送分词请求
curl -H "Content-Type: application/json" -d '{"analyzer":"ik_max_word","text":"各个国家有各个国家的国歌"}' http://192.168.222.132:9200/_analyze
二 安装Elasticsearch-Head插件
docker pull mobz/elasticsearch-head:5
docker run -d -p 9100:9100 --name es-head mobz/elasticsearch-head:5
# 查看容器
docker ps
http://192.168.222.132:9100
; 在链接地址处填写集群其中一个节点的地址即可http://192.168.222.132:9200
Content-Type header [application/x-www-form-urlencoded
异常提示信息, 需要对插件请求接口的方式做修改;# docker cp 容器ID:容器内文件目录 宿主机目标目录
docker cp 7d70511668d4:/usr/src/app/_site/vendor.js /home/docker/container/volume/elasticsearch/head
# 修改vendor.js文件
vim /home/docker/container/volume/elasticsearch/head/vendor.js
# 查找vendor.js文件中这个字眼的地方 使用n/N向下查找, 一般有两处地方
# 查找到后将 application/x-www-form-urlencoded 改为 application/json;charset=UTF-8
/x-www-form-urlencoded
查找vendor.js文件中这个字眼的地方 使用n/N向下查找, 一般有两处地方
查找到后将 application/x-www-form-urlencoded 改为 application/json;charset=UTF-8
contentType: "application/x-www-form-urlencoded"
改为 contentType: "application/json;charset=UTF-8"
var inspectData = s.contentType === "application/x-www-form-urlencoded"&&
改为 var inspectData = s.contentType === "application/json;charset=UTF-8" &&
修改未完成后 :wq 保存文件;
# 删除 容器
docker rm -f 7d70511668d4
# 重新启动一个head容器
docker run -d -p 9100:9100 -v /home/docker/container/volume/elasticsearch/head/vendor.js:/usr/src/app/_site/vendor.js --name es-head mobz/elasticsearch-head:5
至此elasticsearch-head就可以正常调用elasticsearch接口;