快速用ELK搭建日志收集平台

女爷i 2022-11-20 04:59 294阅读 0赞

46a35e2b42d9750e4585711887056c40.png

elasitc.co

ELK是elastic.co发布的三个产品,分别是elasticsearch, logstash, kibana,分别用来做搜索引擎、日志收集和报表展现。这三个东西经常被用到的业务场景就是日志收集展现。
本文将从实用角度出发,教你如何用ELK快速搭建一个日志收集平台。

elasticsearch

  • 运行elasticsearch
    ./bin/elasticsearch -d

  • 测试运行状态

    1. curl localhost:9200
  • 创建索引

    elasticsearch里面的表叫做索引(index),可以通过http请求的方式来创建索引,创建的方式是通过put请求,curl脚本如下:

    1. curl -X PUT \
    2. http://localhost:9200/test1 \
    3. -H 'cache-control: no-cache' \
    4. -H 'postman-token: 438a164f-2ded-b73e-b2ab-e53dbd7f800c' \
    5. -d '{
    6. "settings" : {
    7. "index" : {
    8. "number_of_shards" : 3,
    9. "number_of_replicas" : 2
    10. }
    11. }
    12. }'
  • 常见问题

    • 启动失败

    ERROR: bootstrap checks failed

    1. max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

这个问题是操作系统的vm.max_map_count参数设置太小导致的,解决方法:

  1. $ sudo sysctl -w vm.max_map_count=262144
  2. vm.max_map_count = 262144

logstash

  • 运行logstash

    1. ./bin/logstash -e 'input { stdin {} } output {stdout{}}'

    如果运行正常,则输入hello的运行结果如下:

    1. hello
    2. 2017-06-21T10:12:40.471Z MacBook-Pro.local hello

    一般我们都是将配置写到配置文件,用-f参数来运行,如:

    1. ./bin/logstash -f test.config

    其中test.config里面包含的内容就是之前-e参数所指向的运行参数。

  • 搭建http接口和json格式化

    logstash配置文件由三个部分组成,上面已经提到了input和output两个部分,另外还有一个是filter,分别代表输入->过滤->输出,现在我们配置一个http输入、通过json格式化、输出到elasticsearch的logstash配置,配置文件内容如下:

    1. dengzongrongdeMacBook-Pro:logstash-5.4.2 RoyDeng$ cat test.config
    2. input {
    3. http {
    4. port => 31311
    5. type => "http"
    6. codec => "json"
    7. }
    8. }
    9. filter {
    10. json {
    11. source => "message"
    12. }
    13. }
    14. output {
    15. elasticsearch {
    16. hosts => "localhost"
    17. index => "test"
    18. }
    19. }

    通过post请求,可以看到elasticsearch里面增加了数据,post请求如下:

    1. curl -X POST \
    2. http://localhost:31311/ \
    3. -H 'cache-control: no-cache' \
    4. -H 'postman-token: 888428ef-ee19-1030-9de4-6c4b333e4bca' \
    5. -d '{"action":"test"}'

Kibana

  • 配置运行kibana

    下载kibana

    修改config/kibana.yml配置文件,修改elastic search url,如下:

    1. # The URL of the Elasticsearch instance to use for all your queries.
    2. elasticsearch.url: "http://localhost:9200"

    然后运行./bin/kibana启动kibana

  • 配置报表

e3e0f99d48a802539ea42db7d256f15b.png

kibana_config.png

上面显示的Time-field表示日志的时间,这个参数是必须的,因为kibana展现的一个重要维度就是时间,你上传的数据必须有一个字段代表时间。
当然,如果你是用logstash上传的数据,那么这个字段不需要你配置,logstash会自动帮你加上,但是logstash帮你加的时间是上传的时间。如果你不是希望用上传时间作为时间维度的话,这个字段就需要你上传的时候自己加上了。

然后在首页就能看到刚刚上传上去的数据了

e8aa8140271980a4b2cf96eed4c3b90f.png

kibana-dashboard.png

以上就是快速搭建日志平台的方法,后续会陆续补充ELK框架的一些高级用法,欢迎大家一起讨论。
关注我的公众号,学习更多

6d0cea41fa0da4842c92c8d2c1780c58.png

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读