日誌太多怎麼搞?一套爬蟲監控系統全搞定!

  • 2019 年 11 月 5 日
  • 筆記

作者: Lateautumn4lin

來源:雲爬蟲技術研究筆記

前言

很多讀者也諮詢過我怎麼去監控爬蟲系統的日誌?這裡我們給出一個通用的輕量級監控系統架構方式—ELK+Filebeat+Docker,都知道分佈式爬蟲系統是由一個高可用的控制中心配合多個彈性工作節點組成,假定我們現在把各個工作節點封裝成Docker鏡像,那麼我們通過監控Docker容器的狀態來監控爬蟲系統了。

使用docker搭建elk

1、使用docker-compose文件構建elk。文件如下:

version: '3'  services:    elk:      image: sebp/elk:640      ports:        - "5601:5601"        - "9200:9200"        - "5044:5044"      environment:        - ES_JAVA_OPTS=-Xms512m -Xmx512m      volumes:        - ~dockerdata/elk:/var/lib/elasticsearch

2、執行docker-compose up -d 啟動elk。可以使用docker logs 命令查看elk啟動日誌。啟動成功後打開瀏覽器訪問 http://127.0.0.1:5601

filebeat安裝與配置

關於filebeat本文也不做過多介紹。只講解安裝與配置。

1、filebeat的docker-composep

version: '3'  services:    filebeat:      image: prima/filebeat:6      #restart: always      volumes:        - ./config/filebeat.yml:/filebeat.yml        - ~/dockerdata/filebeat:/data        - /var/lib/docker/containers:/var/lib/docker/containers

掛載說明

  • filebeat.yml配置需要在本地有對應文件,稍後會說到
  • filebeat抓取日誌進度數據,掛載到本地,防止filebeat容器重啟,所有日誌重新抓取
  • 因為要收集docker容器的日誌,所以要掛在到docker日誌存儲目錄,使它有讀取權限

2、filebeat配置文件設置

  • 在docker-compose.yml同級目錄新建config文件夾
  • 在config文件下新建filebeat.yml文件,文件內容如下:
filebeat.prospectors:  - type: log    enabled: true    paths:      - /var/lib/docker/containers/*/*.log #需要讀取日誌的目錄#    json.keys_under_root: true # 因為docker使用的log driver是json-file,因此採集到的日誌格式是json格式,設置為true之後,filebeat會將日誌進行json_decode處理    json.add_error_key: true #如果啟用此設置,則在出現JSON解組錯誤或配置中定義了message_key但無法使用的情況下,Filebeat將添加「error.message」和「error.type:json」鍵。    json.message_key: log #一個可選的配置設置,用於指定應用行篩選和多行設置的JSON密鑰。如果指定,鍵必須位於JSON對象的頂層,且與鍵關聯的值必須是字符串,否則不會發生過濾或多行聚合。    tail_files: true    # 將error日誌合併到一行    multiline.pattern: '^([0-9]{4}|[0-9]{2})-[0-9]{2}'    multiline.negate: true    multiline.match: after    multiline.timeout: 10s  #  registry_file: /opt/filebeat/registry  #-------------------------- Elasticsearch output ------------------------------  # 直接輸出到elasticsearch,這裡的hosts是elk地址,端口號是elasticsearch端口#  output.elasticsearch:    hosts: ["10.9.70.62:9200"]    #==================== Elasticsearch template setting ==========================    setup.template.name: "filebeat.template.json"  setup.template.fields: "filebeat.template.json"  setup.template.overwrite: true  setup.template.enabled: false    # 過濾掉一些不必要字段#  processors:  - drop_fields:      fields: ["input_type", "offset", "stream", "beat"]
  • 在config文件下新建filebeat.template.json文件,文件內容如下:
{    "mappings": {      "_default_": {        "_all": {          "norms": false        },        "_meta": {          "version": "5.1.2"        },        "dynamic_templates": [          {            "strings_as_keyword": {              "mapping": {                "ignore_above": 1024,                "type": "keyword"              },              "match_mapping_type": "string"            }          }        ],        "properties": {          "@timestamp": {            "type": "date"          },          "beat": {            "properties": {              "hostname": {                "ignore_above": 1024,                "type": "keyword"              },              "name": {                "ignore_above": 1024,                "type": "keyword"              },              "version": {                "ignore_above": 1024,                "type": "keyword"              }            }          },          "input_type": {            "ignore_above": 1024,            "type": "keyword"          },          "message": {            "norms": false,            "type": "text"          },          "meta": {            "properties": {              "cloud": {                "properties": {                  "availability_zone": {                    "ignore_above": 1024,                    "type": "keyword"                  },                  "instance_id": {                    "ignore_above": 1024,                    "type": "keyword"                  },                  "machine_type": {                    "ignore_above": 1024,                    "type": "keyword"                  },                  "project_id": {                    "ignore_above": 1024,                    "type": "keyword"                  },                  "provider": {                    "ignore_above": 1024,                    "type": "keyword"                  },                  "region": {                    "ignore_above": 1024,                    "type": "keyword"                  }                }              }            }          },          "offset": {            "type": "long"          },          "source": {            "ignore_above": 1024,            "type": "keyword"          },          "tags": {            "ignore_above": 1024,            "type": "keyword"          },          "type": {            "ignore_above": 1024,            "type": "keyword"          }        }      }    },    "order": 0,    "settings": {      "index.refresh_interval": "5s"    },    "template": "filebeat-*"  }
  • 執行docker-compose up -d 啟動filebeat。

在需要抓取docker日誌的所有主機上按照以上步驟安裝運行filebeat即可。到這一步其實就已經可以在elk裏面建立索引查抓取到的日誌。但是如果docker容器很多的話,沒有辦法區分日誌具體是來自哪個容器,所以為了能夠在elk里區分日誌來源,需要在具體的docker容器上做一些配置,接着看下面的內容

docker容器設置

可以給具體的docker容器增加labels,並且設置logging。參考以下docker-compose.yml

version: '3'  services:    db:      image: mysql:5.7      # 設置labels      labels:        service: db      # logging設置增加labels.service      logging:        options:          labels: "service"      ports:        - "3306:3306"

重新啟動應用,然後訪問http://127.0.0.1:5601 重新添加索引。查看日誌,可以增加過濾條件 attrs.service:db,此時查看到的日誌就全部來自db容器。結果如下圖所示: