码迷,mamicode.com
首页 > 其他好文 > 详细

filebeat->redis->logstash->elasticsearch->kibana

时间:2019-08-28 18:38:31      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:localhost   off   load   cache   logstash   cti   否则   etc   区分   

整体流程

  1. filebeat收集openresty应用日志传输到Redis集群中
  2. Logstash从Redis集群中拉取数据,并传输到Elasticsearch集群
  3. 使用Kibana可视化索引
  4. 使用Elasticsearch-head管理lasticsearch集群

注:Logstash不支持集群模式

环境

均为CentOS 7.4 x64系统

openresty 192.168.0.10 1.15.8版本
filebeat 192.168.0.10 7.3.0版本
Redis集群 192.168.0.11 6381-6386端口(暂采用伪集群的方式) 5.0.5版本
Logstash 192.168.0.12 7.3.0版本
Elasticsearch集群 192.168.0.13-15 7.3.0版本
Kibana 192.168.0.16 7.3.0版本
Elasticsearch-head 192.168.0.17

软件下载地址
filebeat|Logstash|Elasticsearch|Kibana:https://www.elastic.co/cn/downloads/past-releases#elasticsearch
Elasticsearch-head:https://github.com/mobz/elasticsearch-head
Redis:https://redis.io/download

Redis集群开启外部访问,设置访问密码,防火墙放行端口

配置

filebeat配置

安装

软件采用通过下载rpm包的方式进行安装

rpm -ivh filebeat-7.3.0-x86_64.rpm
systemctl daemon-reload
systemctl enable filebeat.service
systemctl start filebeat.service

修改filebeat.yml配置文件

openresty日志文件路径:
access:/usr/local/openresty/nginx/logs/host.access.log
error:/usr/local/openresty/nginx/logs/error.log

文件路径:/etc/filebeat/filebeat.yml

注销其他的output输出

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /usr/local/openresty/nginx/logs/host.access.log
  fields:
    log_source: access

- type: log
  enabled: true
  paths:
    - /usr/local/openresty/nginx/logs/error.log
  fields:
    log_source: error

output.redis:
  hosts: ["192.168.0.11:6381","192.168.0.11:6382","192.168.0.11:6383","192.168.0.11:6384","192.168.0.11:6385","192.168.0.11:6386"]
  password: foobar2000
  db: 0
  key: "log_list"

还有一些其他的优化参数,后期需要再添加

测试

重启filebeat后,使用浏览器访问网址,确保先生成的有access和error日志,然后在Redis集群的第0库查看
会发现有一个key是log_list,值是list的数据,里面一行一行的数据就是收集过来的日志,这些日志加的有一些元数据
具体可以查看本博客的另一篇文章《使用filebeat收集日志传输到redis的各种效果展示》,这里面有详细的讲解。

Logstash配置

安装

  1. 配置java环境
cd /usr/local/src
tar -zxv -f openjdk-12_linux-x64_bin.tar.gz  -C /usr/local/
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/jdk-12
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

source /etc/profile.d/java.sh
  1. 安装logstash

软件采用通过下载rpm包的方式进行安装

rpm -ivh logstash-7.3.0.rpm
systemctl daemon-reload
systemctl enable logstash.service
systemctl start logstash.service

修改启动文件,确保能找到java环境变量
文件路径:/usr/share/logstash/bin/logstash.lib.sh
在行首加入如下信息

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

msg.conf配置文件修改

logstash默认的配置文件时在/etc/logstash/conf.d/目录下创建以conf结尾的文件,本例中创建的是msg.conf
文件路径:/etc/logstash/conf.d/msg.conf
该文件内容如下

input {
  
  redis {
    # 若报错该redis不能访问,则不使用Redis集群,只使用一台redis,需要修改filebeat.yml文件
    host => "192.168.0.11"
    port => 6381
    password => foobar2000
    data_type => "list"
    key => "log_list" 
    db => 0 
  }
  
}

output {
  # 根据filebeat.yml中设置的不同日志来源进行区分,从而创建不同的索引
  if [fields][log_source] == 'access' {
    elasticsearch {
      hosts => ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"]
      index => "filebeat-access-%{+YYYY.MM.dd}"
      id => "access_id"
    }
  } 

  if [fields][log_source] == 'error' {
    elasticsearch {
      hosts => ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"]
      index => "filebeat-error-%{+YYYY.MM.dd}"
      id => "error_id"
    }
  } 
}

测试

查看redis的key值对应的list数据还有没,没有的话说明已经把日志传输给logstash了。

Elasticsearch集群配置

安装

详细参考本博客另一篇文章《ElasticSearch集群》

elasticsearch.yml配置文件修改

文件路径:/etc/elasticsearch/elasticsearch.yml
详细参考本博客另一篇文章《ElasticSearch集群》

测试

详细参考本博客另一篇文章《ElasticSearch集群》

Elasticsearch-head配置

yum -y install epel-release
yum -y install git npm
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start

启动后会占用当前窗口,改用脚本启动方式
在当前目录下创建一个start.sh文件

#!/bin/bash

nohup npm run start > /tmp/head.log 2>&1 & 

默认访问地址是http://localhost:9100

这时可以通过使用nginx代理的方式进行访问
可以配置一个域名,同时设置一个nginx网站访问密码,具体参考本博客的另一篇文章《nginx配置访问密码,输入用户名和密码才能访问》

location /head {
    proxy_pass  http://127.0.0.1:9100;
    #Proxy Settings
    proxy_redirect     off;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_max_temp_file_size 0;
    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;
    proxy_buffer_size          4k;
    proxy_buffers              4 32k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
}

修改elasticsearch.yml配置文件,开启跨域访问
在每个集群节点的elasticsearch.yml配置文件末尾加上如下两行数据

http.cors.enabled: true
http.cors.allow-origin: "*"

Kibana配置

安装

软件采用通过下载rpm包的方式进行安装

rpm -Uvh kibana-7.3.0-x86_64.rpm
systemctl daemon-reload
systemctl enable kibana.service
systemctl start kibana.service

kibana.yml配置文件修改

文件路径:/etc/kibana/kibana.yml
这个也采用nginx代理的方式进行访问,同时设置nginx的web访问密码
具体参考本博客的另一篇文章《nginx配置访问密码,输入用户名和密码才能访问》

后期可以结合X-pack插件进行用户角色和权限管理等功能

server.port: 5601
server.host: "127.0.0.1"
server.basePath: "/kibana"  # 跟nginx那个路径对应,否则无法加载静态资源
elasticsearch.hosts: ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"]
i18n.locale: "zh-CN"

nginx配置文件内容

location /kibana/ {
    proxy_pass http://127.0.0.1:5601;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    rewrite ^/kibana/(.*)$ /$1 break;
}

使用http://ip/kibana进行访问,查看索引信息等

或者采用另一种配置
nginx配置文件

server {
    listen 80;  
    server_name xxx.xxx.com;
    root   /usr/share/nginx/html;
    expires -1;
   
    client_max_body_size    10m;  
    location / {
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-Port $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass  http://172.17.107.187:5600;
    }
}

kibana.yml配置文件

server.port: 5601
server.host: "127.0.0.1"
elasticsearch.hosts: ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"]
i18n.locale: "zh-CN"

访问的话直接使用域名的方式,不加后缀kibana

注意这两种不同方式的配置

测试

打开浏览器进行访问,查看索引,日志文件等

后期扩展

  1. 各软件之间的数据传输采用加密的方式进行
  2. 结合X-pack给kibana的访问设置用户角色和权限控制等
  3. filebeat模块的功能还有待研究
  4. 其他beats产品有待研究
  5. kibana的web界面使用还有待研究
  6. 读取MySQL数据库数据到Elasticsearch功能还有待研究
  7. 在Logstash中过滤数据还有待研究
  8. 其他未了解的也都待研究

filebeat->redis->logstash->elasticsearch->kibana

标签:localhost   off   load   cache   logstash   cti   否则   etc   区分   

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/11425597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!