小言_互联网的博客

傻瓜式 ELK 日志分析平台搭建,你学会了吗?

190人阅读  评论(0)

1. ELK 简介

ELK 实际上是三个工具的集合,Elasticsearch + Logstash + Kibana,这三个工具组合形 成了一套 实用、易用的监控架构,很多公司利用它来搭建可视化的海量日志分析平台。

ElasticSearch:ElasticSearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引 擎,基于 RESTful web 接口。Elasticsearch 是用 Java 开发的,并作为 Apache 许可条款下的开放源码 发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快 速,安装使用方便。

Logstash:Logstash 是一个用于管理日志和事件的工具,你可以用它去收集日志、转换日志、解析日志并将 他们作为数据提供给其它模块调用,例如搜索、存储等。 Logstash是一个完全开源的工具,它可以对你的日志进行收集、过滤、分析,支持大量的数据获取方法,并将其存储供以后使用(如搜索)。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。Logstash事件处理有三个阶段:inputs → filters → outputs。是一个接收,处理,转发日志的工具

Kibana:Kibana是一个基于浏览器页面的Elasticsearch前端展示工具,也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。

Filebeat:Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件,并且转发这些信息到赋予他的输出位置(常见如elasticsearch、kafka或logstarsh)中存放。Filebeat等同于一个轻量级的logstash,当你需要收集信息的机器配置或日志资源数量不是特别多时,最佳实践是使用filebeat来代替logstash收集日志,filebeat十分小巧且稳定。下图是filebeat的工作流程:当你开启filebeat程序的时候,它会启动一个或多个探测器(prospectors)去检测你指定的日志目录或文件,对于探测器找出的每一个日志文件,filebeat启动收割进程(harvester),每一个收割进程读取一个日志文件的新内容,并发送这些新的日志数据到处理程序(spooler),处理程序会集合这些事件,最后filebeat会发送集合的数据到你指定的地点。

为什么要使用 ELK?

目前系统大都采用分布式微服务架构,服务集群部署,这样就会产生很多日志文件,如 果每次查问题都是登录到各个服务器看日志文件,是非常困难的。因此需要有统一收集日 志的工具,来帮我们收集查看日志。ELK 可以让快速准确的定位问题。提高效率。

架构图:

2.软件下载

通过如下地址进行下载,四个软件的版本一致。

https://elasticsearch.cn/download/

上传到服务器上后解压。

3. Elasticsearch 安装配置

官网:

https://www.elastic.co/cn/downloads/past-releases#elasticsearch

解压后进入 Elasticsearch 文件目录。配置如下:

3.1 修改elasticsearch.yml

路径:/elasticsearch-7.5.0/config

修改内容如下:

node.name: node-1 
network.host: 127.0.0.1
http.port: 9200 
cluster.initial_master_nodes: ["node-1"] 

3.2 按需修改 jvm.options内存设置。

路径/elasticsearch-7.5.0/config

-Xms1g 
-Xmx1g

3.3 创建 es 用户

不能使用root 用户。

useradd mtappelk
# 修改密码
passwd mtappelk

改变es目录拥有者账号

chown -R mtappelk elasticsearch/

3.4 修改/etc/sysctl.conf

末尾添加

vm.max_map_count=655360

执行生效

sysctl -p

3.5 修改/etc/security/limits.conf

末尾添加:

*               soft    nofile            65536
*               hard    nofile            65536
*               soft    nproc             4096
*               hard    nproc             4096

3.6 切换es 用户启动

su mtappelk

./bin/elasticsearch -d

3.7 测试

判断是否启动成功:

curl -X GET "127.0.0.1:9200"

或者浏览器访问:

http://192.168.1.123:9200/

4. kibana 安装配置

我们解压 Kibana 后,设置用户和 ES 的用户保持一致。

4.1 修改用户权限和访问权限

这里 logstash 也一起改了

chown -R mtappelk kibana-7.5.0-linux-x86_64
chown -R mtappelk logstash-7.5.0

修改访问权限

chmod -R 777 kibana-7.5.0-linux-x86_64/

4.2 修改配置文件

路径:/kibana-7.5.0-linux-x86_64/config

server.port: 5601 
server.host: "0.0.0.0" 
elasticsearch.hosts: ["http://192.168.168.21:9200"]

4.3 切换用户启动

su mtappelk
nohup sh./bin/kibana &

6、访问

http://192.168.1.123:5601/

5. logstash 安装配置

5.1 新增 default.conf 配置

解压后,进入config 目录下创建default.conf 文件,内容如下:

# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.

input {
  beats {
    port => 5044
  }
}

output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
    #user => "elastic"
    #password => "changeme"
  }
}

5.2 启动 logstash

nohup sh bin/logstash -f config/default.conf &

启动成功如下:

6. filebeat 安装配置

6.1 修改filebeat.yml

修改配置如下:

- 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/*.log
    #- c:\programdata\elasticsearch\logs\*
    
- type: log
  enabled: true
  paths:
    - /home/mtex_plat/logs/mtapp*/all.log
    
#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:
  # Array of hosts to connect to.
#  hosts: ["localhost:9200"]

  # Optional protocol and basic auth credentials.
  #protocol: "https"
  #username: "elastic"
  #password: "changeme"

#----------------------------- Logstash output --------------------------------
output.logstash:
  # The Logstash hosts
  hosts: ["localhost: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"

配置将日志发送到 logstash

6.2 启动 filebeat

nohup  ./filebeat -e -c filebeat.yml -d "publish" &

7. 界面使用

7.1 创建索引

8. 总结

ELK 现在在很多公司都会使用了,作为日志收集平台,开发人员可能更多的在于在 ELK 的使用上,ELK 的部署更多的运维人员看了, 但是如果作为开发人员也能对部署有所了解的话,会不会觉得自己更厉害了点呢哈哈。


转载:https://blog.csdn.net/qq_27790011/article/details/116669600
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场