elk 分析日志详细部署

环境: centos7
elk版本: 7.12.1

ELK简介

ELK主要由ElasticSearch、Logstash和Kibana三个开源工具组成,还有其他专门由于收集数据的轻量型数据采集器Beats。

Elasticsearch :分布式搜索引擎。具有高可伸缩、高可靠、易管理等特点。可以用于全文检索、结构化检索和分析,并能将这三者结合起来。Elasticsearch 是用Java 基于 Lucene 开发,现在使用最广的开源搜索引擎之一,Wikipedia 、StackOverflow、Github 等都基于它来构建自己的搜索引擎。

在elasticsearch中,所有节点的数据是均等的。

Logstash :数据收集处理引擎。支持动态的从各种数据源搜集数据,并对数据进行过滤、分析、丰富、统一格式等操作,然后存储以供后续使用。

Kibana :可视化化平台。它能够搜索、展示存储在 Elasticsearch 中索引数据。使用它可以很方便的用图表、表格、地图展示和分析数据。

Filebeat:轻量级数据收集引擎。相对于Logstash所占用的系统资源来说,Filebeat 所占用的系统资源几乎是微乎及微。它是基于原先 Logstash-fowarder 的源码改造出来。换句话说:Filebeat就是新版的 Logstash-fowarder,也会是 ELK Stack 在 Agent 的第一选择。

版本说明

Elasticsearch、Logstash、Kibana、Filebeat安装的版本号必须全部一致,不然会出现kibana无法显示web页面。

ELK 常见的几种架构

1 Elasticsearch + Logstash + Kibana
这是一种最简单的架构。这种架构,通过logstash收集日志,Elasticsearch分析日志,然后在Kibana(web界面)中展示。这种架构虽然是官网介绍里的方式,但是往往在生产中很少使用。

2 Elasticsearch + Logstash + filebeat + Kibana
与上一种架构相比,这种架构增加了一个filebeat模块。filebeat是一个轻量的日志收集代理,用来部署在客户端,优势是消耗非常少的资源(较logstash), 所以生产中,往往会采取这种架构方式,但是这种架构有一个缺点,当logstash出现故障, 会造成日志的丢失。

3 Elasticsearch + Logstash + filebeat + redis(也可以是其他中间件,比如RabbitMQ) + Kibana
这种架构是上面那个架构的完善版,通过增加中间件,来避免数据的丢失。当Logstash出现故障,日志还是存在中间件中,当Logstash再次启动,则会读取中间件中积压的日志。

环境准备

1,配置jdk环境

tar xf jdk-8u291-linux-x64.tar.gz -C /usr/local/jdk/

1.1 添加环境变量

vim /etc/profile

#jdk
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

source /etc/profile

部署

1,安装包下载

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.12.1-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.12.1.tar.gz
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.12.1-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.12.1-linux-x86_64.tar.gz

2. 解压安装包

tar -xzf elasticsearch-7.12.1-linux-x86_64.tar.gz
tar -xzf logstash-7.12.1.tar.gz
tar -xzf kibana-7.12.1-linux-x86_64.tar.gz
tar -xzf filebeat-7.12.1-linux-x86_64.tar.gz

3. 创建用户

启动elasticsearch和kibana 要求以非root用户启动

groupadd elk
useradd elk -g elk
chown -R elk:elk elasticsearch
chown -R elk:elk kibana

4, elasticsearch部分

创建数据和日志文件夹,并修改权限

mkdir  -pv  /u01/elk/{data,logs}
chown -R elk:elk /u01/elk/

4.1 修改配置文件

vim elasticsearch/config/elasticsearch.yml
#修改如下
cluster.name: jenkins #集群名称

node.name: node-1  #节点名称

path.data: /data/elk/data
# 设置数据存放路径,建议修改这个路径到ES的安装文件夹外面,避免ES升级误删掉这个文件夹

path.logs: /data/elk/logs
# 设置日志存放路径,建议修改这个路径到ES的安装文件夹外面,避免ES升级误删掉这个文件夹

network.host: 0.0.0.0  #部署服务器ip单网卡如上配置即可,多网卡需要配置ip

http.port: 9200   #端口

discovery.seed_hosts: ["172.17.10.29"]  #发现节点配置

cluster.initial_master_nodes: ["node-1"]
vim /etc/security/limits.conf
 
----- 最后添加 -----
*       soft    nofile  65536
*       hard    nofile  131072
*       soft    nproc   4096
*       hard    nproc   4096
vim /etc/sysctl.conf
 
----- 最后添加 -----
 
vm.max_map_count=262144
————————————————
sysctl -p

4.2 启动

su - elk
cd elasticsearch
nohup bin/elasticsearch &

5. root用户下安装logstash

cd /u01/elk/logstash
cp config/logstash-sample.conf ./
nohup bin/logstash -f logstash-sample.conf &

6. 安装kibana

su - elk
cd /u01/elk/kibana

vim config/kibana.yml  #修改host,这样可以在其他机器上的浏览器,用ip+端口去访问kibana

server.port: 5601  #默认监听端口

server.host: "0.0.0.0"  #部署服务器ip单网卡如上配置即可,多网卡需要配置ip

server.name: "jenkins"  #配置服务器名称

elasticsearch.hosts: ["http://172.17.10.29:9200"]  #配置es的集群节点地址

kibana.index: ".kibana"  #创建一个kibana索引

6.1 启动,kibana也不能用root用户启动

nohup bin/kibana &

在浏览器里,输入yourip:5601 访问成功即代表启动成功

7. filebeat  #root

cd /u01/elk/filebeat
vim filebeat.yml
filebeat.inputs:

# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.

- type: log

  # Change to true to enable this input configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /usr/local/nginx/logs/jenkins/*.log

# ============================== Filebeat modules ==============================

filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: true

  # Period on which files under path should be checked for changes
  reload.period: 10s

# ------------------------------ Logstash Output -------------------------------
output.logstash:
  # The Logstash hosts
  hosts: ["172.17.10.29:5044"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"


#末尾添加
i18n.locale: "zh-CN"  #汉化
filebeat 配置主要分为两部分
    inputs: 从哪里的数据手机数据
    output:  数据转发到哪里
默认filebeat会将日志数据放入到名称为: filebeat-%filebeat版本号%-yyyy.MM.dd 的索引中

7.1 配置filebeat日志收集实现方式

Filebeat 收集日志有两种实现方式:

方式一:通过启用模块,修改模块中的配置文件方式实现
比如想监控nginx模块,开启nginx模块,然后在模块的模板配置nginx.yml 文件中配置日志路径就行了

建议使用这种方式,这种直接提供了日志配置模板,简单改下路径即可。

方式二: 通过自定义监控文件路径事项

7.1.1 启用模块和配置数据收集日志

查看所有模块

./filebeat modules list

启用模块 示例nginx,system模块配置

[root@jenkins /u01/elk/filebeat]# ./filebeat modules enable system nginx
Enabled system
Enabled nginx

#执行上面的命令后就会发现./modules.d 文件夹下的nginx.yml.disabled 文件重命名成了ningx.yml

禁用

[root@jenkins /u01/elk/filebeat]# ./filebeat modules disable system nginx
Disabled system
Disabled nginx

#执行上面的命令后就会发现./modules.d 文件夹下的nginx.yml.disabled 文件重命名成了ningx.yml

采集nginx的/usr/local/nginx/logs/hydrus-crm/access.log 日志和/usr/local/nginx/logs/hydrus-crm/error.log日志

编辑modules.d目录下的nginx.yml文件

# Module: nginx
# Docs: https://www.elastic.co/guide/en/beats/filebeat/7.x/filebeat-module-nginx.html

- module: nginx
  # Access logs
  access:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["/usr/local/nginx/logs/hydrus-crm/access.log"]

  # Error logs
  error:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["/usr/local/nginx/logs/hydrus-crm/error.log"]

  # Ingress-nginx controller logs. This is disabled by default. It could be used in Kubernetes environments to parse ingress-nginx logs
  ingress_controller:
    enabled: false

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    #var.paths:

报错:

如果nginx.yml 配置文件出错就会报错如下: range can't iterate over /usr/local/nginx/logs/access.log

7.1.2 手动配置filebeat 监控日志文件

要手动配置Filebeat(而不是使用 模块),请在的filebeat.inputs部分中指定输入列表 filebeat.yml

输入指定Filebeat如何查找和处理输入数据
该列表是一个YAML数组,因此每个输入都以破折号(-)开头。您可以指定多个输入,并且可以多次指定相同的输入类型。
filebeat.inputs:

- type: log

  # Change to true to enable this input configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /usr/local/nginx/logs/jenkins/*.log
    - /usr/local/nginx/logs/hydrus-crm/*.log
本示例中的输入/var/log/*.log将收获路径中的所有文件,这意味着Filebeat将收集/var/log/ 目录中以.log结尾的所有文件
要从预定义级别的子目录中提取所有文件,请使用以下模式: /var/log/*/*.log。这.log将从的子文件夹中提取所有文件 /var/log。
它不会从/var/log文件夹本身获取日志文件。当前,不可能以递归方式获取目录所有子目录中的所有文件。

7.2 启动

首次启动

./filebeat setup -e -c ./filebeat.yml 

以后启动

nohup  ./filebeat -e -c filebeat.yml &

7.3 停止

ps -aux |grep filebeat

kill -9 pid

7.4 调整logstash 配置

input {
  beats {
    port => 5044
  }

}

output {
  elasticsearch {
    hosts => ["172.17.10.29:9200"]
    index => "logsfiled-%{+YYYY.MM.dd}"
  }
  #stdout { codec => rubydebug }
}

8. 查看kibana 日志结果

地址 http://ip:5601

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值