• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

有手就行filebeat+es+kibana收集Nginx日志

武飞扬头像
软件东哥
帮助1

做这篇文章我已经自己搭建了3次,前2次都会遇到不一样的问题,第三次基本遇到问题就很快解决了。

这篇文章,介绍从0开始搭建Nginx,filebeat。es和kibana本次使用的是现有环境,不做介绍。

学新通

环境准备

  • VMware虚拟机
  • filebeat安装包

步骤1:虚拟机准备

确认虚拟机可以访问和远程

我是克隆的虚拟机,虚拟机搭建不做介绍

学新通

克隆虚拟机可能存在ip未能分配:我是通过重启所有机器解决的。

学新通

重启后

学新通

 网络测试

学新通

步骤2:安装Nginx

目标:局域网机器可访问

1、安装wget、tar及nginx必备库

  1.  
    yum -y install wget
  2.  
    yum install -y tar
  3.  
     
  4.  
    yum -y install make zlib zlib-devel gcc gcc-c libtool openssl openssl-devel
  5.  
    yum install -y pcre pcre-devel

2、 下载nginx并安装

  1.  
    wget http://nginx.org/download/nginx-1.21.4.tar.gztar -xzvf nginx-1.21.4.tar.gz
  2.  
     
  3.  
    # ./configure是检查文件的命令,检查是否正确
  4.  
    ./configure
  5.  
     
  6.  
    #执行安装操作
  7.  
    make && make install

3、校验启动

  1.  
    #校验文件
  2.  
    /usr/local/nginx/sbin/nginx -t
  3.  
     
  4.  
    #启动
  5.  
    /usr/local/nginx/sbin/nginx

 4、查看防火墙

  1.  
    #查看开放了哪些端口
  2.  
    firewall-cmd --list-all
  3.  
    #查看开放的端口
  4.  
    firewall-cmd --list-ports
  5.  
    #如果没有显示80,则需要开放80端口
  6.  
    firewall-cmd --add-port=80/tcp --permanent #开放端口
  7.  
    firewall-cmd --remove-port=80/tcp --permanent
  8.  
    firewall-cmd --reload #重新加载

5、测试外网

学新通

6、查看日志

Nginx日志在logs下面。

/usr/local/nginx/logs

学新通

步骤2:安装filebeat

目标:连接es能够读取日志

1、下载

https://www.elastic.co/cn/downloads/past-releases/filebeat-8-0-0

2、上传、解压

比如上传到:/usr/src/filebeat

通过一下命令解压

tar -zxvf 压缩文件名

3、配置Nginx.yml 和 filebeat.yml

Nginx.yml在 cd modules.d/ 文件夹里面

当然里面是是Nginx.yml.disabled

这是我启用后的截图:

学新通

【配置】Nginx.yml

这里你可以通过修改,然后在启用命令。

启动命令:

  1.  
    启动命令:./filebeat modules enable nginx
  2.  
    禁用命令:./filebeat modules disable nginx

也可以直接删除文件,自己本地上传。

  1.  
    # Module: nginx
  2.  
    # Docs: https://www.elastic.co/guide/en/beats/filebeat/master/filebeat-module-nginx.html
  3.  
     
  4.  
    - module: nginx
  5.  
    # Access logs
  6.  
    access:
  7.  
    enabled: true
  8.  
     
  9.  
    # Set custom paths for the log files. If left empty,
  10.  
    # Filebeat will choose the paths depending on your OS.
  11.  
    var.paths: ["/usr/local/nginx/logs/access.log*"]
  12.  
     
  13.  
    # Error logs
  14.  
    error:
  15.  
    enabled: true
  16.  
     
  17.  
    # Set custom paths for the log files. If left empty,
  18.  
    # Filebeat will choose the paths depending on your OS.
  19.  
    #var.paths:
  20.  
     
  21.  
    # Ingress-nginx controller logs. This is disabled by default. It could be used in Kubernetes environments to parse ingress-nginx logs
  22.  
    ingress_controller:
  23.  
    enabled: false
  24.  
     
  25.  
    # Set custom paths for the log files. If left empty,
  26.  
    # Filebeat will choose the paths depending on your OS.
  27.  
    var.paths: ["/usr/local/nginx/logs/error.log*"]
学新通

 配置filebeat.yml

  1.  
     
  2.  
    filebeat.inputs:
  3.  
     
  4.  
    # Each - is an input. Most options can be set at the input level, so
  5.  
    # you can use different inputs for various configurations.
  6.  
    # Below are the input specific configurations.
  7.  
     
  8.  
    # filestream is an input for collecting log messages from files.
  9.  
    - type: log
  10.  
     
  11.  
    # Change to true to enable this input configuration.
  12.  
    enabled: true
  13.  
     
  14.  
    # Paths that should be crawled and fetched. Glob based paths.
  15.  
    paths:
  16.  
    - /usr/local/nginx/logs/*.log
  17.  
    fields:
  18.  
    source: demo
  19.  
     
  20.  
    # ============================== Filebeat modules ==============================
  21.  
     
  22.  
    filebeat.config.modules:
  23.  
    # Glob pattern for configuration loading
  24.  
    path: ${path.config}/modules.d/*.yml
  25.  
     
  26.  
    # Set to true to enable config reloading
  27.  
    reload.enabled: false
  28.  
     
  29.  
    # Period on which files under path should be checked for changes
  30.  
    #reload.period: 10s
  31.  
     
  32.  
    # ======================= Elasticsearch template setting =======================
  33.  
     
  34.  
    setup.template.settings:
  35.  
    index.number_of_shards: 1
  36.  
    #index.codec: best_compression
  37.  
    #_source.enabled: false
  38.  
     
  39.  
    output.elasticsearch:
  40.  
    # Array of hosts to connect to.
  41.  
    hosts: ["192.168.1.666:9200"]
  42.  
     
  43.  
    # Protocol - either `http` (default) or `https`.
  44.  
    #protocol: "https"
  45.  
     
  46.  
    # Authentication credentials - either API key or username/password.
  47.  
    #api_key: "id:api_key"
  48.  
    username: "elastic"
  49.  
    password: "密码"
  50.  
    indices:
  51.  
    - index: "demo-%{ yyyy.MM.dd}"
  52.  
    when.equals:
  53.  
    fields.source: "demo"
  54.  
    processors:
  55.  
    - add_host_metadata:
  56.  
    when.not.contains.tags: forwarded
  57.  
    - add_cloud_metadata: ~
  58.  
    - add_docker_metadata: ~
  59.  
    - add_kubernetes_metadata: ~
学新通

 启动后,如下图;会展示进程id

学新通

步骤3:ES查询

目标:能够查询到数据

1、打开Kibana-DevTools

学新通

输入如下命令,index自动带出

  1.  
    GET demo-2022.12.04/_search
  2.  
    {
  3.  
    "query": {
  4.  
    "match_all": {}
  5.  
    }
  6.  
    }

学新通

查询结果

学新通

来到界面查询发现你没有,根据关键字【index pattern】在 Management 找到配置

学新通

学新通

学新通

右上角新建 Creat index pattern

学新通

选择时间戳字段

学新通

完成创建!右上角有删除按钮,后面不需要可以删除。

学新通

这样在界面就可以搜索啦

学新通

最终效果1

学新通

最终效果2

学新通

Filebeat后台启动

增加文件到如下目录

/usr/lib/systemd/system/filebeat.service

常用命令:

  1.  
    systemctl restart filebeat
  2.  
     
  3.  
    systemctl stop filebeat
  4.  
     
  5.  
    systemctl status filebeat
  6.  
     
  7.  
    systemctl start filebeat
  8.  
     
  9.  
    ps -ef | grep file

filebeat.service

其实重要的命令只有一句:ExecStart ***

还有一个属性:Restart=always;根据字面意思理解,重启属性:总是;也就是你必须通过上述命令 systemctl stop filebeat 去关闭。不能kill。kill掉了还会重启~~~~

  1.  
    [Unit]
  2.  
    Description=Filebeat sends log files to Logstash or directly to Elasticsearch.
  3.  
    Documentation=https://www.elastic.co/products/beats/filebeat
  4.  
    Wants=network-online.target
  5.  
    After=network-online.target
  6.  
     
  7.  
    [Service]
  8.  
     
  9.  
    Environment="BEAT_LOG_OPTS="
  10.  
    Environment="BEAT_CONFIG_OPTS=-c /usr/src/filebeat/filebeat-8.0.0-linux-x86_64/filebeat.yml"
  11.  
    Environment="BEAT_PATH_OPTS=--path.home /usr/src/filebeat/filebeat-8.0.0-linux-x86_64/filebeat --path.config /usr/src/filebeat/filebeat-8.0.0-linux-x86_64 --path.data /usr/src/filebeat/filebeat-8.0.0-linux-x86_64/data --path.logs /usr/src/filebeat/filebeat-8.0.0-linux-x86_64/logs"
  12.  
    ExecStart=/usr/src/filebeat/filebeat-8.0.0-linux-x86_64/filebeat --environment systemd $BEAT_LOG_OPTS $BEAT_CONFIG_OPTS $BEAT_PATH_OPTS
  13.  
    Restart=always
  14.  
     
  15.  
    [Install]
  16.  
    WantedBy=multi-user.target
学新通

常见问题:

虚拟机Linux打开是黑屏

答:Vmware安装Linux打开一直黑屏的四个解决方法-爱码网 (likecs.com)

LInux未能获取到内网ip。

答:重启所有的虚拟机。

ping 127.0.0.1 通,但是ping www.百度.com 不通

问题原因:没有启用联网功能,启用联网功能

vi /etc/sysconfig/network-scripts/ifcfg-ens33

修改配置: ONBOOT=yes

重启网络服务: service network restart

ifconfig command not found

需要安装 net-tools 

yum update

yum -y install net-tools

文章参考:

linux安装nginx - 行者无疆 - 博客园 (cnblogs.com)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanheiccke
系列文章
更多 icon
同类精品
更多 icon
继续加载