一、单节点线下测试
拉取镜像
1、 进入实验机器
go 10.10.10.10
2、 拉取es 7.16.2镜像
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.16.2
安装ik分词器
1、 启动容器
docker run -d -p 0.0.0.0:9200:9200 -p 0.0.0.0:9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.16.2
2、 安装ik分词器
a. 查看刚启动的容器,找到CONTAINER ID
docker ps
b. 安装ik
注意: ik分词器需要版本和es版本一致,例如es版本是7.16.2,ik分词器也需要是7.16.2
docker exec [CONTAINERID] ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.16.2/elasticsearch-analysis-ik-7.16.2.zip
c. 重启容器
docker restart [CONTAINERID]
如果要保存带ik分词器的镜像,可以如下提交镜像
docker commit [CONTAINERID] elasticsearch_ik:8.0.0
然后就可以push到hub或者本地保存镜像
二、多节点docker-compose部署
1、编辑docker-compose.yml
首先进入到/myhome/service/elaticsearch/
,创建esv7目录(esv7代表elasticsearch 7版本) 进入esv7目录,创建docker-compose.yml文件,如下:
version: '2'
services:
es01:
image: [docker.elastic.co/elasticsearch/elasticsearch:7.16.2](http://docker.elastic.co/elasticsearch/elasticsearch:7.16.2)
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
- http.cors.enabled=true
- http.cors.allow-origin=*
- "ES_JAVA_OPTS=-Xms512m -Xmx512m" #按需修改,过低节点容易挂
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es01/data:/usr/share/elasticsearch/data
- ./plugins:/usr/share/elasticsearch/plugins
ports:
- "9200:9200"
- "9300:9300"
networks:
- elastic
es02:
image: [docker.elastic.co/elasticsearch/elasticsearch:7.16.2](http://docker.elastic.co/elasticsearch/elasticsearch:7.16.2)
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:
- ./es02/data:/usr/share/elasticsearch/data
- ./plugins:/usr/share/elasticsearch/plugins
networks:
- elastic
es03:
image: [docker.elastic.co/elasticsearch/elasticsearch:7.16.2](http://docker.elastic.co/elasticsearch/elasticsearch:7.16.2)
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:
- ./es03/data:/usr/share/elasticsearch/data
- ./plugins:/usr/share/elasticsearch/plugins
networks:
- elastic
kib01:
image: [docker.elastic.co/kibana/kibana:7.16.2](http://docker.elastic.co/kibana/kibana:7.16.2)
container_name: kib01
ports:
- 5601:5601
environment:
ELASTICSEARCH_URL: [http://es01:9200](http://es01:9200)
ELASTICSEARCH_HOSTS: '["[http://es01:9200](http://es01:9200)","[http://es02:9200](http://es02:9200)","[http://es03:9200](http://es03:9200)"]'
networks:
- elastic
volumes:
data01:
driver: local
data02:
driver: local
data03:
driver: local
networks:
elastic:
driver: bridge
2、创建挂载目录
这里我们创建了三个节点的es,在docker-compose.yml中分别是es01,es02,es03,并且分别做了数据映射,所以我们需要在esv7里面创建如下几个目录:
esv7/
├── docker-compose.yml
├── es01
│ └── data
├── es02
│ └── data
├── es03
│ └── data
└── plugins
并且设置权限:
chmod -R 777 es01 es02 es03
配置ik分词插件
然后配置分词器。下载ik插件放到plugins目录,服务器可能没外网,可本地下载上传
https://github.com/medcl/elasticsearch-analysis-ik/releases下载es的对应版本
比如下载elasticsearch-analysis-ik-7.16.2.zip, 注意: ik分词器的版本需要和elasticsearch版本一致,例如这里es用的7.16.2,ik分词器也需要下载7.16.2。然后:
cd ./plugins/ && mkdir ik
解压
unzip elasticsearch-analysis-ik-7.16.2.zip -d ./ik
最终得到如下的esv7项目目录至少包含如下结构:
esv7/
├── docker-compose.yml
├── es01
│ └── data
├── es02
│ └── data
├── es03
│ └── data
└── plugins
└── ik
启动服务
执行 docker-compose up -d
启动es集群服务,完成。
可能遇到错误
1、文件权限
报错如下:
AccessDeniedException[/usr/share/elasticsearch/data/nodes]
解决:
提前将挂载目录创建好,并将权限设置好:
chmod -R 777 es01 es02 es03
2、内存不足
报错如下:
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决:修改配置文件:
vi /etc/sysctl.conf
在文件末尾添加一行内容:
vm.max_map_count=262144
执行以下命令立即生效:
/sbin/sysctl -p
之后重新启动即可。
三、读写数据示例
curl -X PUT "localhost:9200/fql_faq_test1" -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 1
},
"mappings": {
"properties": {
"content": {
"type":"text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"vec":{
"type":"dense_vector",
"dims":768
},
"faq_id":{
"type":"text",
"index": false
},
"fsimilar_id":{
"type":"text",
"index": false
}
}
}
}
'
标签:7.16,compose,ik,elasticsearch,es01,docker,es02,ES From: https://www.cnblogs.com/jax-/p/17647888.html