容器化部署ES集群

一、ElasticSearch基本概念

在这里插入图片描述

1、索引(Index)

在ElasticSearch中,索引是文档的集合,类似于关系数据库中的数据库,它是文档的主要存储单位,每个文档都属于一个索引。

1、创建索引:

PUT /index_name

2、查看索引:

GET /index_name

3、删除索引:

DELETE /index_name

2、文档(Document)

在Elasticsearch中,文档是存储在索引中的基本数据单元。每个文档都有一个唯一的 ID,并且是以 JSON 格式存储的,一个文档对应一条数据。

1、创建文档:

POST /index_name/_doc/2783862

{
  "title": "实例文档",
  "content": "实例文档的内容..."
}
  • index_name:创建文档的索引,文档存放在索引中;
  • _doc:是文档类型,通常用 _doc;
  • 2783862:是文档的唯一 ID;

2、查看文档:

GET /index_name/_doc/2783862

这将返回包含文档内容的 JSON 响应,例如:

{
    "_index": "qinziteng666",
    "_type": "_doc",
    "_id": "2783862",
    "_version": 1,
    "_seq_no": 3,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "实例文档",
        "content": "实例文档的内容..."
    }
}

3、删除文档:

DELETE /index_name/_doc/2783862

4、查看索引中所有文档:

GET /index_name/_search

3、分片和副本

  • 分片(Shard):索引可以分成多个分片,每个分片是一个独立的Lucene索引。分片允许在集群中分布和并行处理数据。
  • 副本(Replica):每个分片可以有零个或多个副本。副本提供了数据的冗余备份和提高搜索性能的能力。

1、查看索引分片信息:

GET /_cat/shards/index_name?v

2、查看集群中所有索引的分片信息:

GET /_cat/shards?v

3、查看分片分配情况和状态:

GET /_cat/allocation?v

4、查看集群健康状态和分片情况:

GET /_cluster/health

5、查看集群中所有索引详细信息:

GET /_cluster/state

4、映射(Mapping)

映射定义了索引中每个字段的数据类型和属性。它类似于关系数据库中表的模式定义。

1、创建索引,并在请求体中定义索引的设置和映射

PUT /index_name
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "description": { "type": "text" },
      "created_at": { "type": "date" }
    }
  }
}
  • settings:包含了索引的设置,例如分片数和副本数;
  • mappings: 定义了索引中文档的结构,指定每个字段的类型和分析器等信息。

2、查看索引,返回索引的详细信息,包括设置、Mapping 和索引状态等。

GET /index_name

3、只查看映射:

GET /index_name/_mapping

4、更新映射

PUT /index_name/_mapping
{
  "properties": {
    "title": { "type": "text" },
    "tags": { "type": "keyword" },
    "views": { "type": "integer" }
  }
}

5、删除映射,直接删除索引即可

DELETE /index_name

二、容器部署ElasticSearch集群

1、环境信息:

IP地址ES节点
16.32.15.115node1
16.32.15.200node2
16.32.15.201node3

2、compose信息如下:

node1配置:

version: '3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.6
    container_name: elasticsearch
    restart: on-failure:3
    network_mode: host
    volumes:
      - /home/software/elasticsearch/data:/usr/share/elasticsearch/data
      - /home/software/elasticsearch/plugins:/usr/share/elasticsearch/plugins
      - /home/software/elasticsearch/logs:/usr/share/elasticsearch/logs
      - /etc/localtime:/etc/localtime
      - /etc/sysctl.conf:/etc/sysctl.conf
    environment:
      - node.name=node1
      - node.master=true
      - node.data=true
      - network.host=0.0.0.0
      - discovery.seed_hosts=16.32.15.115,16.32.15.200,16.32.15.201
      - cluster.initial_master_nodes=node1,node2,node3
      - cluster.name=es-cluster
      - ES_JAVA_OPTS=-Xms256m -Xmx256m

node2配置:

version: '3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.6
    container_name: elasticsearch
    restart: on-failure:3
    network_mode: host
    volumes:
      - /home/software/elasticsearch/data:/usr/share/elasticsearch/data
      - /home/software/elasticsearch/plugins:/usr/share/elasticsearch/plugins
      - /home/software/elasticsearch/logs:/usr/share/elasticsearch/logs
      - /etc/localtime:/etc/localtime
      - /etc/sysctl.conf:/etc/sysctl.conf
    environment:
      - node.name=node2
      - node.master=true
      - node.data=true
      - network.host=0.0.0.0
      - discovery.seed_hosts=16.32.15.115,16.32.15.200,16.32.15.201
      - cluster.initial_master_nodes=node1,node2,node3
      - cluster.name=es-cluster
      - ES_JAVA_OPTS=-Xms256m -Xmx256m

node3配置:

version: '3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.6
    container_name: elasticsearch
    restart: on-failure:3
    network_mode: host
    volumes:
      - /home/software/elasticsearch/data:/usr/share/elasticsearch/data
      - /home/software/elasticsearch/plugins:/usr/share/elasticsearch/plugins
      - /home/software/elasticsearch/logs:/usr/share/elasticsearch/logs
      - /etc/localtime:/etc/localtime
      - /etc/sysctl.conf:/etc/sysctl.conf
    environment:
      - node.name=node2
      - node.master=true
      - node.data=true
      - network.host=0.0.0.0
      - discovery.seed_hosts=16.32.15.115,16.32.15.200,16.32.15.201
      - cluster.initial_master_nodes=node1,node2,node3
      - cluster.name=es-cluster
      - ES_JAVA_OPTS=-Xms256m -Xmx256m

注意:如果是已经运行很久的单节点升级为集群(保留单节点ES数据),需要删除这台单节点的几个文件,如下:

  • data/nodes/0/_state/node-0.st
    • 这个文件记录了特定节点(例如节点编号为 0 的节点)的状态信息和元数据。
    • 包括节点的唯一标识符、IP 地址、端口号等基本信息。
    • 集群配置信息,如节点所属的集群名称、集群的一些全局设置。
    • 索引的元数据,包括映射(mapping)、分片(shard)的分配情况、每个分片所在的节点等。
    • 分片的状态信息,如每个分片的健康状态、复制状态等。
    • 路由表信息,记录了集群中每个索引分片的路由信息,用于数据的定位和检索。
  • data/nodes/0/_state/manifest-0.st
    • 这个文件是 Elasticsearch 的数据文件描述符(manifest)。
    • 主要用于描述和管理索引和分片的元数据信息。
    • 记录了每个索引的结构信息、分片的位置和状态、每个分片的文件列表等。
    • 提供了索引数据的元信息,帮助 Elasticsearch 了解如何访问和处理索引数据。

三、容器部署ElasticSearch伪集群

PS:伪集群只在单台服务器中部署的ES集群,并没有分散开,当次服务器宕机后ES集群也就没了,生产环境建议走上面的分散到不通服务器的集群,如果只是测试,伪集群是很好的选择。

1、创建ES集群所需目录

mkdir /home/es/{node1/{data,plugins,logs},node2/{data,plugins,logs},node3/{data,plugins,logs}} -p
chmod -R 777 /home/es

2、创建docker-compose.yaml文件 并启动集群

version: '3'
services:
  es-node1:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.6
    container_name: es-node1
    restart: on-failure:3
    volumes:
      - /home/es/node1/data:/usr/share/elasticsearch/data
      - /home/es/node1/plugins:/usr/share/elasticsearch/plugins
      - /home/es/node1/logs:/usr/share/elasticsearch/logs
      - /etc/sysctl.conf:/etc/sysctl.conf
    environment:
      - node.name=es-node1
      - node.master=true
      - node.data=true
      - network.host=0.0.0.0
      - discovery.seed_hosts=es-node1,es-node2,es-node3
      - cluster.initial_master_nodes=es-node1,es-node2,es-node3
      - cluster.name=es-cluster
      - ES_JAVA_OPTS=-Xms256m -Xmx256m
    ports:
      - 9200:9200

  es-node2:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.6
    container_name: es-node2
    restart: on-failure:3
    volumes:
      - /home/es/node2/data:/usr/share/elasticsearch/data
      - /home/es/node2/plugins:/usr/share/elasticsearch/plugins
      - /home/es/node2/logs:/usr/share/elasticsearch/logs
      - /etc/sysctl.conf:/etc/sysctl.conf
    environment:
      - node.name=es-node2
      - node.master=true
      - node.data=true
      - network.host=0.0.0.0
      - discovery.seed_hosts=es-node1,es-node2,es-node3
      - cluster.initial_master_nodes=es-node1,es-node2,es-node3
      - cluster.name=es-cluster
      - ES_JAVA_OPTS=-Xms256m -Xmx256m
    ports:
      - 8200:9200

  es-node3:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.6
    container_name: es-node3
    restart: on-failure:3
    volumes:
      - /home/es/node3/data:/usr/share/elasticsearch/data
      - /home/es/node3/plugins:/usr/share/elasticsearch/plugins
      - /home/es/node3/logs:/usr/share/elasticsearch/logs
      - /etc/sysctl.conf:/etc/sysctl.conf
    environment:
      - node.name=es-node3
      - node.master=true
      - node.data=true
      - network.host=0.0.0.0
      - discovery.seed_hosts=es-node1,es-node2,es-node3
      - cluster.initial_master_nodes=es-node1,es-node2,es-node3
      - cluster.name=es-cluster
      - ES_JAVA_OPTS=-Xms256m -Xmx256m
    ports:
      - 7200:9200

3、查看集群

curl http://localhost:9200/_cat/nodes

在这里插入图片描述

  • 10
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇的海马体

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值