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集成打下了基础。