25

详解EFK搭建过程及ES的生命周期管理

 4 years ago
source link: https://www.tuicool.com/articles/QRFfEjj
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

概述

今天主要介绍下EFK搭建过程及ES的生命周期管理,平台采用EFK(ElasticSearch-6.6.1 + FileBeat-6.6.2 + Kibana-6.6.1)架构。建议三个组件主次版本保持一致。

uUJR3aF.jpg!web

EFK概念

EFK采用集中式的日志管理架构

  • elasticsearch:一个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
  • kibana:可以为Logstash 、Beats和ElasticSearch提供友好的日志分析Web 界面,可以帮助汇总、分析和搜索重要数据日志。
  • filebeat:轻量级日志采集器。需要在每个应用服务器配置filebeat,来采集日志,并输出到elasticsearch
U7vq6vr.jpg!web

一、ElasticSearch

#rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch 
#vi/etc/yum.repos.d/elasticsearch.repo 
===================================================== 
[elasticsearch-6.x] 
name=Elasticsearch repository for 6.x packages 
baseurl=https://artifacts.elastic.co/packages/6.x/yum 
gpgcheck=1 
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch 
enabled=1 
autorefresh=1 
type=rpm-md 
===================================================== 
# yum install elasticsearch 
# vim /etc/elasticsearch/elasticsearch.yml 
==================================================== 
network.host: 0.0.0.0 
==================================================== 
# service elasticsearch restart 

二、Kibana

1、部署

# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch 
# vim /etc/yum.repos.d/kibana.repo 
===================================================== 
snippet.bash 
[kibana-6.x] 
name=Elasticsearch repository for 6.x packages 
baseurl=https://artifacts.elastic.co/packages/6.x/yum 
gpgcheck=1 
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch 
enabled=1 
autorefresh=1 
type=rpm-md 
===================================================== 
# yum install kibana 
# vim /etc/kibana/kibana.yml 
===================================================== 
server.host: "kibana服务器ip" 
elasticsearch.hosts: ["http://ES服务器IP:9200"] 
#如果通过反向代理访问,则还需要添加如下配置。路径具体值视情况而定 
server.basePath: "/kibana" 

2、下载汉化包并复制到指定目录

wget https://codeload.github.com/anbai-inc/Kibana_Hanization/zip/master 
unzip master 
cp -r Kibana_Hanization-master/translations/ /usr/share/kibana/src/legacy/core_plugins/kibana/ 
​ 
#修改语言配置 
#vim /etc/kibana/kibana.yml 
====================================== 
i18n.locale: "zh_CN" 
====================================== 

3、重启服务

service kibana restart 

三、FileBeat

Filebeat隶属于Beats家族。目前Beats家族包含六种工具:

Packetbeat(搜集网络流量数据)

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

Filebeat(搜集文件数据)

Winlogbeat(搜集 Windows 事件日志数据)

Auditbeat( 轻量型审计日志采集器)

Heartbeat(轻量级服务器健康采集器)

1、部署

# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch 
# vim /etc/yum.repos.d/filebeat.repo 
================================================== 
snippet.bash 
[filebeat-6.x] 
name=Elasticsearch repository for 6.x packages 
baseurl=https://artifacts.elastic.co/packages/6.x/yum 
gpgcheck=1 
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch 
enabled=1 
autorefresh=1 
type=rpm-md 
================================================== 
# yum install filebeat 

2、配置

/etc/filebeat/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: 
 - d:/ams_logs/*.log 
 encoding: gbk 
  
# 输出配置 
output.elasticsearch: 
 # Array of hosts to connect to. 
 hosts: ["ES服务器IP:9200"] 

3、重启服务

service filebeat restart

效果图如下:

q6Znu2J.jpg!web

四、ES生命周期管理

对于日志数据,由于单个索引的存储量的瓶颈,ES一般推荐使用时间作为后缀为同一份日志数据创建多个索引,而用户则通过一个定时器来定时删除过期的索引。ES在6.6之后,在x-pack中推出了索引生命周期管理相关的API来简化与增强类似日志数据索引的管理。该方案基于时间将索引数据分为四个阶段:Hot、Warm、Cold、Delete,对于这四种并给不同的数据阶段,ES也给出了不同的数据处理方式,最终实现日志的生命周期管理

1、策略配置

管理→Index Lifecycle Policies,Create Policy,

Fn6Nzmn.jpg!web

2、日志生成

filebeat提供了两种日志生成方式。一般情况下,建议使用默认生成策略

2.1、默认生成策略

打开filebeat配置文件,添加如下内容。使用本方案所对应的策略配置名称,必须为 beats-default-policy

raMBnum.jpg!web
output.elasticsearch: 
 hosts: ["ES服务器IP:9200"] 
 ilm.enabled: true 
 ilm.rollover_alias: "fsl.ams" 
 ilm.pattern: "{now/d}-000001"  

2.2、高级生成策略

打开filebeat配置文件,添加如下内容。在6.6.1版本下,使用本方案前,请事先在es上创建合适的索引模板。否则其直接生成的索引将不会存在别名(疑似bug),最终造成无法使用生命周期策略。

output.elasticesarch: 
 hosts: ["ES服务器IP:9200"] 
 index: fsl.ams-%{+yyyy.MM.dd} 
setup.template.name: "fsl.ams" 
setup.template.pattern: "fsl.ams-*" 
setup.template.settings.index.lifecycle.rollover_alias: "fsl.ams" 
setup.template.settings.index.lifecycle.name: "beats-default-policy" 
aeqa2aR.jpg!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK