filebeat + 监控elk

image.png

filebeat收集日志同步到redis或kafka,轻量级!!

下载方式

1,安装filebeat

[root@\ es02~]# wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.12.1-x86_64.rpm

[root@\ es02~]# yum install -y filebeat-7.12.1-x86_64.rpm 

# 查看配置文件
[root@\ es02~]# grep -Ev '#' /etc/filebeat/filebeat.yml | grep -E '[^\ ]'
filebeat.inputs:
- type: log
  enabled: false
  paths:
    - /var/log/*.log
- type: filestream
  enabled: false
  paths:
    - /var/log/*.log
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 1
setup.kibana:
output.elasticsearch:
  hosts: ["localhost:9200"]
processors:
  - add_host_metadata:
      when.not.contains.tags: forwarded
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~


使用文档

2,自定义配置文件,收集文件日志,输出到文件中

# 原先的配置文件做备份
[root@\ es02~]# mv /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml.bak



# 编写配置文件(输出到文件中)
[root@\ es02~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:   # filebeat输入
- type: log
  paths:
    - /var/log/messages
    - /var/log/*.log
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 1
setup.kibana:
output.file:        # filebeat输出
  path: "/tmp/filebeat"
  filename: filebeat.log
processors:
  - add_host_metadata:
      when.not.contains.tags: forwarded
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~







# 启动配置文件
[root@\ es02~]# /usr/bin/filebeat -c /etc/filebeat/filebeat.yml
helli

hello
或者

[root@\ es02~]# systemctl start filebeat.service 



# 查看日志文件
[root@\ es02/tmp/filebeat]# ll
total 9152
-rw------- 1 root root 9371024 May 13 16:34 filebeat.log



[root@\ es02/tmp/filebeat]# cat filebeat.log
... ...
{"@timestamp":"2021-05-13T08:34:02.914Z","@metadata":{"beat":"filebeat","type":"_doc","version":"7.12.1"},"log":{"offset":768680,"file":{"path":"/var/log/messages"}},"message":"May 13 16:33:57 es02 systemd-logind: New session 1830 of user root.","input":{"type":"log"},"ecs":{"version":"1.8.0"},"host":{"os":{"platform":"centos","version":"7 (Core)","family":"redhat","name":"CentOS Linux","kernel":"3.10.0-1160.21.1.el7.x86_64","codename":"Core","type":"linux"},"id":"18321d7070024b2cbb8a9d8132640345","containerized":false,"name":"es02","ip":["192.168.15.71","fe80::20c:29ff:fe43:5179","172.16.1.71","fe80::20c:29ff:fe43:5183"],"mac":["00:0c:29:43:51:79","00:0c:29:43:51:83"],"hostname":"es02","architecture":"x86_64"},"agent":{"version":"7.12.1","hostname":"es02","ephemeral_id":"9eb5d3bc-e88b-4fd4-bf89-a14843c3dd48","id":"bc9227b9-4e96-4380-baa8-6e3ab92e6f41","name":"es02","type":"filebeat"}}

把日志内容复制到json里查看
 如下图。图1
图1

3,收集日志,输出到redis

[root@\ es02~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:   # filebeat输入
- type: log
  paths:
    - /var/log/messages
    - /var/log/*.log
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 1
setup.kibana:
output.redis:
  hosts: ["192.168.15.71"]
  password: ""
  key: "filebeat"
  db: 0
  timeout: 5
processors:
  - add_host_metadata:
      when.not.contains.tags: forwarded
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~

# 修改redis配置文件监听端口
[root@\ es02~]# vim /etc/redis
bind 0.0.0.0

# 启动redis
[root@\ es02~]# systemctl restart redis

# 重启filebeat
[root@\ es02~]# systemctl restart filebeat.service 


# 进入redis查看
127.0.0.1:6379> keys *
1) "filebeat"


4, 从reids输出到Elasticsearch

查看文档
https://www.elastic.co/guide/en/logstash/current/plugins-inputs-redis.html




[root@\ es02~]# vim redis-system.conf
input {
  redis {

    data_type => 'list'
    host => "192.168.15.71"
    key => "filebeat"
    port => 6379
  }
}

output {

    elasticsearch {

        hosts => ["172.16.1.70:9200"] 
        index => "filebeat-systemlog-%{+YYYY.MM.dd}" 

    }

}



[root@es-02 ~]# /usr/share/logstash/bin/logstash -f redis-system.conf


查看,见下图,图2,图3

图2
图3

5,关联kibana

PS:
ES02内存不足,把redis-system.conf卸载在es02上,
重新执行/usr/share/logstash/bin/logstash -f redis-system.conf

查看日志

6,nginx反向代理


[root@\ es01~]# yum install -y nginx



[root@\ es01~]# vim /etc/nginx/conf.d/kibana.conf
server {
        listen 80;
        server_name kibana.default.cluster.local.com;

        location / {
                proxy_pass http://172.16.1.71:5601;
        }


[root@\ es01~]# nginx -t
[root@\ es01~]# systemctl restart nginx

域名登录
创建索引
查看日志

日志测试

注意:filebeat下载在es02机器上,并做了配置,所以在这台机器上测试

[root@\ es02~]#  cd /var/log/

[root@\ es02/var/log]# echo 'cute baby' >> messages 

查看日志如下图,图6

图6

nginx优化kibana

[root@\ es01~]# yum install -y httpd-tools

[root@\ es01~]# cd /etc/nginx/
[root@\ es01/etc/nginx]#  htpasswd -c auth kibana
New password: 
Re-type new password: 
Adding password for user kibana
# 密码为123456



#优化nginx配置
[root@\ es01~]# vim /etc/nginx/conf.d/kibana.conf 
server {
        listen 80;
        server_name kibana.nginx.com;

        auth_basic "User Authentication";
        auth_basic_user_file /etc/nginx/auth;

        location / {
                proxy_pass http://172.16.1.71:5601;
        }

   }
server {
        listen 80 default_server;
        server_name locahost;
        return 302 return 302 http://kibana.nginx.com/;
}


[root@\ es01~]# systemctl restart nginx

再次登录测试,如下图,图7,图8

这个时候用ip依然可以访问,如图9

图7
图8
图9

nginx继续优化kibana

# 修改kibana配置文件,只能监听内网段
[root@\ es02~]# vim /etc/kibana/kibana.yml

server.host: "172.16.1.71"


[root@\ es01~]# vim /etc/nginx/conf.d/kibana.conf
server {
        listen 80;
        server_name kibana.nginx.com;

        auth_basic "User Authentication";
        auth_basic_user_file /etc/nginx/auth;

        location / {
                proxy_pass http://172.16.1.71:5601;
        }

   }
server {
        listen 80 default_server;
        server_name locahost;
        return 302 http://kibana.nginx.com/;
}



# 重启kibana和nginx
[root@\ es02~]# systemctl restart kibana.service 
[root@\ es01~]# systemctl restart nginx

测试外网ip无法访问 见图10

图10

7,监控

搜索metric接口
prometheus 官网

# 监控的机器改配置,增加以下内容
[root@\ prometheus~]#  vim prometheus.yml 
...
- job_name: "ELK"
    static_configs:
      - targets: ["192.168.15.71:9114"]





# 部署elasticsearch export
[root@\ es02~]# docker run --rm -p 9114:9114 -e "--es.uri=http://172.16.1.70:9200/" justwatch/elasticsearch_exporter:1.1.0


已创建metric接口
查看已关联elk
测试查询监控项
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容

  • 一、什么是ELK ELK是Elasticsearch + Logstash + Kibana 这种架构的简写。这是...
    哥本哈根月光阅读 15,953评论 0 10
  • ELK 搭建及实战 JDK1.8环境搭建和Kibana实战部署 ELK介绍和JDK1.8环境搭建 实战环境 Cen...
    全村滴希望阅读 4,471评论 1 4
  • 背景 在基于elk的日志系统中,filebeat几乎是其中必不可少的一个组件,例外是使用性能较差的logstash...
    生活的探路者阅读 2,169评论 0 3
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,471评论 28 53
  • 信任包括信任自己和信任他人 很多时候,很多事情,失败、遗憾、错过,源于不自信,不信任他人 觉得自己做不成,别人做不...
    吴氵晃阅读 6,133评论 4 8