使用 Docker Compose 快速建立 Graylog 日誌管理系統

Graylog 是一套開源的日誌管理系統,也具有搜尋、警報系統。
很有趣的是 Graylog 的 GitHub 帳號是 Graylog2 ,一開始還以為找錯了,後來發現原來是 Graylog 已經被使用走了。

Docker Compose

使用官方 docker compose 範本:
    
git clone https://github.com/Graylog2/docker-compose.git
    

因為資料夾名稱會是專案名稱,這裡為了怕未來搞混,先將資料夾改個名字,並進入到 docker-compose.yml 所在位置
    
mv docker-compose graylog-docker-compose
cd graylog-docker-compose

cd open-core
    

複製一份設定檔
    
cp .env.example .env
    

修改設定檔:

GRAYLOG_PASSWORD_SECRET 是密碼,至少要 64 個字元。
GRAYLOG_ROOT_PASSWORD_SHA2 是密碼的 SHA-256
    
# You MUST set a secret to secure/pepper the stored user passwords here. Use at least 64 characters.
# Generate one by using for example: pwgen -N 1 -s 96
# ATTENTION: This value must be the same on all Graylog nodes in the cluster.
# Changing this value after installation will render all user sessions and encrypted values in the database invalid. (e.g. encrypted access tokens)
GRAYLOG_PASSWORD_SECRET="gVG7t4VFXqBuSIlnnLIyI4aCnzF6tzTQQvfwIbSdUly75kYSW5J2fPZeCNye4pycmiurL8sHjrrh1bZyrWL73olvuZQQyrXX"

# You MUST specify a hash password for the root user (which you only need to initially set up the
# system and in case you lose connectivity to your authentication backend)
# This password cannot be changed using the API or via the web interface. If you need to change it,
# modify it in this file.
# Create one by using for example: echo -n yourpassword | shasum -a 256
# and put the resulting hash value into the following line
# CHANGE THIS!
GRAYLOG_ROOT_PASSWORD_SHA2="8eaec6364c10b392b348a41e18cf750fef2276e1e1b3ac7cb09c0d7adf380295"
    

在 linux 中可以使用下面的指令安裝 pwgen 套件:
    
sudo apt install pwgen
    

然後就可以使用 pwgen 產生密碼:
    
pwgen -N 1 -s 96
    

在 linux 中可以使用下面的指令產生 SHA-256 ,請將 gVG7t4... 的部分替換為自己的密碼
    
echo -n gVG7t4VFXqBuSIlnnLIyI4aCnzF6tzTQQvfwIbSdUly75kYSW5J2fPZeCNye4pycmiurL8sHjrrh1bZyrWL73olvuZQQyrXX | shasum -a 256
    

修改版本

在筆者使用的當下 docker-compose.yml 中使用的 graylog docker image 是 5.0 版本,最新的已經出到 5.1.5 版本了,所以筆者在 .env 檔案的最後面加了下面這一條來手動指定 docker image 版本:
    
GRAYLOG_IMAGE="graylog/graylog:5.1.5"
    

啟動 docker compose

    
docker compose up -d 
    

順便附上關閉 docker compose 的指令:
    
docker compose down
    

登入

使用瀏覽器開啟下面的連結:
    
http://localhost:9000/
    

帳號: admin
密碼: 就是在 .env 中設定的 GRAYLOG_PASSWORD_SECRET 參數內容

接收資料輸入

Graylog 預設不會接收資料輸入,需要點選上方的 System > Inputs ,選擇 GELF HTTP,點選 Launch new input

在 Title 的位置輸入讓自己可以辨識的標題

然後捲動到最下方點選 Launch input

然後就可以使用 API 發送的訊息:
    
curl --location 'http://localhost:12201/gelf' \
--header 'Content-Type: application/json' \
--data '{
    "id":1,
    "message":"hi"
}'
    

在 Search 中就會看到資料了!



參考資料:
GitHub - Graylog2/docker-compose
docker hub - graylog/graylog

留言