千家信息网

CentOS 8 部署 ELK日志分析 平台

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,需求1.开发人员不能登录线上服务器查看日志2.各个系统都有日志,日志分散难以查找3.日志数据量大,查找慢,数据不够实时解决办法:部署ELK平台ELK介绍ELK是三个开源软件的缩写,分别表示:Elast
千家信息网最后更新 2025年12月02日CentOS 8 部署 ELK日志分析 平台

需求

1.开发人员不能登录线上服务器查看日志
2.各个系统都有日志,日志分散难以查找
3.日志数据量大,查找慢,数据不够实时

解决办法:部署ELK平台

ELK介绍

ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash 。

ELK架构图

Elasticsearch简介:

Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。
特点:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。

部署Elasticsearch

1.配置yum源

rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch #导入密钥
vim /etc/yum.repos.d/elasticsearch.repo #配置yum源

[elasticsearch-2.x]name=Elasticsearch repository for 2.x packagesbaseurl=http://packages.elastic.co/elasticsearch/2.x/centosgpgcheck=1gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearchenable=1

2.安装elasticsearch

yum install elasticsearch -y #安装elasticsearch

3.配置Elasticsearch

vim /etc/elasticsearch/elasticsearch.yml

cluster.name: yltx    #17行 集群名称node.name: node1   #23行 节点名称path.data: /data/es-data   #33行工作目录path.logs: /var/log/elasticsearch  #37行日志目录bootstrap.memory_lock: true    #43行 防止交换swap分区network.host: 0.0.0.0    #54行 监听网络http.port: 9200   #58行 端口

mkdir -p /data/es-data
chown -R elasticsearch:elasticsearch /data/es-data/

4.内存解锁和文件限制

生产环境中必须要修改(注意)

vim /etc/security/limits.conf

末尾插入elasticsearch soft memlock unlimited   elasticsearch hard memlock unlimited   * soft nofile 65535        * hard nofile 65535

systemctl start elasticsearch.service #启动服务
netstat -ntap | grep 9200
ps -ef |grep elasticsearch

网页测试:http://192.168.0.102:9200/

安装Elasticsearch - head插件

/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head

网页访问:

http://192.168.0.102:9200/_plugin/head/

Logstash 介绍:

Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。
logstash收集日志基本流程: input-->codec-->filter-->codec-->output
1.input:从哪里收集日志。
2.filter:发出去前进行过滤
3.output:输出至Elasticsearch或Redis消息队列
4.codec:输出至前台,方便边实践边测试
5.数据量不大日志按照月来进行收集

部署Logstash

1.配置yum源

vim /etc/yum.repos.d/logstash.repo

[logstash-2.1]name=Logstash repository for 2.1.x packagesbaseurl=http://packages.elastic.co/logstash/2.1/centosgpgcheck=1gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearchenable=1

2.下载安装logstash

yum install logstash -y

测试logstash

logstash的基本语法

input {
指定输入
}

output {
指定输出
}

1.测试标准输入输出

使用rubydebug方式前台输出展示以及测试
/opt/logstash/bin/logstash -e 'input { stdin {} } output { stdout { codec => rubydebug} }'
hello #输入hello测试

2.测试输出到文件

/opt/logstash/bin/logstash -e 'input { stdin {} } output { file { path => "/tmp/test-%{+YYYY.MM.dd}.log"} }'
cat /tmp/test-2020.02.17.log

3.开启日志压缩

/opt/logstash/bin/logstash -e 'input { stdin {} } output { file { path => "/tmp/test-%{+YYYY.MM.dd}.log.tar.gz" gzip => true } }'
ll /tmp/

4.测试输出到elasticsearch

/opt/logstash/bin/logstash -e 'input { stdin {} } output { elasticsearch { hosts => ["192.168.0.102:9200"] index => "logstash-test-%{+YYYY.MM.dd}" } }'
ll /data/es-data/yltx/nodes/0/indices


5.网页验证

Kibana 简介

Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。

Kibana 部署

1.下载安装kibana

wget https://artifacts.elastic.co/downloads/kibana/kibana-7.6.0-linux-x86_64.tar.gz
tar zxvf kibana-7.6.0-linux-x86_64.tar.gz -C /opt/
mv /opt/kibana-7.6.0-linux-x86_64/ /usr/local/kibana

2.修改配置

vim /usr/local/kibana/config/kibana.yml

server.port: 5601           #2行 访问端口server.host: "0.0.0.0"   #5行 监听网络elasticsearch.url: "http://192.168.0.102:9200"    #12行 ES地址kibana.index: ".kibana"    #20行 

3.启动服务

/usr/local/kibana/bin/kibana &
netstat -ntap |grep 5601 #查看端口号

4.网页验证:

http://192.168.0.102:5601/


测试 ELK平台

收集系统日志和收集java异常日志

1.修改logstash配置文件:

vim /root/file.conf

input {            file {                    path => "/var/log/messages"     #收集系统日志                    type => "system"                    start_position => "beginning"            }            file {                    path => "/var/log/elasticsearch/yltx.log"   #收集java异常日志                    type => "es-error"                    start_position => "beginning"                    codec => multiline {                    pattern => "^\["                    negate => true                    what => "previous"                }            }}output {         if [type] == "system" {                 elasticsearch {                         hosts => ["192.168.0.102:9200"]                           index => "system-%{+YYYY.MM.dd}"                   }         }         if [type] == "es-error" {                 elasticsearch {                         hosts => ["192.168.0.102:9200"]                         index => "es-error-%{+YYYY.MM.dd}"                 }         }}

2.写入到elasticsearch中

/opt/logstash/bin/logstash -f /root/file.conf

3.查看Elasticsearch

4.查看Kibana


相关资料

ELK官网:https://www.elastic.co/cn/
中文指南:https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/details

日志 测试 数据 输出 配置 分析 网页 服务 工具 文件 方式 系统 搜索 输入 平台 分布式 前台 名称 服务器 架构 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 远程同乐自建服务器 广州智慧电梯软件开发费用 网络安全对用户应急的预案 株洲it软件开发师速成班 威海软件开发公司经营范围 中国电子学会网络安全标准 混响插件软件开发 清华大学出版社数据库技术 移动流量软件开发 厦门龙商网络技术服务有限 新罗区鑫洪源网络技术工作室 朝阳分局网络安全保卫 接口自动化数据库数据断言 济南计算软件开发工资待遇 访问同一个数据库需要加锁吗 软件开发螺旋图 渭南网络技术价目表 邮件服务器协议是 装oracle数据库 数据库领域技术的发展前景 河北省网络安全保卫工作条例 软件开发中的po是 云长网络技术有限公司 绿色SQL数据库路径 会计软件主流的软件开发商 为什么要用云服务器学linux 无锡新一代服务器哪个厂家质量好 汽车电子总线网络技术线束 怎么下载网络安全证书 我的世界奇思怪谈服务器
0