Elastic Stack 安装与配置指南

2025年4月3日 | 版本 8.12

简介

Elastic Stack (原 ELK Stack) 是一套开源的数据分析工具集合,包括 Elasticsearch、Kibana、Logstash 和 Beats。本指南将详细介绍如何安装和配置这些组件。

🔍

Elasticsearch

分布式搜索和分析引擎,用于存储、搜索和分析大量数据。

📊

Kibana

数据可视化平台,用于探索和可视化 Elasticsearch 中的数据。

🔄

Logstash

服务器端数据处理管道,用于从多个来源采集数据并转换后发送到 Elasticsearch。

📡

Beats

轻量级数据采集器,用于将各种类型的数据发送到 Elasticsearch 或 Logstash。

系统要求

硬件要求

  • CPU: 至少 2 核 (生产环境推荐 4 核以上)
  • 内存: 至少 4GB (生产环境推荐 8GB 以上)
  • 存储: SSD 硬盘,至少 10GB 可用空间

软件要求

  • 操作系统: Linux, macOS 或 Windows
  • Java: JDK 17 或更高版本
  • Python: 3.7 或更高版本 (某些 Beats 需要)

注意: 生产环境建议使用专用服务器或云实例,不要与其他资源密集型应用共享资源。

安装 Elasticsearch

Linux (Debian/Ubuntu)

  1. 导入 Elasticsearch GPG 密钥:
    wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch  | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
  2. 添加 Elasticsearch 仓库:
    echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg]  https://artifacts.elastic.co/packages/8.x/apt  stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
  3. 更新软件包列表并安装:
    sudo apt update 
    sudo apt install elasticsearch
  4. 启动 Elasticsearch 服务:
    sudo systemctl start elasticsearch 
    sudo systemctl enable elasticsearch

macOS

  1. 使用 Homebrew 安装:
    brew tap elastic/tap 
    brew install elastic/tap/elasticsearch-full
  2. 启动 Elasticsearch:
    brew services start elastic/tap/elasticsearch-full

Windows

  1. 下载 Windows ZIP 包: 下载 Elasticsearch
  2. 解压 ZIP 文件到指定目录
  3. 运行 Elasticsearch:
    .\bin\elasticsearch.bat

验证安装

打开浏览器访问 http://localhost:9200,应该能看到类似以下响应:

{
  "name" : "your-hostname",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "xxxxxx",
  "version" : {
    "number" : "8.12.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "xxxxxx",
    "build_date" : "2025-03-15T10:00:00.000Z",
    "build_snapshot" : false,
    "lucene_version" : "9.8.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

配置 Elasticsearch

主要配置文件

Elasticsearch 的主要配置文件位于:

  • Linux: /etc/elasticsearch/elasticsearch.yml
  • macOS: /usr/local/etc/elasticsearch/elasticsearch.yml
  • Windows: config\elasticsearch.yml

重要配置项

  1. 集群名称 (生产环境应修改):
    cluster.name:  my-elasticsearch-cluster
  2. 节点名称 (建议设置为有意义的名字):
    node.name:  node-1
  3. 网络绑定 (允许远程访问):
    network.host:  0.0.0.0

    警告: 生产环境中不要使用 0.0.0.0,应该配置具体的 IP 地址并设置防火墙规则。

  4. HTTP 端口 (默认 9200):
    http.port:  9200
  5. 发现设置 (多节点集群需要配置):
    discovery.seed_hosts:  ["host1", "host2"]
    cluster.initial_master_nodes:  ["node-1", "node-2"]
  6. JVM 堆内存设置 (建议不超过物理内存的50%):

    修改 jvm.options 文件:

    -Xms4g 
    -Xmx4g

安全配置

Elasticsearch 8.x 默认启用安全功能,首次启动时会自动生成以下安全配置:

  • 自动生成的密码存储在 /etc/elasticsearch (Linux) 或安装目录的 config 文件夹中
  • HTTPS 和 HTTP 层安全自动配置
  • 内置用户 (elastic, kibana_system, logstash_system 等)

重置 elastic 用户密码:

bin/elasticsearch-reset-password -u elastic

生成新的 SSL 证书:

bin/elasticsearch-certutil cert -out config/elastic-certificates.p12 -pass ""

安装 Kibana

Linux (Debian/Ubuntu)

  1. 使用与 Elasticsearch 相同的仓库:
    sudo apt install kibana
  2. 启动 Kibana 服务:
    sudo systemctl start kibana 
    sudo systemctl enable kibana

macOS

  1. 使用 Homebrew 安装:
    brew install elastic/tap/kibana-full
  2. 启动 Kibana:
    brew services start elastic/tap/kibana-full

Windows

  1. 下载 Windows ZIP 包: 下载 Kibana
  2. 解压 ZIP 文件到指定目录
  3. 运行 Kibana:
    .\bin\kibana.bat

验证安装

打开浏览器访问 http://localhost:5601,应该能看到 Kibana 登录页面。

配置 Kibana

主要配置文件位于:

  • Linux: /etc/kibana/kibana.yml
  • macOS: /usr/local/etc/kibana/kibana.yml
  • Windows: config\kibana.yml

重要配置项:

server.port:  5601 
server.host:  "0.0.0.0"
elasticsearch.hosts:  ["http://localhost:9200"]
elasticsearch.username:  "kibana_system"
elasticsearch.password:  "your-kibana-system-password"

安装 Logstash

Linux (Debian/Ubuntu)

  1. 使用与 Elasticsearch 相同的仓库:
    sudo apt install logstash
  2. 启动 Logstash 服务:
    sudo systemctl start logstash 
    sudo systemctl enable logstash

macOS

  1. 使用 Homebrew 安装:
    brew install elastic/tap/logstash-full
  2. 启动 Logstash:
    brew services start elastic/tap/logstash-full

Windows

  1. 下载 Windows ZIP 包: 下载 Logstash
  2. 解压 ZIP 文件到指定目录
  3. 运行 Logstash:
    .\bin\logstash.bat

基本配置

创建一个简单的 Logstash 配置文件 logstash-simple.conf:

input {
  stdin { }
}
 
output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "logstash-%{+YYYY.MM.dd}" 
    user => "elastic"
    password => "your-elastic-password"
  }
  stdout { codec => rubydebug }
}

使用配置文件运行 Logstash:

bin/logstash -f logstash-simple.conf

安装 Beats

Beats 是轻量级数据采集器,有多种类型:

📄

Filebeat

采集日志文件

📊

Metricbeat

采集系统和服务指标

🔄

Packetbeat

网络流量分析

安装 Filebeat (Linux)

  1. 使用与 Elasticsearch 相同的仓库:
    sudo apt install filebeat
  2. 配置 Filebeat:

    编辑 /etc/filebeat/filebeat.yml:

    filebeat.inputs: 
    - type: log 
      enabled: true 
      paths:
        - /var/log/*.log 
     
    output.elasticsearch: 
      hosts: ["localhost:9200"]
      username: "elastic"
      password: "your-elastic-password"
  3. 启动 Filebeat:
    sudo systemctl start filebeat 
    sudo systemctl enable filebeat

安装 Metricbeat (Linux)

  1. 使用与 Elasticsearch 相同的仓库:
    sudo apt install metricbeat
  2. 配置 Metricbeat:

    编辑 /etc/metricbeat/metricbeat.yml:

    metricbeat.modules: 
    - module: system 
      metricsets:
        - cpu 
        - memory 
        - network 
        - process 
      enabled: true 
      period: 10s 
     
    output.elasticsearch: 
      hosts: ["localhost:9200"]
      username: "elastic"
      password: "your-elastic-password"
  3. 启动 Metricbeat:
    sudo systemctl start metricbeat 
    sudo systemctl enable metricbeat

常见问题排查

Elasticsearch 无法启动

  • 检查日志文件:
    journalctl -u elasticsearch --no-pager -n 50
  • 检查 Java 版本:
    java -version
  • 检查端口冲突:
    sudo netstat -tulnp | grep 9200

Kibana 无法连接 Elasticsearch

  • 检查 Elasticsearch 是否运行:
    curl -X GET "localhost:9200"
  • 验证 Kibana 配置中的 Elasticsearch URL 和凭据
  • 检查 Kibana 日志:
    journalctl -u kibana --no-pager -n 50

Logstash 管道问题

  • 使用测试模式运行 Logstash:
    bin/logstash -f your-config.conf  --config.test_and_exit
  • 增加日志级别:
    bin/logstash -f your-config.conf  --log.level  debug

下一步

学习资源

进阶配置

  • 设置索引生命周期管理 (ILM)
  • 配置索引模板
  • 设置用户认证和角色权限
  • 配置集群监控
  • 设置备份和恢复