# Docker
Docker version 26.1.4, build 5650f9b
# 系统信息
Operating System: openEuler 24.03 (LTS)
Kernel: Linux 6.6.0-28.0.0.34.oe2403.x86_64
Architecture: x86-64
# 拉取 elasticsearch docker 镜像
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.4.3
mkdir -p /data/es/{data,config,logs}
chown 1000:0 /data/es/{data,logs}
ingest.geoip.downloader.enabled: false
http.host: 0.0.0.0
node.name: node-1
cluster.name: es-cluster
path.data: /data/elasticsearch-8.4.3/data
path.logs: /data/elasticsearch-8.4.3/logs
xpack.security.http.ssl.enabled: false
xpack.security.enabled: false
xpack.security.transport.ssl.enabled: false
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Type,Content-Length
cluster.routing.allocation.disk.threshold_enabled: false
elasticsearch.yml文件说明
ingest.geoip.downloader.enabled: false 禁用了GeoIP数据的下载;GeoIP 是一种用于根据 IP地址查找地理位置信息的功能,通过将其设置为“false”,将禁用此功能
http.host: 0.0.0.0 指定 Elasticsearch 服务 HTTP请求地址;“0.0.0.0”意味着服务将监听所有可用的网络接口,允许Elasticsearch接受来自任何IP地址的HTTP请求
node.name: node-1 指定 Elasticsearch 节点的名称,Elasticsearch集群中的每个节点必须有一个唯一的名称
cluster.name: es-cluster 指定 Elasticsearch 集群的名称,属于同一集群的节点应该配置相同的集群名称
path.data: /data/elasticsearch-8.4.3/data 指定 Elasticsearch 数据存储路径
path.logs: /data/elasticsearch-8.4.3/logs 指定 Elasticsearch 日志存储路径
xpack.security.http.ssl.enabled: false 控制HTTP通信是否启用SSL/TLS加密,通过将其设置为“false”,HTTP流量的SSL/TLS加密被禁用
xpack.security.enabled: false 是否启用X-Pack安全特性;X-Pack是Elastic (Elasticsearch背后的公司)提供的一组商业功能,通过将其设置为“false”,将禁用这些安全功能
xpack.security.transport.ssl.enabled: false 控制是否为传输层启用SSL/TLS加密,传输层处理Elasticsearch节点之间的通信;通过将其设置为“false”,在节点到节点的通信中禁用SSL/TLS加密
http.cors.enabled: true 开启Elasticsearch HTTP服务的CORS (Cross-Origin Resource Sharing)功能;CORS是一个安全特性,允许从运行在不同域上的web浏览器控制访问Elasticsearch资源
http.cors.allow-origin: “*” 指定CORS请求允许的源(域);’ * ’ 通配符表示允许任何域向Elasticsearch发出CORS请求
http.cors.allow-headers: Authorization,X-Requested-With,Content-Type,Content-Length 定义了CORS请求允许的HTTP头列表;CORS请求Elasticsearch时,允许使用指定的头,如Authorization、X-Requested-With、Content-Type、Content-Length
# 在Docker中运行Elasticsearch 8.4.3镜像
docker run --name elasticsearch --net=host --restart=always -d -e ES_JAVA_OPTS="-Xms2048m -Xmx2048m" -v /data/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /data/es/logs:/data/elasticsearch-8.4.3/logs -v /data/es/data:/data/elasticsearch-8.4.3/data elasticsearch:8.4.3
# 验证 elasticsearch 服务是否正常运行,elasticsearch 默认使用 9200 端口进行http请求
curl http://localhost:9200
# 在浏览器的地址栏输入https://localhost:9200
# Elasticsearch-head可视化工具查看内容
# 创建network
docker network create elastic
# 拉取镜像,如拉取版本8.12.2
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.12.2
# 创建挂载目录,赋予读写权限
mkdir -p ./es/{data,config,plugins}
chmod -R 777 ./es
# es部署目录下创建docker-compose.yml文件
version: '3.8'
services:
es01:
container_name: es01
image: docker.elastic.co/elasticsearch/elasticsearch:8.12.2
environment:
- discovery.type=single-node
- ELASTIC_PASSWORD=sc2024
- TZ=Asia/Shanghai
ports:
- "9200:9200"
- "9300:9300"
mem_limit: 1g
volumes:
- ./es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./es/plugins:/usr/share/elasticsearch/plugins
- ./es/data:/usr/share/elasticsearch/data
networks:
- elastic
networks:
elastic:
external: true
# 配置host为0.0.0.0 允许远程访问
http.host: 0.0.0.0
# 跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
# 开启密码访问,如果不开启密码访问则设置为false
xpack.security.enabled: true
# docker-compose.yml的同级目录下执行如下命令启动服务
docker-compose up -d 或 docker compose up -d
#默认用户名 elastic
用户名:elastic
# 密码在docker-compose.yml 中配置的环境变量ELASTIC_PASSWORD的值
密码:your password
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --net=elastic --name elasticsearch -v /data/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /data/es/data:/usr/share/elasticsearch/data -v /data/es/plugins:/usr/share/elasticsearch/plugins docker.elastic.co/elasticsearch/elasticsearch:8.4.3
version: '3.8'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:8.4.3
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=-Xms2048m -Xmx2048m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es/data01:/usr/share/elasticsearch/data
- ./es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./es/plugins:/usr/share/elasticsearch/plugins
ports:
- 9200:9200
- 9300:9300
networks:
- elastic
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:8.4.3
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=-Xms2048m -Xmx2048m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es/data02:/usr/share/elasticsearch/data
- ./es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./es/plugins:/usr/share/elasticsearch/plugins
networks:
- elastic
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:8.4.3
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=-Xms2048m -Xmx2048m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es/data03:/usr/share/elasticsearch/data
- ./es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./es/plugins:/usr/share/elasticsearch/plugins
networks:
- elastic
volumes:
data01:
driver: local
data02:
driver: local
data03:
driver: local
networks:
elastic:
# driver: bridge
external: true
docker-compose.yml 使用环境变量 ES_JAVA_OPTS 手工设置堆大小为 512 MB。我们不推荐在生产环境使用 ES_JAVA_OPTS。
Docker Compose 文件,提供了一个三节点 Elasticsearch 集群。节点 es01 监听 localhost:9200,es02 和 es03 通过 Docker 网络与 es01 通信
此配置在所有网络接口上暴露端口 9200,Elasticsearch 集群可以公开访问,可能会忽略任何防火墙设置。如果不想暴露端口 9200,转而使用反向代理,在 docker-compose.yml 文件中用 127.0.0.1:9200:9200 替代 9200:9200。Elasticsearch 将只能从主机自身访问。
Docker 命名卷 data01、data02 和 data03 存储节点数据目录,以便重启时数据持续存在。如果他们不存在,docker-compose 将会在你创建集群时创建他们
# 启动集群,使用 docker-compose up 重启集群,Docker 卷中的数据将被保存和加载
docker-compose up
# 提交请求 _cat/nodes 查看节点是否启动运行
curl -X GET "localhost:9200/_cat/nodes?v=true&pretty"
# 停止集群
docker-compose down
# 在停止集群时删除数据卷,指定 -v 选项
docker-compose down -v
from elasticsearch import Elasticsearch
ES_HOSTS = 'http://localhost:9200'
# maxsize设置连接池中每个节点的最大连接数,timeout设置请求超时时间,retry_on_timeout超时后重试请求
def initialize_es(ES_HOSTS):
es_client = Elasticsearch(hosts=ES_HOSTS,maxsize=1000,timeout=30,retry_on_timeout=True)
health = es_client.cluster.health() # 检查ES集群健康状态
return es_client
def create_es_index(es_client, index_name, dims=1024):
if not es_client.indices.exists(index=index_name):
mapping = {
"mappings": {
"properties": {
"id": {"type": "keyword"},
"embeddings": {
"type": "dense_vector",
"dims": dims,
"index": True,
"similarity": "cosine"
},
"original_path": {"type": "keyword"},
"thumbnail_path": {"type": "keyword"}
}
}
}
es_client.indices.create(index=index_name, body=mapping)
doc = {"id": image_id,"embeddings": vector,"original_path": original_url,"thumbnail_path": thumbnail_url}
es_client.index(index=index_name, id=doc['id'], document=doc)
es_client.indices.create(index=index_name, body=mapping) # 创建索引
index_info = es_client.indices.get(index=index_name) # 查询索引
if es.indices.exists(index=index_name): # 判断索引是否存在
es.indices.delete(index=index_name) # 删除索引
es_client.index(index=index_name, id=doc['id'], document=doc) # 添加数据,更新数据(如果当前id存在则为更新)
res = es_client.search(index=index_name, body=query) # 查询数据,根据查询方式更改query