- 环境准备
- docker、docker-compose安装(https://docs.docker.com/compose/install/)
#docker安装
curl -sSL https://get.daocloud.io/docker | sh
#docker-compose安装
curl -L
https://get.daocloud.io/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m`
> /usr/local/bin/docker-compose
chmod x /usr/local/bin/docker-compose
#查看安装结果
docker-compose -v
- 创建
elasticsearch
数据、日志存放目录
#创建数据/日志目录 这里我们部署3个节点
mkdir /root/app/elasticsearch/data/{node0,node1,node2} -p
mkdir /root/app/elasticsearch/logs/{node0,node1,node2} -p
cd /opt/elasticsearch
#权限
chmod 0777 data/* -R && chmod 0777 logs/* -R
#防止JVM报错
echo vm.max_map_count=262144 >> /etc/sysctl.conf
sysctl -p
- 创建docker-compose编排文件
在刚刚新建的目录(/root/app/elasticsearch)创建docker-compose.yml文件,基于镜像elasticsearch:7.4.0
代码语言:javascript复制ersion: '2.2'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.0
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./data/node0:/usr/share/elasticsearch/data
- ./logs/node0:/usr/share/elasticsearch/logs
ports:
- 9200:9200
networks:
- elastic
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.0
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./data/node1:/usr/share/elasticsearch/data
- ./logs/node1:/usr/share/elasticsearch/logs
networks:
- elastic
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.0
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./logs/node2:/usr/share/elasticsearch/data
- ./logs/node2:/usr/share/elasticsearch/logs
networks:
- elastic
networks:
elastic:
driver: bridge
参数说明:
- 集群名称:cluster.name=es-docker-cluster
- 节点名称:node.name=es01
- 是否可作为主节点:node.master=true
- 是否存储数据:node.data=true
- 锁定进程的物理内存地址避免交换(swapped)来提高性能:bootstrap.memory_lock=true
- 创建并启动服务
#启动
docker-compose up -d
#查看
docker-compose ps
可以运行 curl -X GET “localhost:9200/_cat/nodes?v&pretty” 查看集群信息
- 安装Head插件(https://github.com/mobz/elasticsearch-head)
git clone git://github.com/mobz/elasticsearch-head.git
- 下载Node.js(https://nodejs.org/en/download)
安装完成 在cmd窗口执行node -v查看node.js的版本号 检查是否安装成功
- 安装grunt
npm install -g grunt-cli
通过node.js的包管理器npm安装grunt为全局命宁,grunt是基于Node.js的项目构建工具
- 执行 npm install (不执行该命宁 使用grunt server命令会报错)
npm install
- 启动elasticsearch-head服务
cd ~/app/elasticsearch-head
grunt server
#如果后台启动 运行 nohup grunt server &exit
- 停止elasticsearch-head服务
如果是后台运行的elasticsearch-head服务,只能通过kill 端口停止服务
代码语言:javascript复制#通过lsof -i找到端口对应的进程(PID)
lsof -i:9100
#kill调对应的进程
kill -9 6076
注意:启动如果报一下错误:
代码语言:javascript复制1. >>Local Npm module "grunt-contrib-clean" not found. Is it installed?
2. >> Local Npm module "grunt-contrib-concat" not found. Is it installed?
3. >> Local Npm module "grunt-contrib-watch" not found. Is it installed?
4. >> Local Npm module "grunt-contrib-connect" not found. Is it installed?
5. >> Local Npm module "grunt-contrib-copy" not found. Is it installed?
6. >> Local Npm module "grunt-contrib-jasmine" not found. Is it installed?
Warning: Task "connect:server" not found. Use --force to continue.
在elasticsearch-head目录下运行一下命令:
代码语言:javascript复制npm install grunt-contrib-clean --registry=https://registry.npm.taobao.org
npm install grunt-contrib-concat --registry=https://registry.npm.taobao.org
npm install grunt-contrib-watch --registry=https://registry.npm.taobao.org
npm install grunt-contrib-connect --registry=https://registry.npm.taobao.org
npm install grunt-contrib-copy --registry=https://registry.npm.taobao.org
npm install grunt-contrib-jasmine --registry=https://registry.npm.taobao.org
- 查看效果
head主控页面是可以显示的,但是显示连接失败,出现了‘集群健康值:未连接’,如何解决这个问题呢?
- 检查是否开启了跨域
出现这种错误首先检查配置文件是否开启了跨域(http.cors.enabled)和http.cors.allow-origin,http.cors.enabled:默认是false,表示是否运行跨域;http.cors.allow-origin:当设置允许跨域,默认为*,表示支持所有域名,如果我们只是允许某些网站能访问,那么可以使用正则表达式。比如只允许本地地址。 /https?://localhost(:[0-9] )?/
- 如果是自己的服务器的话,还需要做以下修改
#修改elasticsearch-head/_site/app.js
#找到app-base_uri关键字,并且把localhost修改为自己的ip
- 再次查看效果
连接地址成功换成了自己的IP,集群健康值也变成了绿色
后续文章介绍如何使用…