前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >搭建ELK日志分析平台+Filebeat

搭建ELK日志分析平台+Filebeat

作者头像
用户8826052
修改于 2021-07-12 03:26:34
修改于 2021-07-12 03:26:34
8190
举报
文章被收录于专栏:编程乐园编程乐园

ELK介绍

需求背景

业务发展越来越庞大,服务器越来越多

各种访问日志、应用日志、错误日志量越来越多,导致运维人员无法很好的去管理日志

开发人员排查问题,需要到服务器上查日志,不方便

运营人员需要一些数据,需要我们运维到服务器上分析日志

为什么要用到ELK?

一般我们需要进行日志分析场景:直接在日志文件中 grep、awk 就可以获得自己想要的信息。但在规模较大也就是日志量多而复杂的场景中,此方法效率低下,面临问题包括日志量太大如何归档、文本搜索太慢怎么办、如何多维度查询。需要集中化的日志管理,所有服务器上的日志收集汇总。常见解决思路是建立集中式日志收集系统,将所有节点上的日志统一收集,管理,访问。

大型系统通常都是一个分布式部署的架构,不同的服务模块部署在不同的服务器上,问题出现时,大部分情况需要根据问题暴露的关键信息,定位到具体的服务器和服务模块,构建一套集中式日志系统,可以提高定位问题的效率。

一个完整的集中式日志系统,需要包含以下几个主要特点:

1)收集-能够采集多种来源的日志数据

2)传输-能够稳定的把日志数据传输到中央系统

3)存储-如何存储日志数据

4)分析-可以支持 UI 分析

5)警告-能够提供错误报告,监控机制

而ELK则提供了一整套解决方案,并且都是开源软件,之间互相配合使用,完美衔接,高效的满足了很多场合的应用。是目前主流的一种日志系统。

ELK简介

ELK是三个开源软件的缩写,分别为:Elasticsearch 、 Logstash以及Kibana , 它们都是开源软件。不过现在还新增了一个Beats,它是一个轻量级的日志收集处理工具(Agent),Beats占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具,目前由于原本的ELK Stack成员中加入了 Beats 工具所以已改名为Elastic Stack。

Elastic Stack包含:

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

详细可参考Elasticsearch权威指南

Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。

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

Beats在这里是一个轻量级日志采集器,其实Beats家族有6个成员,早期的ELK架构中使用Logstash收集、解析日志,但是Logstash对内存、cpu、io等资源消耗比较高。相比 Logstash,Beats所占系统的CPU和内存几乎可以忽略不计

ELK Stack (5.0版本之后)--> Elastic Stack == (ELK Stack + Beats)。目前Beats包含六种工具:

Packetbeat: 网络数据(收集网络流量数据)

Metricbeat: 指标 (收集系统、进程和文件系统级别的 CPU 和内存使用情况等数据)

Filebeat: 日志文件(收集文件数据)

Winlogbeat: windows事件日志(收集 Windows 事件日志数据)

Auditbeat:审计数据 (收集审计日志)

Heartbeat:运行时间监控 (收集系统运行时的数据)

ELK官网:https://www.elastic.co/cn/

中文指南:https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/details

环境准备

操作系统:CentOS Linux release 7.4.1708 (Core)

服务器IP:192.168.8.25

软件版本

elasticsearch:elasticsearch-6.4.0.tar.gz

kibana:kibana-6.4.0-linux-x86_64.tar.gz

logstash:logstash-6.4.0.tar.gz

filebeat:filebeat-6.4.0-linux-x86_64.tar.gz

JDK:JDK-1.8.0_181

一、基础环境配置及软件包下载

1、关闭防火墙和selinux

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# systemctl disable firewalld

[root@localhost ~]# setenforce 0

[root@localhost ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config

2、内核优化

[root@localhost ~]# vim /etc/security/limits.conf

#在文件最后添加以下内容

* soft nofile 65537

* hard nofile 65537

* soft nproc 65537

* hard nproc 65537

[root@localhost ~]# vim /etc/security/limits.d/20-nproc.conf

#修改以下内容

* soft nproc 4096

[root@localhost ~]# vim /etc/sysctl.conf

#添加以下内容

vm.max_map_count = 262144

net.core.somaxconn=65535

net.ipv4.ip_forward = 1

#执行sysctl -p使其生效

[root@localhost ~]# sysctl –p

3、安装JDK环境

[root@localhost ~]# wget https://mirrors.yangxingzhen.com/jdk/jdk-8u181-linux-x64.tar.gz

[root@localhost ~]# tar zxf jdk-8u181-linux-x64.tar.gz -C /usr/local

#配置/etc/profile,添加以下内容

[root@localhost ~]# vim /etc/profile

export JAVA_HOME=/usr/local/jdk1.8.0_181

export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin

[root@localhost ~]# source /etc/profile

#看到如下信息,java环境配置成功

[root@localhost ~]# java -version

java version "1.8.0_181"

Java(TM) SE Runtime Environment (build 1.8.0_181-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

二、安装elasticsearch

1、创建持久化目录及Logs日志目录

[root@localhost ~]# mkdir -p /data/elasticsearch/{data,logs}

2、下载elasticsearch软件包

[root@localhost ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.0.tar.gz

3、解压并重命名

[root@localhost ~]# tar zxf elasticsearch-6.4.0.tar.gz

[root@localhost ~]# mv elasticsearch-6.4.0 /usr/local/elasticsearch

4、修改elasticsearch.yml配置文件,文件内容如下

[root@localhost ~]# vim /usr/local/elasticsearch/config/elasticsearch.yml

node.name: localhost

path.data: /data/elasticsearch/data

path.logs: /data/elasticsearch/logs

network.host: 0.0.0.0

http.port: 9200

5、创建elk用户并授权

[root@localhost ~]# useradd elk

[root@localhost ~]# chown -R elk.elk /usr/local/elasticsearch/

[root@localhost ~]# chown -R elk.elk /data/elasticsearch/*

6、启动es服务(第一次先测试好在加-d后台启动)

[root@localhost ~]# su - elk

[elk@localhost ~]$ /usr/local/elasticsearch/bin/elasticsearch

7、后台启动es服务

[elk@localhost ~]$ /usr/local/elasticsearch/bin/elasticsearch -d

Elasticsearch常用命令

curl -XDELETE 'http://host.IP.address:9200/logstash-*' 删除索引(后面为索引名称)

curl -XGET 'host.IP.address:9200/_cat/health?v&pretty' 查看集群状态

curl -XGET 'host.IP.address:9200/_cat/indices?v&pretty' 查看索引

三、安装logstash

1、下载软件包

[root@localhost ~]# wget https://artifacts.elastic.co/downloads/logstash/logstash-6.4.0.tar.gz

2、解压并重命名

[root@localhost ~]# tar zxf logstash-6.4.0.tar.gz

[root@localhost ~]# mv logstash-6.4.0 /usr/local/logstash

3、编辑logstash.yml配置文件,添加以下内容

[root@localhost ~]# vim /usr/local/logstash/config/logstash.yml

config.reload.automatic: true

config.reload.interval: 10s

4、创建Nginx配置文件

input {

beats {

host => "0.0.0.0"

port => 5044

}

}

output {

stdout {

codec => rubydebug

}

elasticsearch {

hosts => ["192.168.8.25:9200"]

index => "nginx_access-%{+YYYY.MM.dd}"

}

}

5、启动logstash服务

[root@localhost ~]# chown -R elk.elk /usr/local/logstash

[root@localhost ~]# su - elk

#前台启动

[elk@localhost ~]$ /usr/local/logstash/bin/logstash -f /usr/local/logstash/conf/nginx.conf

#后台启动

[elk@localhost ~]$ nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/conf/nginx.conf &

四、安装filebeat

1、下载filebeat软件包

[root@localhost ~]# wget -c https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.4.0-linux-x86_64.tar.gz

2、解压并重命名

[root@localhost ~]# tar zxf filebeat-6.4.0-linux-x86_64.tar.gz

[root@localhost ~]# mv filebeat-6.4.0-linux-x86_64 /usr/local/filebeat

[root@localhost ~]# chown -R elk.elk /usr/local/filebeat/

3、编辑filebeat.yml配置文件,配置内容如下

[root@localhost ~]# vim /usr/local/filebeat/filebeat.yml

filebeat.inputs:

- type: log

enabled: true

paths:

- /usr/local/nginx/logs/*.log

fields:

service: localhost

log_type: log

server_id: 127.0.0.1

scan_frequerncy: 60

multiline.pattern: ^\{4}

multiline.negate: true

multiline.match: afte

filebeat.config.modules:

path: ${path.config}/modules.d/*.yml

reload.enabled: false

setup.template.settings:

index.number_of_shards: 3

setup.kibana:

output.elasticsearch:

hosts: ["192.168.8.25:9200"]

4、启动filebeat服务

[root@localhost filebeat]# su - elk

[elk@localhost filebeat]# cd /usr/local/filebeat

#前台启动

[elk@localhost filebeat]$ ./filebeat -e -c filebeat.yml

#后台启动

[elk@localhost filebeat]$ nohup ./filebeat -e -c filebeat.yml &

五、安装Kibana

1、下载Kibana软件包

[root@localhost ~]# wget https://artifacts.elastic.co/downloads/kibana/kibana-6.4.0-linux-x86_64.tar.gz

2、解压并重命名

[root@localhost ~]# tar zxf kibana-6.4.0-linux-x86_64.tar.gz

[root@localhost ~]# mv kibana-6.4.0-linux-x86_64 /usr/local/kibana

[root@localhost ~]# chown -R elk.elk /usr/local/kibana

3、编辑kibana.yml配置文件

[root@localhost ~]# vim /usr/local/kibana/config/kibana.yml

server.port: 5601

server.host: "192.168.8.25"

server.name: "XiaoQi-kibana"

elasticsearch.url: "http://192.168.8.25:9200"

kibana.index: ".kibana6"

4、启动Kibana服务

[root@localhost ~]# su - elk

#前台启动

[elk@localhost ~]$ /usr/local/kibana/bin/kibana

#后台启动

[elk@localhost ~]$ /usr/local/kibana/bin/kibana &

温馨提示:可以先前台启动查看日志,正常之后在后台启动。

5、访问Kibana

#浏览器访问:http://192.168.8.25:5601,出现如下界面

因为现在没有数据,我们现在配置Nginx进行Nginx的日志收集

六、安装Nginx并配置收集日志

1、下载Nginx软件包

[root@localhost ~]# wget https://mirrors.yangxingzhen.com/nginx/nginx-1.15.2.tar.gz

2、解压软件包

[root@localhost ~]# tar zxf nginx-1.15.2.tar.gz

3、预编译

[root@localhost ~]# cd nginx-1.15.2

[root@localhost nginx-1.15.2]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++

[root@localhost nginx-1.15.2]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module

4、编译及安装

[root@localhost nginx-1.15.2]# make && make install

5、编辑nginx.conf配置文件,配置内容如下

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

user www www;

worker_processes 2;

events {

worker_connections 1024;

}

http {

include mime.types;

log_format json '{"@timestamp":"$time_iso8601",'

'"host":"$server_addr",'

'"clientip":"$remote_addr",'

'"remote_user":"$remote_user",'

'"request":"$request",'

'"http_user_agent":"$http_user_agent",'

'"size":$body_bytes_sent,'

'"responsetime":$request_time,'

'"upstreamtime":"$upstream_response_time",'

'"upstreamhost":"$upstream_addr",'

'"http_host":"$host",'

'"requesturi":"$request_uri",'

'"url":"$uri",'

'"domain":"$host",'

'"xff":"$http_x_forwarded_for",'

'"referer":"$http_referer",'

'"status":"$status"}';

access_log logs/access.log json;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name localhost;

location / {

root html;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

6、启动Nginx服务

[root@localhost nginx-1.15.2]# useradd -s /sbin/nologin www

[root@localhost nginx-1.15.2]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost nginx-1.15.2]# /usr/local/nginx/sbin/nginx

7、配置Kibana

1)点击Managemen à Index Patterns

2)创建索引

#索引创建完毕

3)查看日志

至此,Nginx日志采集成功。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Python发送邮件
Python发邮件需要有SMTP服务,可以在本地搭建SMTP服务,也可以使用第三方的SMTP服务(比如网易邮箱或QQ邮箱)。 这里我使用了网易126邮箱来发邮件。
海天一树
2018/07/25
1.5K0
Python发送邮件
Python发送邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
青阳
2021/11/26
9060
【干货】用Python每天定时发送监控邮件
不管是在信贷领域还是支付领域,作为一个风控人员,我们都需要对部署的策略模型进行监控,信贷领域可能还需要对客户的逾期表现进行监控。
阿黎逸阳
2021/07/23
2.6K0
【干货】用Python每天定时发送监控邮件
用python发送邮件
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。
用户6021899
2021/03/11
3230
用python发送邮件
使用python自动化发送邮件
smtplib模块是对SMTP协议的封装,用于发送邮件;email模块用于构建邮件内容,支持以下3种形式的邮件
生信修炼手册
2020/12/11
7720
使用python自动化发送邮件
python使用smtplib和MIMEText发送邮件
1.使用Python来操作邮箱:https://blog.csdn.net/qq_38059635/article/details/81569081
晓歌
2018/10/11
5.2K0
python使用smtplib和MIMEText发送邮件
【Python实用工具】巧妙利用Python实现发送带附件的邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
天道Vax的时间宝藏
2021/08/11
2.5K0
Python3实现自动发送邮件
首先了解SMTP(简单邮件传输协议),邮件传送代理程序使用SMTP协议来发送电邮到接收者的邮件服务器。SMTP协议只能用来发送邮件,不能用来接收邮件,而大多数的邮件发送服务器都是使用SMTP协议。SMTP协议的默认TCP端口号是25。
用户9925864
2022/07/27
3310
Python3实现自动发送邮件
python发送邮件(二)——smtplib模块和email模块
一、模块介绍 1、smtplib 模块(用于邮件的发送) ①理论解释 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]]) 通过这个语句,可以向SMTP服务器发送指令,执行相关操作(如:登陆、发送邮件)。所有的参数都是可选的。 host:smtp服务器主机名 port:smtp服务的端口,默认是25;端口号可以省略。 但是使用25号端口有一个问题,就是保密性不够好,数据都是明文传输,没有加密。 现在一般都推荐使用SSL,Secure So
Elsa_阿尼
2021/07/27
5.2K0
python发送邮件(二)——smtplib模块和email模块
Python实现自动发送邮件(详解)
这点很关键,别忘了去开启SMTP, 别忘了去开启SMTP,否则邮件是无法发送成功的 。然后你还需要点击下面生成授权码,这个授权码才是使用Python发送邮件时的真正密码。
全栈程序员站长
2022/11/17
1.1K0
Python实现自动发送邮件(详解)
python发送邮件
一.获取邮箱授权(以QQ邮箱为例子) 点击设置>账号 开启服务:POP3/SMTP服务 选择开启 然后获取授权码xxx 二.发送文本 import smtplib from email.mime.text import MIMEText subject = "标题" # 邮件的主题 content = '测试' #内容 sender = "568972484@qq.com" # 发件人 password = 'xxx' # 刚才我们在QQ邮箱里设置的授权密码 receiver = "5689
小小咸鱼YwY
2020/06/19
5360
干货 | 解放双手,用Python实现自动发送邮件
使用Python实现自动化邮件发送,可以让你摆脱繁琐的重复性业务,节省非常多的时间。 Python有两个内置库:smtplib和email,能够实现邮件功能,smtplib库负责发送邮件,email库负责构造邮件格式和内容。 邮件发送需要遵守SMTP协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。 1、先导入相关的库和方法
派大星的数据屋
2022/04/02
1.9K0
干货 | 解放双手,用Python实现自动发送邮件
Linux之python发送邮件
在服务器端开发时,会遇到通过邮件发送报警或结果的情形,本文对使用 python 发送附件的方法做一个总结,用到的库是 smtp 和 email 两个基础库
全栈程序员站长
2022/11/15
1.7K0
python笔记3-发送邮件(smtplib)
前言 本篇总结了QQ邮箱和163邮箱发送邮件,邮件包含html中文和附件,可以发给多个收件人,专治各种不行,总之看完这篇麻麻再也不用担心我的邮件收不到了。 以下代码兼容python2和python3,
上海-悠悠
2018/04/08
1.1K0
python笔记3-发送邮件(smtplib)
Python3 SMTP发送邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
织幻妖
2021/01/20
1K0
Python3 SMTP发送邮件
python之SMTP发送邮件
假设我们自己的邮件地址是myemail@163.com,对方的邮件地址是friend@qq.com,我们在网易提供的163邮箱界面编写邮件,然后发送给对方。那么,这封邮件是怎么从我方发送到对方的呢?
菲宇
2019/06/13
1.4K0
Python 发邮件
普通邮件 [root@localhost checksalt]# cat python_email.py  #!/usr/bin/python # -*- coding: utf-8 -*- import sys def smtp(title,file):     import smtplib     from email.mime.text import MIMEText     from email.header import Header           with open(file, 'r') 
py3study
2020/01/15
1.7K0
使用Python调用SMTP服务自动发送Email
假设我们想设计一个定时任务,比如每天定时的用python来测试服务是否在正常运行,但是又不希望每天登录到系统后台去查看服务状态。这里我们就可以采取python的smtp模块进行任务结果广播,申请一个公共邮箱,每次python执行完定时的测试任务后,调用smtp的接口将测试结果广播给需要接收的人的邮箱中。这就使得,我们可以在移动端就能按照我们的意愿实时监测系统的状态。
DechinPhy
2021/05/21
9010
发送电子邮件
在即时通信软件如此发达的今天,电子邮件仍然是互联网上使用最为广泛的应用之一,公司向应聘者发出录用通知、网站向用户发送一个激活账号的链接、银行向客户推广它们的理财产品等几乎都是通过电子邮件来完成的,而这些任务应该都是由程序自动完成的。
用户8442333
2021/05/20
2.4K0
【测试开发】python系列教程:smtplib库
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
雷子
2023/08/21
2810
【测试开发】python系列教程:smtplib库
相关推荐
Python发送邮件
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档