# Rancher Monitoring 配置告警信通知 * 需先安裝好 SMTP Server * 安裝 Rancher Monitoring ## 目標 * 如果我有一個 pod 一直啟動失敗,設定發送郵件告知我 ## 實作 * 建立一個 crash 的 pod ``` $ kubectl create ns test $ kubectl -n test create deploy crash --image=quay.io/hahappyman/myapp $ kubectl -n test get po NAME READY STATUS RESTARTS AGE crash-85bcfd65f6-mh8bq 1/2 CrashLoopBackOff 1 (108s ago) <invalid> ``` * Monitoring -> Advanced -> PrometheusRules 在 `test` namespace 建立規則 ![image](https://hackmd.io/_uploads/S1GaW-o5yx.png) * PromQL 表達式使用以下語法 ``` kube_pod_container_status_waiting_reason{reason="CrashLoopBackOff"} > 0 ``` * 需帶有 `podcrashalert=true` 的標籤 ![image](https://hackmd.io/_uploads/H1qMGZi91x.png) * 如果有 pod crash 就會出現告警 ![image](https://hackmd.io/_uploads/H1sYPli9yg.png) ### 設定 AlertmanagerConfigs * 如果是要發出 `test` namespace 的告警,那麼 AlertmanagerConfigs 就要在 `test` namespace 建立。 1. 建立 Route,Monitoring -> AlertmanagerConfig -> Create ![image](https://hackmd.io/_uploads/HJYw_ejckx.png) 2. 設定 Matchers 為上一個 PrometheusRules 步驟所新增的標籤。此時還不用加入 Receiver。 ![image](https://hackmd.io/_uploads/Hkvnuxo5yg.png) > 名稱: podcrashalert > Matchers: podcrashalert=true > Match Type: Match Eqyal 3. 上一步 Create 後,再次編輯 AlertmanagerConfigs ![image](https://hackmd.io/_uploads/Sk5btlsckl.png) 4. Add Receiver ![image](https://hackmd.io/_uploads/ByUEKgoq1x.png) 5. 在 test namespace 建立 SMTP 憑證 secret,需手動將憑證匯入後做出 secret。 ``` $ kubectl -n test create secret generic smtp-tls-secret \ --from-file=tls.crt=/root/smtp/server.crt \ --from-file=tls.key=/root/smtp/server.key \ --from-file=ca.crt=/root/smtp/ca.crt ``` 6. 選擇 Email 並設定 > Name: myalert > Target: email@example.com # 收件人位置 > Sender: rancher@lab.com # 寄件人位置 > Host: smtp.cooloo9871.com:587 # SMTP 的位置,這邊使用自建 SMTP Server ![image](https://hackmd.io/_uploads/rkn8Kljc1e.png) * email 一定要走 SSL 認證 ![image](https://hackmd.io/_uploads/B1TL2gi51e.png) 7. 再重新編輯 AlertmanagerConfig,在 Route 中綁定 Receiver。 ![image](https://hackmd.io/_uploads/BJgonei5yg.png) * 檢查 alertmanager pod 設定檔是否符合預期 ``` $ kubectl -n cattle-monitoring-system exec alertmanager-rancher-monitoring-alertmanager-0 -- cat /etc/alertmanager/config_out/alertmanager.env.yaml ``` ## 驗證 * 確認信箱已收到 podcrash 告警信 ![image](https://hackmd.io/_uploads/BJhVVZo5Jl.png) ## 設定 AlertmanagerConfig 可以警告所有 namespace * 預設情況下,建立出來的 AlertmanagerConfig 會自動新增上 `namespace="xxx"` 的 Matcher,這是由於 Alertmanager 的 alertmanagerConfigMatcherStrategy 設定預設為 OnNamespace。如果需要一個 AlertmanagerConfig 可以警告所有 namespace,那麼需要修改 alertmanagerConfigMatcherStrategy 的配置。 * 在 Rancher 修改 Monitoring 配置,透過 YAML 編輯,在 `alertmanager.alertmanagerConfigMatcherStrategy` 下加入 `type: None` ``` alertmanagerConfigMatcherStrategy: type: None ``` ![image](https://hackmd.io/_uploads/SyfcnON1ge.png) * 更新完畢後,可以在任何 namespace 下根據先前的步驟建立 AlertmanagerConfig,而這個 AlertmanagerConfig 預設會對所有 namespace 進行警告。如果後續需要針對 namespace 進行告警,則需要在 AlertmanagerConfig 的路由配置中,新增 namespace 的 Matcher。 * 在 Alertmanager -> Status 可以看到此時並不會添加 `namespace="xxx" 的 Matcher` ![image](https://hackmd.io/_uploads/BJwLaO4Jxe.png) ![image](https://hackmd.io/_uploads/B1hhC_4kxe.png)