Contents

Windows 快速安裝 Loki 方法

最近有機會在 Windows 測試機上安裝 Loki 服務,來解決長久以來查看文字 txt log 的問題。同事原本想用 ELK 方案,不過這是一個非常吃資源的大怪獸,後來我們決定試試我玩過的 Loki。

前面研究的 Loki

說來慚愧,之前在我家主要蒐集 traefik 的一些 log,相關設定我沒有到很熟悉。

主要我知道 ELK 對應到 Loki 一樣有三個元件:Grafana + Loki + Promtail,它們對應做的事情也差不多。

Grafana:查詢 Loki 紀錄的平台。
Loki:蒐集 log 的地方。
Promtail:對應 Filebeat,負責解析 log 並傳送到 Loki(類似 ELK 中的 Filebeat 傳到 ElasticSearch)。

安裝 Lab 說明

這次因為我的環境只有一台機器,Promtail 不打算安裝,這邊的規劃是 .Net 專案除了會記錄 txt log,也會直接推送 log 到 Loki。

:::info
這邊推薦看以下專業安裝教學,本文內容是從這些文章整理而來:

安裝教學

Grafana

這邊下載 Grafana。

https://user-images.githubusercontent.com/6058558/271972419-87ba8599-4a2e-4356-b981-949b217f2dbd.png

安裝 Grafana 就不詳細說明,就一直下一步就對了。

https://user-images.githubusercontent.com/6058558/271973603-53a124ee-e517-403c-bca6-d3c33a955f8d.png

Loki

Github grafana/loki 這邊下載 loki-windows-amd64.exe.zip,注意這邊不是 lokicli 開頭喔!

https://user-images.githubusercontent.com/6058558/271974424-1c1fdfbc-adfe-48f3-89fd-b8f4dc32ecbb.png

Promtail

本環境不打算安裝 Promtail,如果需要可以參考以下文章:

設定 Loki Config

可以到官方查看設定檔說明:Grafana Loki configuration parameters | Grafana Loki documentation官方文件裡面也有放範例。

這邊使用官方的 Local-Configuration-Example.yaml,將設定檔存成 config.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
auth_enabled: false

server:
  http_listen_port: 3100

common:
  ring:
    instance_addr: 127.0.0.1
    kvstore:
      store: inmemory
  replication_factor: 1
  path_prefix: /tmp/loki

schema_config:
  configs:
  - from: 2020-05-15
    store: boltdb-shipper
    object_store: filesystem
    schema: v11
    index:
      prefix: index_
      period: 24h

這邊設定完就可以直接在 PowerShell 執行 loki 程式。一般教學都會教你加上 config 參數,但我比較懶。😅

https://user-images.githubusercontent.com/6058558/271977265-f1a89854-e84e-497e-b016-9fdb937bad56.png

登入 Grafana

登入 Grafana 頁面,網址是 http://localhost:3000/login,帳號密碼都是 admin。進入之後會要求你重新設定新密碼。

https://user-images.githubusercontent.com/6058558/271977494-0da45c62-525a-4ee6-a75b-f5a2bc3424c2.png

進入 Grafana 後,前往 Connections 功能頁。

https://user-images.githubusercontent.com/6058558/271978071-bcac5ecf-c419-46f8-85bf-85fc748f6228.png

搜尋 loki,選擇 Loki

https://user-images.githubusercontent.com/6058558/271978323-8f1f4d31-a240-4c0b-aac2-def91510770d.png

選擇 Add new data source

https://user-images.githubusercontent.com/6058558/271978552-8366da45-4a12-4360-9cc0-8ecf5bddd80a.png

URL 輸入 http://localhost:3100,然後可以按 Save & test

https://user-images.githubusercontent.com/6058558/271979230-23b5d2ff-14a3-41ec-9641-f4348cb62409.png

別擔心,你可能還沒看到成功訊息。通常如果你有安裝 Promtail 並正確設定,可能就會成功。

1
Data source connected, but no labels were received. Verify that Loki and Promtail are correctly configured.

這個訊息簡單來說,是因為目前 Loki 裡面是空的,所以 Grafana 不給你加入。這時候只要簡單呼叫 API 就可以解決了。開啟 Git Bash,執行下面語法:

1
2
curl -v -H "Content-Type: application/json" -XPOST -s "http://localhost:3100/loki/api/v1/push" --data-raw \
  '{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1696254175000000000", "fizzbuzz" ] ] }]}'

參考:Grafana Loki HTTP API | Grafana Loki documentation

完成後,再按一次 Save & test 即可成功。

https://user-images.githubusercontent.com/6058558/271982863-f18a091c-cc66-4a1f-a466-5a739360e16e.png

:::warning
注意:如果打 Loki push API 沒有成功,但不影響後續操作。你可能會得到 entry for stream '{foo="bar2"}' has timestamp too old: 2019-10-12T02:23:58+08:00, oldest acceptable timestamp is: 2023-09-25T21:35:56+08:00 錯誤。原因是範例中的時間太舊,你可以改成現在的時間戳記。

時間轉換可用:Unix时间戳(Unix timestamp)转换工具 - 站长工具
:::

把 Loki 設定成 Windows Service

方法一:使用 sc 命令

可以使用 Windows 內建的 sc 命令來新增服務:

1
sc.exe create <new_service_name> binPath= "<path_to_the_service_executable>"

但這個方法在實際使用上可能會有一些限制。

參考:

方法二:使用 WinSW

推薦使用 WinSW(Windows Service Wrapper)來註冊服務。建立一個 XML 設定檔:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<service>
    <!-- 該服務的唯一標識 -->
    <id>Grafana Loki</id>
    <!-- 該服務的名稱 -->
    <name>Grafana Loki</name>
    <!-- 該服務的描述 -->
    <description>Grafana Loki</description>
    <!-- 要運行的程序路徑 -->
    <executable>loki-windows-amd64</executable>
    <!-- 攜帶的參數 -->
    <arguments>--config.file=config.yaml</arguments>
    <!-- 第一次啟動失敗 60秒後重啟 -->
    <onfailure action="restart" delay="60 sec"/>
    <!-- 第二次啟動失敗 120秒後重啟 -->
    <onfailure action="restart" delay="120 sec"/>
    <!-- 日誌模式 -->
    <logmode>append</logmode>
    <!-- 指定日誌文件目錄(相對於executable配置的路徑) -->
    <logpath>logs</logpath>
</service>

使用 WinSW 的常用命令:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// 註冊服務
winsw.exe install
// 卸載服務
winsw.exe uninstall
// 啟動服務
winsw.exe start
// 停止服務
winsw.exe stop
// 重啟服務
winsw.exe restart
// 查看狀態
winsw.exe status

參考:使用 winsw 將程式註冊為系統服務 - 夜升筆談

彩蛋

以下是一些相關的參考資料:

如果遇到多人查詢時出現 too many outstanding requests 錯誤,可以參考最後一篇文章的解決方法。