Elk监控nginx
一、服务介绍
ELK分别表示:Elasticsearch , Logstash, Kibana FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash
Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。
Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。
二、环境搭建
2.1搭建单机es
参考上篇文章集群搭建 https://mp.weixin.qq.com/s/UHtp28Kp2_EF_fDGO3JyAw
配置文件有不同之处 修改 elasticsearch.yml
cluster.name: es
node.name: node-1
path.data: /data/elasticsearch-7.14.2/data
path.logs: /data/elasticsearch-7.14.2/log
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["192.168.8.10"]
cluster.initial_master_nodes: ["node-1"]
http.cors.enabled: true
http.cors.allow-origin: "*"
启动 搭建完成
2.2下载logstash-7.6.2.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.6.2.tar.gz
配置config目录下的logstash.conf
# 数据输入部分
input {
读取nginx访问日志
file {
:监听文件的路径,绝对路径
为Nginx的access.log路径
格式:json
codec => "json"
监听文件的起始位置,beginning:从文件的头开始读取
start_position => "beginning"
:自定义类型
type => "nginx-access-log"
}
读取nginx异常日志
file {
为Nginx的error.log路径
格式:plain,输入的是字符串,输出把全部内容放到message字段
codec => "plain"
start_position => "beginning"
type => "nginx-error-log"
}
方式
beats {
port => 5044
type => "beats_log"
}
自定义端口,一个项目可对应一个自定义tcp端口接收数据
# tcp {
# mode => "server"
# host => "192.168.X.X" #IP地址
# port => 21022
# codec => json
# type => "application-log" #application一般为项目名称
# }
}
# 数据处理部分
filter{
if[type] == "application-log"{
mutate {
rename => {"host" => "hostname"}
}
}
}
# 数据输出部分
output {
监控信息
if [type] == "beats_log"{
elasticsearch {
hosts => ["192.168.8.10:9200"]
manage_template => false
index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
user => "elastic"
password => "elastic"
}
}
访问日志
if[type] == "nginx-access-log" {
elasticsearch {
hosts => ["192.168.8.10:9200"]
index => "nginx-access-log-%{+YYYY.MM.dd}"
的用户名、密码
user => "elastic"
password => "elastic"
}
}
异常日志
if[type] == "nginx-error-log"{
elasticsearch{
hosts => ["192.168.8.10:9200"]
index => "nginx-error-log-%{+YYYY.MM.dd}"
user => "elastic"
password => "elastic"
}
}
应用系统日志
if[type] == "application-log"{
elasticsearch {
hosts => ["192.168.8.10:9200"]
index => "application-log-%{+YYYY.MM.dd}"
user => "elastic"
password => "elastic"
}
}
}
#根据实际环境配置 此测试环境没有应用 所以注释了应用接口
配置完成之后 进入解压目录后台启动
nohup ./bin/logstash -f config/logstash.conf &
ps -ef | grep logstash # 查看是否启动成功
2.3配置kibana
下载解压
vim config/kibana.yml
server.port: 5601 #kibana端口
server.host: "0.0.0.0" #所有主机都能访问,或者也可以指定一个ip
elasticsearch.hosts: "http://es服务公网IP:9200" #配置es的访问地址
kibana.index: ".kibana"
用es用户给kibana目录授权
然后 后台启动 nohup ./kibana &
2.4 配置filebeat
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.6.1-linux-x86_64.tar.gz
配置 filebeat.yml
output.logstash:
# The Logstash hosts
修改为实际IP
修改 (将前面注释取消掉)
### Multiline options
multiline.pattern: ^\[
multiline.negate: true
multiline.match: after
注释掉以下
#output.elasticsearch:
# Array of hosts to connect to.
#hosts: ["localhost:9200"]
然后启动
nohup ./filebeat &
2.5登录kibana 访问地址 :5601
点击索引管理之后会看到现有索引,然后新建索引模式
然后点击discover
查看nginx访问日志 自己访问一下 有最新日志 代表抓取成功