Elasticsearch 7.17 是 7.x 系列的最终维护版本,目前仍有大量生产环境集群运行此版本。本指南将详细介绍如何在 Debian 12/13 上完整部署 ELK 7.17 栈(Elasticsearch、Logstash、Kibana)。
环境要求
- Debian 12(bookworm)或 Debian 13(trixie)
- 至少 4GB 内存(默认堆内存占用约 2.4GB)
- 开放端口:9200(Elasticsearch HTTP)、9300(Elasticsearch 传输)、5601(Kibana)、9600(Logstash)
- 稳定的网络连接用于从 Elastic 仓库拉取包
添加 Elastic 7.x 仓库
导入 GPG 密钥并配置 APT 源:
1curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic-7.x.gpg 2echo "deb [signed-by=/usr/share/keyrings/elastic-7.x.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list 3sudo apt update 4
安装配置 Elasticsearch
安装核心组件并调整配置:
1sudo apt install -y elasticsearch 2
编辑主配置文件 /etc/elasticsearch/elasticsearch.yml:
1cluster.name: elk-lab 2node.name: node-1 3network.host: 127.0.0.1 4discovery.type: single-node 5
调整 JVM 堆内存(建议设置为可用内存的 50%):
1sudo vi /etc/elasticsearch/jvm.options 2
修改以下参数:
1-Xms2g 2-Xmx2g 3
启动服务并验证状态:
1sudo systemctl enable --now elasticsearch 2curl -s http://127.0.0.1:9200/_cluster/health?pretty 3
安装配置 Kibana
安装并修改配置文件 /etc/kibana/kibana.yml:
1server.port: 5601 2server.host: "0.0.0.0" 3elasticsearch.hosts: ["http://127.0.0.1:9200"] 4
启动服务并访问界面:
1sudo systemctl enable --now kibana 2
通过浏览器访问 http://服务器IP:5601
安装配置 Logstash
安装后创建示例配置文件 /etc/logstash/conf.d/sample.conf:
1input { 2 beats { 3 port => 5044 4 } 5} 6output { 7 elasticsearch { 8 hosts => ["http://127.0.0.1:9200"] 9 } 10} 11
启动服务:
1sudo systemctl enable --now logstash 2
在 Debian 13 上安装 rsyslog
Debian 13 默认不包含 /var/log/syslog,需安装 rsyslog:
1sudo apt install -y rsyslog 2sudo systemctl enable --now rsyslog 3
安装后,rsyslog 会生成 /var/log/syslog 并开始记录日志。
创建 Syslog 管道
Logstash 管道配置文件位于 /etc/logstash/conf.d/,创建 syslog 管道:
1sudo vi /etc/logstash/conf.d/syslog.conf 2
添加以下配置:
1input { 2 file { 3 path => "/var/log/syslog" 4 start_position => "beginning" 5 sincedb_path => "/var/lib/logstash/sincedb_syslog" 6 type => "syslog" 7 } 8} 9 10filter { 11 grok { 12 match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" } 13 } 14 date { 15 match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ] 16 } 17} 18 19output { 20 elasticsearch { 21 hosts => ["http://127.0.0.1:9200"] 22 index => "syslog-%{+YYYY.MM.dd}" 23 } 24} 25
为 Logstash 用户添加读取 syslog 的权限:
1sudo usermod -aG adm logstash 2
启动并启用 Logstash:
1sudo systemctl enable --now logstash 2
检查 Logstash 日志确认管道启动:
1sudo journalctl -u logstash --no-pager -n 20 2
验证 Logstash 监控 API:
1curl -s http://127.0.0.1:9600?pretty 2
检查 Elasticsearch 是否接收数据:
1curl -s 'http://127.0.0.1:9200/_cat/indices?v' 2
在CentOS 7上安装ELK Stack
确认syslog索引数据已成功导入Elasticsearch,可通过以下命令检查索引状态:
1health status index uuid pri rep docs.count docs.deleted store.size pri.store.size 2green open syslog-2026.03.29 QwErTyUiOpAsDfGhJkLm 1 0 1847 0 1.1mb 1.1mb 3
在Kibana中配置索引模式
通过API快速创建索引模式:
1curl -s -X POST "http://127.0.0.1:5601/api/saved_objects/index-pattern/syslog-*" \ 2 -H "kbn-xsrf: true" \ 3 -H "Content-Type: application/json" \ 4 -d '{"attributes":{"title":"syslog-*","timeFieldName":"@timestamp"}}' 5
登录Kibana网页界面,左侧菜单选择"Discover",选择已创建的syslog-*索引模式。可查看已解析的字段如syslog_program、syslog_hostname等。
命令行操作Elasticsearch
查看所有索引:
1curl -s 'http://127.0.0.1:9200/_cat/indices?v&s=index' 2
搜索特定日志条目(如sshd):
1curl -s 'http://127.0.0.1:9200/syslog-*/_search?pretty' \ 2 -H 'Content-Type: application/json' \ 3 -d '{"query":{"match":{"syslog_program":"sshd"}},"size":3}' 4
统计索引文档总数:
1curl -s 'http://127.0.0.1:9200/syslog-*/_count?pretty' 2
日志源差异处理
对于不同Linux发行版:
- Debian 12默认安装rsyslog,日志文件位于
/var/log/syslog - Debian 13默认使用systemd-journald,需手动安装rsyslog或改用Filebeat采集journald日志
如需从journald直接采集日志,可参考Filebeat+Logstash的配置方案,其他组件(包名、配置文件路径等)在不同版本中保持相同。
安全加固建议
- 配置防火墙规则限制访问源IP
- 为 Elasticsearch 和 Kibana 启用基础认证
- 定期备份
/var/lib/elasticsearch目录 - 监控系统日志
/var/log/elasticsearch/elk-lab.log
常见问题排查
- 端口冲突:检查
netstat -tulnp | grep <端口号> - 内存不足:观察
journalctl -u elasticsearch中的 OOM 错误 - 启动超时:增加 systemd 服务的
TimeoutStartSec参数
该部署方案已通过 Debian 12/13 和 ELK 7.17.29 的完整测试验证。如需升级到 8.x 版本,建议参考 Elastic 官方迁移指南进行操作。
《在 Debian 上部署 ELK 7.17 完整指南》 是转载文章,点击查看原文。