Akemi

两周复习EFK第一天之部署与访问ES集群

2026/03/23

EFK第一天之部署与访问ES集群

部署ES集群

首先进行ES集群的部署工作。我们使用Docker Compose来快速搭建一个ES集群,包含3个节点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: '3.8'
services:
node1:
image: docker.elastic.co/elasticsearch/elasticsearch:9.3.2
environment:
- cluster.name=es-cluster
- node.name=node1
- discovery.type=single-node
- cluster.initial_master_nodes=node1
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ports:
- "9200:9200"
volumes:
- ./data1:/usr/share/elasticsearch/data
networks:
- elastic

networks:
elastic:
driver: bridge

访问ES集群

启动集群后,我们可以通过以下方式访问ES:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 验证服务状态
curl -X GET "localhost:9200/_cluster/health?pretty"

# 查看集群信息
curl -X GET "localhost:9200/_cluster/state?pretty"

# 创建索引
curl -X PUT "localhost:9200/my-index" -H "Content-Type: application/json" -d'
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}'

# 插入测试数据
curl -X PUT "localhost:9200/my-index/_doc/1" -H "Content-Type: application/json" -d'
{
"title": "测试文档",
"content": "这是第一个测试文档",
"timestamp": "2026-03-23T16:00:00Z"
}'

# 查询数据
curl -X GET "localhost:9200/my-index/_search?pretty"

基本操作

ES提供丰富的API来进行数据操作:

1
2
3
4
5
6
7
8
9
# 查看所有索引
curl -X GET "localhost:9200/_cat/indices?v"

# 删除索引
curl -X DELETE "localhost:9200/my-index"

# 查看集群状态
curl -X GET "localhost:9200/_cat/master?v"
curl -X GET "localhost:9200/_cat/nodes?v"

安全配置

为了集群安全,需要配置基本的安全认证:

1
2
3
4
5
# 创建内置用户
docker exec node1 bin/elasticsearch-setup-passwords auto

# 或者手动设置密码
docker exec node1 bin/elasticsearch-setup-passwords interactive

通过上述配置,我们成功搭建了一个基本的ES集群,为后续的EFK集成打下了基础。

CATALOG
  1. 1. EFK第一天之部署与访问ES集群
    1. 1.1. 部署ES集群
    2. 1.2. 访问ES集群
    3. 1.3. 基本操作
    4. 1.4. 安全配置