1.搜索Docker仓库中elasticsearch相关的镜像
docker search elasticsearch
image.png
2.下载elasticsearch:7.10.1的镜像
docker pull elasticsearch:7.10.1
image.png
3.查询Docker仓库中的镜像
docker images
image.png
4.创建Elasticsearch容器
4.1创建所需文件
mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data
#注意:http.host: 0.0.0.0 冒号后有一空格。
echo "http.host: 0.0.0.0" >>/mydata/elasticsearch/config/elasticsearch.yml
#文件夹赋权
chmod -R 777 /mydata/elasticsearch/
4.2命令启动
docker run --name elasticsearch -p 9200:9200 \
-p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx128m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.10.1
或者
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d --name ES01 elasticsearch:7.10.1
参数说明
--name elasticsearch:将容器命名为 elasticsearch
-p 9200:9200:将容器的9200端口映射到宿主机9200端口
-p 9300:9300:将容器的9300端口映射到宿主机9300端口,目的是集群互相通信
-e "discovery.type=single-node":单例模式
-e ES_JAVA_OPTS="-Xms64m -Xmx128m":配置内存大小
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:将配置文件挂载到宿主机
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data:将数据文件夹挂载到宿主机
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins:将插件目录挂载到宿主机(需重启)
-d elasticsearch:7.10.1:后台运行容器,并返回容器ID
image.png
5.docker-compose启动
version: '2'
services:
elasticsearch:
container_name: elasticsearch
image: elasticsearch:7.10.1
ports:
- "9200:9200"
volumes:
- /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- /mydata/elasticsearch/data:/usr/share/elasticsearch/data
- /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins
environment:
- "ES_JAVA_OPTS=-Xms64m -Xmx128m"
- "discovery.type=single-node"
- "COMPOSE_PROJECT_NAME=elasticsearch-server"
restart: always
查看容器
docker ps -a
Docker启动容器自启
docker update elasticsearch --restart=always
6.插件安装
docker进入容器命令,id为8620bf1ffc9c
#docker exec -it 8620bf1ffc9c /bin/bash
docker exec -it <容器ID>/bin/bash
plugins安装:
# cd plugins
cd /usr/share/elasticsearch/plugins/
# 安装插件
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.10.1/elasticsearch-analysis-ik-7.10.1.zip
# 退出容器
exit
# 重启docker容器
#docker restart 8620bf1ffc9c
docker restart <容器ID>
6.1elasticsearch-head 监控的插件
#下载容器
docker pull mobz/elasticsearch-head:5
#启动容器
docker run -d -p 9100:9100 docker.io/mobz/elasticsearch-head:5
image.png
如图如果你把localhost改成elasticsearch的IP地址还不可以那就是出现跨域拒绝访问问题
#拿到运行容器elasticsearch 的 id
docker ps -a
#进入容器
docker exec -it <容器id> /bin/bash
#找到目录
cd ./config
#打开文件
vim elasticsearch.yml
PS:如果遇到 vim: command not found 这个问题
这都是我遇到的坑啊
#安装vim程序
yum -y install vim*
在elasticsearch.yml中添加:
http.cors.enabled: true
http.cors.allow-origin: "*"
保存yml文件以后
# 退出容器
exit
# 重启docker容器
#docker restart 8620bf1ffc9c
docker restart <容器ID>
image.png
这样就大功告成了
网友评论