本頁面縮網址: https://hackmd.io/@estraining/DevOpsDaysTaipei2022
講者: 喬叔 (Joe Wu)
Facebook 粉絲頁: 喬叔 - Elastic Stack 技術交流
這份文件,已於 2023.09 將版本從 8.4.1 更新至 8.9.0。
請注意,由於這次工作坊執行的 docker containers 數量眾多,記憶體的要求較高,如果記憶體不到 16GB 的話幾乎很難順利運作,若是要開雲端主機的話,可以參考 - 如果要使用雲端主機,可以設定-SSH-Tunnel-來存取 的設定方式。
請先在電腦中安裝以下所需的執行環境:
這次的工作坊,使用的是 Elastic 在 GitHub 所提供的 apm-integration-testing 開源專案。
因為在操作時會需要 build Docker image 以及下載一些 Elastic Stack 的 Docker images,為了避免工作坊的時間被佔用在處理 build & download,建議請大家先完成以下的步驟。
請注意,喬叔有特別為了這次工作坊進行一些準備,所以請直接到喬叔 fork 出來的 GitHub 專案進行下載 https://github.com/joecwu/apm-integration-testing/,不要下載到 Elastic 官方版的哦!
可使用 git clone
的指令,或是直接下載 Zip 壓縮檔。
有以下兩種選擇,自行 build 或是直接抓喬叔準備好的 docker images.
2-1. 自行 build:
在專案的根路徑下,執行以下 compose.py build
的指令,以建立及下載所需要使用到的 Docker images:
Mac or Linux:
Windows:
2-2. 直接使用喬叔 build 好的 docker image
Mac M1:
有一些 opbeans 的 docker base image,是使用 AMD64 所建置,所以使用 Mac M1 或 M2,請開啟 Rosetta 以正常模擬 AMD64 的執行環境來運行這些 Containers。
Linux:
Linux 版本還保持在 8.4.1 ,若要使用 8.9.0 請自行 build。
Windows:
Window 版本還保持在 8.4.1 ,若要使用 8.9.0 請自行 build。
其他平台
其他平台需自行 build,否則 opbeans-go
和 opbeans-rum
會無法成功執行。
使用 docker images
指令,確認有存在下列 Docker Images.
使用自行 build,請用:docker-compose up -d
若是使用另外的 yml 檔,請指定 -f
參數:docker-compose -f ./docker-compose-linux.yml up -d
請確認 containers 都能正常運作 (狀態為 healthy
),若有問題,可先參考 FAQ
並使用 docker ps
確認 containers 都有正常的運作:
可以透過 http://localhost:5601 存取 Kibana,確認 Elastic Stack 服務有正常啟動。
登入帳密為
admin
與changeme
若遇到任何問題,歡迎透過 FB 粉絲頁 喬叔 - Elastic Stack 技術交流 與我聯繫。
APM Integration Testing 是一個公開在 GitHub 的開放原始碼的專案,這個專案主要語言是用 Python 撰寫,並且使用 Docker 來運作 Elastic Stack 的各種服務以及 opbeans 這個 Demo 專用的庫存管理系統,讓我們能夠擁有一個 Elastic APM 所需要執行的情境,並且能夠將當中的某些元件替換成真實運作的版本,可以協助開發人員進行 debug,或是協助整合測試 (Integration Test) 所需使用的複雜的環境。
以下幾種角色,是 APM Integration Testing 的 Docker Containers 運作起來時,裡面有的角色:
下圖是 Kibana > Observability > Trace > Service Map 的截圖。
請留意,每個 opbeans 即是一個完整的 web 專案,本身就可以獨立運作,但是當 apm-integration-testing 透過 docker-compose 啟動時,如果同時執行超過一種以上的 opbeans-XXX
,會在執行時隨機存取其他 opbeans-XXX
,創造出 service 與 service 之間互相溝通的存取,以模擬多層次或是分散式架構系統的運作情境。
請確認電腦已安裝好下列必要的執行環境:
透過執行 compose.py start
產生並執行 docker-compose.yml
。
Mac or Linux:
Windows:
如果系統資源不足,跑不動太多的 Containers 的話,可以減少一些 --with-opbeans-{XXX}
。
建議最少要啟動 opbeans-node
及 opbeans-rum
。
例如:
Mac or Linux:
Windows:
由於 opbeans-dotnet
.Net 的版本在 Apple M1 系列 CPU 建置時可能會有問題,所以我們這次直接略過不使用 opbeans-dotnet
版本,若你是 Windows 的環境,且硬體資源充足,想嘗試的話,還是可以加入 --with-opbeans-dotnet
請記得拉最新的 main branch 的 code
Mac M1:
Linux:
Windows:
其他平台需自行 build,否則 opbeans-go
和 opbeans-rum
會無法成功執行。
使用 docker-compose ps -a
或是 docker ps -a
查看執行中的 containers。
Kibana: http://localhost:5601
預設管理者帳號:admin
預設密碼:changeme
進入 Kibana > Stack Monitoring,可以成功查看 Elasticsearch Cluster 正常的運作,同時也有 Kibana 正在運作中。
filebeat.yml
修改 ./docker/filebeat/filebeat.yml
在 ## autodiscover 的區塊裡,填加以下的設定。
接下來,可以進入 Kibana (http://localhost:5601) 在 Observability > Logs > Stream (http://localhost:5601/app/logs/stream) 查看是否 Log 有成功傳送到 Elasticsearch 中。
可以在搜尋框中,嘗試打入 event.dataset: opbeans-
,自動完成的功能會列出目前有的 event.dataset
的值供選取,如果有成功將 opbeans-node
, opbeans-python
, opbeans-go
…這些服務的 Logs 收集到,就可以針對這些服務進行篩選。
metricbeat.yml
修改 ./docker/metricbeat/metricbeat.yml
在 metricbeat.monitors
的部份配置以下設定:
在 metricbeat.autodiscover
的部份配置以下設定:
接下來,可以進入 Kibana 在 Observability > Infrastructure > Inventory (http://localhost:5601/app/metrics/inventory) ,切換 Show 為 Docker Containers
查看是否 Metricbeat 有成功傳送資料到 Elasticsearch 中。
這個部份由於需要改 Code,這次 Workshop 時間有限,不在這邊練習,目前執行的版本,已經都實作好 APM Agents 的部份,同時 APM Server 也透過 Elastic Agent 在運作了。
除了參考 Elastic 官方 APM 的說明文件,可以配合從 Elastic GitHub 搜尋 opbeans-
,查看目前所執行的 opbeans 各種語系專案的實作方式。
例如 opbeans-node
的 NodeJS 的 server.js
實作參考。
heartbeat.yml
修改 ./docker/heartbeat/heartbeat.yml
在 heartbeat.monitors
的部份配置以下設定:
另外在 heartbeat.autodiscover
的部份配置以下設定:
接下來,可以進入 Kibana 在 Observability > Uptime > Monitors (http://localhost:5601/app/uptime) 查看是否 Heartbeat 的 monitors 有成功傳送到 Elasticsearch 中。
增加 heartbeat.monitors
的配置:
建立新的 Alert,選擇 Rule Type 為 Uptime monitor status,並依照需求填入設定。
依需求建立 Web Uptime SLI-1
Actions 的部份,以 Index 為例,建立新的 Index Connector:
Document to index 的 index document 簡單範例:
建立新的 Alert,選擇 Rule Type 為 APM Latency threshold,並依照需求填入設定。
依需求建立 Web Latency SLI-2-1
依需求建立 Web Latency SLI-2-2
Document to index 的 index document 使用一樣的簡單範例:
在 Kibana > Alert > Manage Rules (右上角) 可以查看 Rules 設定結果。
composer.py
時 Docker build 出現 GPG error
docker 出現 GPG error: At least one invalid signature was encountered 相關問題及解決辦法。
There are a few reasons why you encounter these errors:
There might be an issue with the existing cache and/or disc space. In order to fix it you need to clear the APT cache by executing: sudo apt-get clean and sudo apt-get update.
The same goes with existing docker images. Execute: docker image prune -f and docker container prune -f in order to remove unused data and free disc space.
If you don’t care about the security risks, you can try to run the apt-get command with the –allow-unauthenticated or –allow-insecure-repositories flag. According to the docs:
Ignore if packages can’t be authenticated and don’t prompt about it. This can be useful while working with local repositories, but is a huge security risk if data authenticity isn’t ensured in another way by the user itself.
Finally, on MacOS, where Docker runs inside a dedicated VM, you may need to increase the disk available to Docker from the Docker Desktop application (Settings -> Resources -> Advanced -> Disk image size).
執行以下指令會將你的 Docker 環境清空,包含 images, dangling build caches, containers,請確認後再操作。
查看 docker-compose logs {container_name}
時,發現以下的錯誤:
這代表該 Docker Image 不是用對應的 platform 所建置,需自行 build imgae 才能使用。
unhealthy
的錯誤有可能是當下某一些有相依性的 service 還沒有正常的啟動,可以先重新使用 docker-compose
啟動一次試試,看看是否有改善。
若是依然有服務沒辦法正常啟動,可使用 docker ps -a
查看沒有正常啟動的服務是哪些,並進一步使用 docker logs {{DockerContainerName}}
查看錯誤訊息。
opbeans-dotnet
在 Apple M1 無法啟動使用 docker logs localtesting_8.4.1_opbeans-dotnet
查看,發現以下錯誤:
在這次 Workshop 中,若是使用 Apple M1,我們先不啟用 opbeans-dotnet
先清除所有正在運作中的 containers,以下方式二擇一:
composer.py
docker-compose
如果有需要,可以一併刪除已產生的 docker volume.
~/.ssh/config
的參考設定如下:
以上述的例子,設定完成後,執行 ssh gcptunnel
即可將本機的 port 轉接到雲端主機。
以上有關 .yaml
檔的修改,已 commit 在喬叔 GitHub 的 2022-devopsdays-workshop
branch 之中,可以直接 git checkout 2022-devopsdays-workshop
,或是點此下載。