iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 22
0
Elastic Stack on Cloud

親愛的,我把ElasticSearch上雲了系列 第 22

Day 22 Docker nginx container with filebeat

Day 22 Docker nginx container with filebeat

前言

昨天我們講解了Filebeat如何蒐集nginx log,今天我們要開始進入比較實際操作的部分。昨天及前天我們使用的log是提供的demo檔案,今天我們首先先讓大家用docker 建構一個nginx的container,等於是一個web的應用,並且蒐集這個web的log,再上傳到elastic cloud上

架構介紹

這次的Sample主要的架構是,透過docker 建構一個nginx container,並且透過volume的設定,將container的服務log往外拋到設定的volume資料夾。而這時候透過外部local端監聽的filebeat,將log拋到elastic cloud。
因此這架構,我們將網頁服務架構在container內,filebeat放在local端。

這次的教學因為主軸仍然是filebeat以及elastic cloud實際應用的流程,因此docker的操作就當作大家基本上有概念。如果不熟docker操作,可以參考docker的官網
另外因為我demo的筆電礙於作業系統關係,因此只能安裝docker toolbox,這邊簡單講解一下差異,若原生的docker,可以直接用linux核心模擬,但docker toolbox的作法是先透過Virtual Box建構一個VM,再VM中建立docker。因此要連結到docker container,必須透過VM IP (預設為192.168.99.100),另外volume的設定限於C:/Users/user中的資料夾,若安裝的是docker就沒有這個問題囉(但需要設定權限)

步驟

創立蒐集的資料夾,並且建構nginx container
一、首先我們先建立一個資料夾在D:/,名稱叫做docker-nginx-log

二、透過docker run 建構一個nginx的container,並且將container內部的port 80,mapping到外部的80,設定volume,並且設置在我們剛剛設定的資料夾

  • 特別注意,若用windows docker toolbox volume的設法必須寫成 /c/Users/user/docker-nginx-log,因為它是透過vm裡面去做兩層的對照
docker run -p 80:80 -itd -v /c/Users/user/docker-nginx-log:/var/log/nginx nginx

最後一個nginx若你沒有nginx的image,他會自己下載,並且run起來。

-itd意思是tty模式以及detach(不會將畫面固定在nginx啟動的Listen狀態)

三、修改filebeat nginx中的module設定

# Module: nginx
# Docs: https://www.elastic.co/guide/en/beats/filebeat/7.9/filebeat-module-nginx.html

- module: nginx
  # Access logs
  access:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["d:/docker-nginx-log/access.log*"]

  # Error logs
  error:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["d:/docker-nginx-log/error.log*"]

  # Ingress-nginx controller logs. This is disabled by default. It could be used in Kubernetes environments to parse ingress-nginx logs
  ingress_controller:
    enabled: false

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    #var.paths:

四、啟動filebeat

filebeat.exe -e

啟動後filebeat就會監聽D:/docker-nginx-log,而我們剛剛設置好volume,因此只要有人拜訪這個服務,log生成以後,就會被filebeat listen到。

五、拜訪服務

剛剛我們將服務port80對照到外面80,因此我們只需要拜訪http://127.0.0.1,就能夠看到預設的nginx頁面

而這樣拜訪後,可以看到D:/docker-nginx-log裡面會出現,accesslog以及error log,接著filebeat就會自動上傳了!

透過這樣蒐集,可以按照昨天的demo,去Kibana檢視這個服務nginx相關的log!


上一篇
Day 21 Collect Log with Filebeat-2
下一篇
Day 23 Docker-Compose nginx + flask container with filebeat
系列文
親愛的,我把ElasticSearch上雲了30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言