目錄html
一、使用docker-compose文件構建elk。文件以下:mysql
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
二、執行docker-compose up -d 啓動elk。可使用docker logs 命令查看elk啓動日誌。啓動成功後打開瀏覽器訪問 http://127.0.0.1:5601sql
關於filebeat本文也不作過多介紹。只講解安裝與配置。docker
一、filebeat的docker-composepjson
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配置文件設置app
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"]
{ "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日誌的全部主機上按照以上步驟安裝運行filebeat便可。到這一步其實就已經能夠在elk裏面創建索引查抓取到的日誌。可是若是docker容器不少的話,沒有辦法區分日誌具體是來自哪一個容器,因此爲了可以在elk裏區分日誌來源,須要在具體的docker容器上作一些配置,接着看下面的內容elasticsearch
能夠給具體的docker容器增長labels,而且設置logging。參考如下docker-compose.ymlide
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容器。結果以下圖所示:
3d
http://www.devzxd.top/2018/10/25/elk-filebeat-dockerlogs.html