docker深入2-監控docker-swarm集羣的方案swarmprom介紹
2018/4/28
本文目的
- 引導
- 本地化
簡介
相信您也對如何監控容器化的業務感到煩惱,在此先強烈推薦您體驗一下 swarmprom
這個演示方案,期待能幫助您打造出適合自身場景的監控方案
Swarmprom is a starter kit for Docker Swarm monitoring with Prometheus, Grafana, cAdvisor, Node Exporter, Alert Manager and Unsee.
簡而言之,該方案是以下工具的組合:
caddy
網關,提供了基礎的認證功能grafana
數據展示http://<swarm-ip>:3000
prometheus
數據源http://<swarm-ip>:9090
alertmanager
告警http://<swarm-ip>:9093
unsee
告警看板http://<swarm-ip>:9094
cAdvisor
容器 metrics 收集nodeExporter
主機 metrics 收集
本地化 - alertmanager 的告警方式切換成 wechat
alertmanager
的告警 receiver
默認設置的是 slack
(這工具好用,只是你懂的,在國內網絡用起來不方便)
切換成 wechat
來接收告警,更符合國人的習慣
通過上述 2 個 PR 可以發現 wechat 相關的配置指南(文末簡介)
- 在這個 branch 中,更新的配置文件包含:
alertmanager/conf/alertmanager.yml
- 使用 wechat 作爲 receiver 的配置模版
alertmanager/templates/wechat.tmpl
- 自定義告警內容的模版
docker-compose.yml
- 更換
alertmanager
的鏡像爲prom
官方默認的的版本 - 通過 volume 映射來傳遞
wechat
相關的配置
其中,docker-compose.yml
變更的內容爲:
alertmanager:
image: prom/alertmanager:v0.14.0
networks:
- net
volumes:
- alertmanager:/alertmanager
- ./alertmanager/conf/alertmanager.yml:/etc/alertmanager/config.yml
- ./alertmanager/templates:/etc/alertmanager/templates
示例中使用了一個腳本 start.sh
來簡化操作
$ git clone https://github.com/opera443399/swarmprom.git
$ cd swarmprom
$ git checkout -b feat-alertmanager-receiver-wechat remotes/origin/feat-alertmanager-receiver-wechat
### 設置 wechat
$ vim alertmanager/conf/alertmanager.yml
### 設置訪問賬號
$ vim start.sh
#!/bin/bash
#
ADMIN_USER='admin' \
ADMIN_PASSWORD='admin' \
docker stack deploy -c docker-compose.yml mon
請先確認 docker-compose.yml
中定義的 DOCKER_GWBRIDGE_IP
和 /var/lib/docker
是正確的值
##### 配置中默認的 `IP` 是 `172.18.0.1` 如果不一致請替換
# ip -o addr show docker_gwbridge
3: docker_gwbridge inet 172.18.0.1/16 scope global docker_gwbridge\ valid_lft forever preferred_lft forever
##### 查看 docker 的根目錄
# docker info |grep 'Docker Root Dir'
Docker Root Dir: /var/lib/docker
配置 docker 節點
##### 在 docker 的配置 `daemon.json` 中增加 `metrics-addr` 相關指令
##### 請注意,爲了在容器內通過 `DOCKER_GWBRIDGE_IP` 來收集數據,使用的 ip 不是 `127.0.0.1:9323` 而是 `0.0.0.0:9323`
{
"metrics-addr" : "0.0.0.0:9323",
"experimental" : true
}
啓動監控 swarmprom
$ sh start.sh
wechat 相關的文檔介紹
<wechat_config>
# Whether or not to notify about resolved alerts.
[ send_resolved: <boolean> | default = false ]
# The API key to use when talking to the Wechat API.
[ api_secret: <secret> | default = global.wechat_secret_url ]
# The Wechat API URL.
[ api_url: <string> | default = global.wechat_api_url ]
# The corp id for authentication
[ corp_id: <string> | default = global.wechat_api_corp_id ]
# API request data as defined by the Wechat API.
[ message: <tmpl_string> | default = '{{ template "wechat.default.message" . }}' ]
[ agent_id: <string> | default = '{{ template "wechat.default.agent_id" . }}' ]
[ to_user: <string> | default = '{{ template "wechat.default.to_user" . }}' ]
[ to_party: <string> | default = '{{ template "wechat.default.to_party" . }}' ]
[ to_tag: <string> | default = '{{ template "wechat.default.to_tag" . }}' ]
重點請關注這一行:
[ message: <tmpl_string> | default = '{{ template "wechat.default.message" . }}' ]
這個默認的 message 的模版來源:
https://github.com/prometheus/alertmanager/blob/master/template/default.tmpl
是的,已經被合併到 master 上啦。
其中,定義的默認 message
格式爲:
{{ define "__subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}{{ end }}
{{ define "__text_alert_list" }}{{ range . }}Labels:
{{ range .Labels.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}Annotations:
{{ range .Annotations.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}Source: {{ .GeneratorURL }}
{{ end }}{{ end }}
{{ define "wechat.default.message" }}{{ template "__subject" . }}
{{ .CommonAnnotations.SortedPairs.Values | join " " }}
{{ if gt (len .Alerts.Firing) 0 -}}
Alerts Firing:
{{ template "__text_alert_list" .Alerts.Firing }}
{{- end }}
{{ if gt (len .Alerts.Resolved) 0 -}}
Alerts Resolved:
{{ template "__text_alert_list" .Alerts.Resolved }}
{{- end }}
AlertmanagerUrl:
{{ template "__alertmanagerURL" . }}
{{- end }}
您也可以自定義 message
來格式化數據實例,請參考示例:
$ vim alertmanager/templates/wechat.tmpl