ELK 企业日志分析系统部署
ELK 企业日志分析系统部署
一、ELK 概述
1. 核心组件
- Elasticsearch(ES):分布式搜索与分析引擎,负责数据存储、索引和快速查询。
- Logstash:数据处理管道,负责日志收集、过滤、格式化并传输至 ES。
- Kibana:可视化平台,提供 Web 界面用于数据探索、图表展示和集群管理。
- 辅助工具:Elasticsearch-head(集群可视化管理插件)、Beats(轻量级数据收集工具,文档中未实操)。
2. 工作流程
日志/数据源 → Logstash(收集、过滤)→ Elasticsearch(存储、索引)→ Kibana(可视化分析)→ 浏览器展示
3. 实验环境
| 主机名 | IP 地址 | 操作系统 | 核心软件 | 端口说明 |
|---|---|---|---|---|
| node1 | <192.168.108.41> | CentOS 7.9 | Elasticsearch、Kibana | 9200(ES)、5601(Kibana) |
| node2 | <192.168.108.42> | CentOS 7.9 | Elasticsearch | 9200(ES) |
| apache | <192.168.108.43> | CentOS 7.9 | Logstash、Apache(httpd) | 9600(Logstash) |
二、Elasticsearch 集群部署(node1 + node2)
1. 环境准备(两节点均执行)
(1)配置域名解析
Bash
[root@node1 ~]# vi /etc/hosts
192.168.108.41 node1
192.168.108.42 node2
[root@node2 ~]# vi /etc/hosts
192.168.108.41 node1
192.168.108.42 node2
(2)验证 Java 环境
Bash
[root@node1 ~]# java -version # 需确保为 OpenJDK 1.8.0_262 及以上
[root@node2 ~]# java -version
2. 安装 Elasticsearch(两节点均执行)
(1)安装 RPM 包
Bash
[root@node1 ~]# cd /opt
[root@node1 opt]# rpm -ivh elasticsearch-5.5.0.rpm # 上传包至 /opt 目录
[root@node2 ~]# cd /opt
[root@node2 opt]# rpm -ivh elasticsearch-5.5.0.rpm
(2)配置系统服务
Bash
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl enable elasticsearch.service
[root@node2 ~]# systemctl daemon-reload
[root@node2 ~]# systemctl enable elasticsearch.service
(3)修改主配置文件
Bash
# 备份配置文件
[root@node1 ~]# cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
[root@node2 ~]# cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
# 编辑配置文件(node1 和 node2 配置一致,仅 node.name 区分)
[root@node1 ~]# vi /etc/elasticsearch/elasticsearch.yml
cluster.name: my-elk-cluster # 集群名称(两节点一致)
node.name: node1 # node2 此处改为 node2
path.data: /data/elk_data # 数据存储路径
path.logs: /var/log/elasticsearch/ # 日志路径
bootstrap.memory_lock: false # 不锁定物理内存
network.host: 0.0.0.0 # 监听所有地址
http.port: 9200 # 服务端口
discovery.zen.ping.unicast.hosts: ["node1", "node2"] # 集群节点发现
[root@node2 ~]# vi /etc/elasticsearch/elasticsearch.yml
# 同 node1 配置,仅 node.name 改为 node2
(4)创建数据目录并授权
Bash
[root@node1 ~]# mkdir -p /data/elk_data
[root@node1 ~]# chown elasticsearch:elasticsearch /data/elk_data/
[root@node2 ~]# mkdir -p /data/elk_data
[root@node2 ~]# chown elasticsearch:elasticsearch /data/elk_data/
(5)启动服务并验证
Bash
[root@node1 ~]# systemctl start elasticsearch.service
[root@node1 ~]# netstat -antp | grep 9200 # 检查端口监听
[root@node2 ~]# systemctl start elasticsearch.service
[root@node2 ~]# netstat -antp | grep 9200
(6)集群状态验证(浏览器访问)
- 节点信息:http://192.168.108.41:9200 和 http://192.168.108.42:9200
- 集群健康:http://192.168.108.41:9200/_cluster/health?pretty(status 为 green 正常)
- 集群状态:http://192.168.108.41:9200/_cluster/state?pretty
3. 安装 Elasticsearch-head 插件(两节点均执行)
(1)安装 Node.js 依赖
Bash
[root@node1 ~]# yum install gcc gcc-c++ make -y
[root@node1 ~]# cd /opt
[root@node1 opt]# tar xzvf node-v8.2.1.tar.gz # 上传包至 /opt
[root@node1 opt]# cd node-v8.2.1/
[root@node1 node-v8.2.1]# ./configure
[root@node1 node-v8.2.1]# make -j4 # 耗时较长
[root@node1 node-v8.2.1]# make install
# node2 执行相同步骤
[root@node2 ~]# yum install gcc gcc-c++ make -y
[root@node2 ~]# cd /opt
[root@node2 opt]# tar xzvf node-v8.2.1.tar.gz
[root@node2 opt]# cd node-v8.2.1/
[root@node2 node-v8.2.1]# ./configure
[root@node2 node-v8.2.1]# make -j4
[root@node2 node-v8.2.1]# make install
(2)安装 PhantomJS
Bash
[root@node1 ~]# cd /usr/local/src/
[root@node1 src]# tar xjvf phantomjs-2.1.1-linux-x86_64.tar.bz2 # 上传包至该目录
[root@node1 src]# cd phantomjs-2.1.1-linux-x86_64/bin
[root@node1 bin]# cp phantomjs /usr/local/bin
# node2 执行相同步骤
[root@node2 ~]# cd /usr/local/src/
[root@node2 src]# tar xjvf phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@node2 src]# cd phantomjs-2.1.1-linux-x86_64/bin
[root@node2 bin]# cp phantomjs /usr/local/bin
(3)安装 Elasticsearch-head
Bash
[root@node1 ~]# cd /usr/local/src/
[root@node1 src]# tar xzvf elasticsearch-head.tar.gz # 上传包至该目录
[root@node1 src]# cd elasticsearch-head/
[root@node1 elasticsearch-head]# npm install # 安装依赖
# 开启 ES 跨域访问
[root@node1 ~]# vi /etc/elasticsearch/elasticsearch.yml
# 末尾添加
http.cors.enabled: true
http.cors.allow-origin: "*"
[root@node1 ~]# systemctl restart elasticsearch # 重启 ES
# 启动 head 插件(后台运行)
[root@node1 elasticsearch-head]# npm run start &
[root@node1 elasticsearch-head]# netstat -lnupt | grep 9100 # 检查 9100 端口
# node2 执行相同步骤(解压、npm install、修改 ES 配置、启动)
(4)验证 head 插件(浏览器访问)
- 访问 http://192.168.108.41:9100/,输入 ES 地址 http://192.168.108.41:9200 点击连接
- 集群健康值显示 green 即为成功
三、Logstash 部署(apache 节点)
1. 环境准备
(1)关闭防火墙和 SELinux
Bash
[root@apache ~]# systemctl stop firewalld.service
[root@apache ~]# setenforce 0
(2)安装 Apache 服务
Bash
[root@apache ~]# yum -y install httpd
[root@apache ~]# systemctl start httpd
[root@apache ~]# systemctl enable httpd
(3)验证 Java 环境
Bash
[root@apache ~]# java -version # 需为 OpenJDK 1.8.0_262 及以上
2. 安装 Logstash
Bash
[root@apache ~]# cd /opt
[root@apache opt]# rpm -ivh logstash-5.5.1.rpm # 上传包至 /opt
[root@apache opt]# systemctl start logstash.service
[root@apache opt]# systemctl enable logstash.service
[root@apache opt]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/ # 建立软连接
3. Logstash 功能测试
(1)标准输入输出测试
Bash
[root@apache ~]# logstash -e 'input { stdin{} } output { stdout{} }'
# 输入 www.baidu.com 测试输出,按 Ctrl+C 退出
(2)详细输出测试(rubydebug 格式)
Bash
[root@apache ~]# logstash -e 'input { stdin{} } output { stdout{ codec=>rubydebug } }'
# 输入 www.baidu.com 查看结构化输出,按 Ctrl+C 退出
(3)对接 Elasticsearch 测试
Bash
[root@apache ~]# logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["192.168.108.41:9200"] } }'
# 输入 www.baidu.com、www.qq.com 等,通过 ES-head 查看索引(logstash-日期)
4. 配置 Logstash 收集日志
(1)收集系统日志(/var/log/messages)
Bash
[root@apache ~]# chmod o+r /var/log/messages # 授权日志文件读取权限
[root@apache ~]# vi /etc/logstash/conf.d/system.conf
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["192.168.108.41:9200"]
index => "system-%{+YYYY.MM.dd}" # 索引名按日期命名
}
}
[root@apache ~]# systemctl restart logstash.service # 重启服务
(2)收集 Apache 日志(访问日志 + 错误日志)
Bash
[root@apache ~]# cd /etc/logstash/conf.d/
[root@apache conf.d]# vi apache_log.conf
input {
file {
path => "/etc/httpd/logs/access_log"
type => "access"
start_position => "beginning"
}
file {
path => "/etc/httpd/logs/error_log"
type => "error"
start_position => "beginning"
}
}
output {
if [type] == "access" {
elasticsearch {
hosts => ["192.168.108.41:9200"]
index => "apache_access-%{+YYYY.MM.dd}"
}
}
if [type] == "error" {
elasticsearch {
hosts => ["192.168.108.41:9200"]
index => "apache_error-%{+YYYY.MM.dd}"
}
}
}
[root@apache conf.d]# /usr/share/logstash/bin/logstash -f apache_log.conf # 启动配置
(3)生成测试日志
Bash
# 访问 Apache 服务生成访问日志
[root@apache ~]# curl http://192.168.108.43
四、Kibana 部署(node1 节点)
1. 安装 Kibana
Bash
[root@node1 ~]# cd /usr/local/src/
[root@node1 src]# rpm -ivh kibana-5.5.1-x86_64.rpm # 上传包至该目录
2. 配置 Kibana
Bash
[root@node1 ~]# cd /etc/kibana/
[root@node1 kibana]# cp kibana.yml kibana.yml.bak
[root@node1 kibana]# vi kibana.yml
server.port: 5601 # Kibana 端口
server.host: "0.0.0.0" # 监听所有地址
elasticsearch.url: "http://192.168.108.41:9200" # 对接 ES 集群
kibana.index: ".kibana" # Kibana 自身索引
3. 启动服务并验证
Bash
[root@node1 kibana]# systemctl start kibana.service
[root@node1 kibana]# systemctl enable kibana.service
[root@node1 kibana]# netstat -lnupt | grep 5601 # 检查端口
4. Kibana 可视化配置(浏览器操作)
(1)访问 Kibana
(2)创建索引模式
- 点击左侧 Management → Index Patterns → Create index pattern
- 分别创建以下索引模式(匹配 Logstash 配置的索引名):
- 系统日志:
system-* - Apache 访问日志:
apache_access-* - Apache 错误日志:
apache_error-*
- 系统日志:
- 每个索引模式创建时,选择时间字段
@timestamp,点击 Create。
(3)查看日志数据
- 点击左侧 Discover,选择对应索引模式,即可查看结构化日志数据。
五、关键验证命令
1. ES 集群索引测试
Bash
# 创建测试索引
[root@node1 ~]# curl -XPUT 'localhost:9200/index-demo/test/1?pretty' -H 'Content-Type: application/json' -d '{"user":"zhangsan","mesg":"hello world"}'
# 查看索引(通过 ES-head 或 Kibana)
2. Logstash 配置文件验证
Bash
[root@apache ~]# logstash -t -f /etc/logstash/conf.d/system.conf # 验证配置文件正确性