使用 Notification Manager 管理多租户通知

Notification Manager 管理 KubeSphere 中的通知。它接收来自不同发送器的告警或者通知,然后将通知发送给不同用户。

支持的发送器包括:

  • Prometheus Alertmanager
  • 自定义发送器(即将上线)

支持的接收器包括:

notification-manager

快速入门

配置 Prometheus Alertmanager 向 Notification Manager 发送告警

Notification Manager 使用端口 19093 和 API 路径 /api/v2/alerts 来接收由 KubeSphere 的 Prometheus Alertmanager 发送的告警。

为了接收 Alertmanager 告警,KubeSphere 已添加如下所示的 Alertmanager Webhook 和路由配置(编辑命名空间 kubesphere-monitoring-system 中的密钥 alertmanager-main):

将 Prometheus 告警发送至 Notification Manager:

  1. "receivers":
  2. - "name": "prometheus"
  3. "webhook_configs":
  4. - "url": "http://notification-manager-svc.kubesphere-monitoring-system.svc:19093/api/v2/alerts"
  5. "route":
  6. "routes":
  7. - "match":
  8. "alerttype": ""
  9. "receiver": "prometheus"

将事件告警发送至 Notification Manager:

  1. "receivers":
  2. - "name": "event"
  3. "webhook_configs":
  4. - "url": "http://notification-manager-svc.kubesphere-monitoring-system.svc:19093/api/v2/alerts"
  5. "send_resolved": false
  6. "route":
  7. "routes":
  8. - "match":
  9. "alerttype": "event"
  10. "receiver": "event"
  11. "group_interval": "30s"

将审计告警发送至 Notification Manager:

  1. "receivers":
  2. - "name": "auditing"
  3. "webhook_configs":
  4. - "url": "http://notification-manager-svc.kubesphere-monitoring-system.svc:19093/api/v2/alerts"
  5. "send_resolved": false
  6. "route":
  7. "routes":
  8. - "match":
  9. "alerttype": "auditing"
  10. "receiver": "auditing"
  11. "group_interval": "30s"

备注

以上所示为默认配置。如果您不想接收某种类型的告警,可以删除相应配置。

配置接收器

Notification Manager 目前支持三种类型的接收器:电子邮件、企业微信和 Slack。只有管理员才能配置接收器。

电子邮件

如果一个名为 test-user 的租户想接收电子邮件通知,请创建一个电子邮件接收器,如下所示:

  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: v1
  3. data:
  4. password: dGVzdA==
  5. kind: Secret
  6. metadata:
  7. labels:
  8. app: notification-manager
  9. name: test-user-email-secret
  10. namespace: kubesphere-monitoring-system
  11. type: Opaque
  12. ---
  13. apiVersion: notification.kubesphere.io/v1alpha1
  14. kind: EmailConfig
  15. metadata:
  16. labels:
  17. app: notification-manager
  18. type: tenant
  19. user: test-user
  20. name: test-user-config
  21. namespace: kubesphere-monitoring-system
  22. spec:
  23. authPassword:
  24. key: password
  25. name: test-user-email-secret
  26. authUsername: abc1
  27. from: abc1@xyz.com
  28. requireTLS: true
  29. smartHost:
  30. host: imap.xyz.com
  31. port: "25"
  32. ---
  33. apiVersion: notification.kubesphere.io/v1alpha1
  34. kind: EmailReceiver
  35. metadata:
  36. labels:
  37. app: notification-manager
  38. type: tenant
  39. user: test-user
  40. name: test-user-receiver
  41. namespace: kubesphere-monitoring-system
  42. spec:
  43. emailConfigSelector:
  44. matchLabels:
  45. type: tenant
  46. user: test-user
  47. to:
  48. - abc2@xyz.com
  49. - abc3@xyz.com
  50. EOF

emailConfigSelector 是一个选择器,用于选择电子邮件接收器的 EmailConfig。如果没有设置 emailConfigSelector,接收器将使用默认电子邮件配置。您可以创建一个默认电子邮件配置,如下所示:

  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: v1
  3. data:
  4. password: dGVzdA==
  5. kind: Secret
  6. metadata:
  7. labels:
  8. app: notification-manager
  9. name: default-email-secret
  10. namespace: kubesphere-monitoring-system
  11. type: Opaque
  12. ---
  13. apiVersion: notification.kubesphere.io/v1alpha1
  14. kind: EmailConfig
  15. metadata:
  16. labels:
  17. app: notification-manager
  18. type: default
  19. name: default-email-config
  20. namespace: kubesphere-monitoring-system
  21. spec:
  22. authPassword:
  23. key: password
  24. name: default-email-secret
  25. authUsername: default
  26. from: default@xyz.com
  27. requireTLS: true
  28. smartHost:
  29. host: imap.xyz.com
  30. port: "25"
  31. EOF

带有 type: tenant 标签的电子邮件接收器只接收来自指定租户可以访问的命名空间的通知。如果您想让接收器接收来自所有命名空间的通知,或者不带命名空间标签,您可以创建一个全局电子邮件接收器,标签为 type: global,如下所示:

  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: notification.kubesphere.io/v1alpha1
  3. kind: EmailReceiver
  4. metadata:
  5. labels:
  6. app: notification-manager
  7. type: global
  8. name: global-email-receiver
  9. namespace: kubesphere-monitoring-system
  10. spec:
  11. to:
  12. - global@xyz.com
  13. EOF

备注

全局电子邮件接收器将使用默认电子邮件配置。

企业微信

Notification Manager 支持向企业微信发送通知。如果一个名为 test-user 的租户想接收企业微信通知,请创建一个微信接收器,如下所示:

  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: v1
  3. data:
  4. wechat: dGVzdA==
  5. kind: Secret
  6. metadata:
  7. labels:
  8. app: notification-manager
  9. name: test-user-wechat-secret
  10. namespace: kubesphere-monitoring-system
  11. type: Opaque
  12. ---
  13. apiVersion: notification.kubesphere.io/v1alpha1
  14. kind: WechatConfig
  15. metadata:
  16. name: test-user-config
  17. namespace: kubesphere-monitoring-system
  18. labels:
  19. app: notification-manager
  20. type: tenant
  21. user: test-user
  22. spec:
  23. wechatApiUrl: https://qyapi.weixin.qq.com/cgi-bin/
  24. wechatApiSecret:
  25. key: wechat
  26. name: test-user-wehat-secret
  27. wechatApiCorpId: wwfd76b24f06513578
  28. wechatApiAgentId: "1000002"
  29. ---
  30. apiVersion: notification.kubesphere.io/v1alpha1
  31. kind: WechatReceiver
  32. metadata:
  33. name: test-user-wechat
  34. namespace: kubesphere-monitoring-system
  35. labels:
  36. app: notification-manager
  37. type: tenant
  38. user: test-user
  39. spec:
  40. wechatConfigSelector:
  41. matchLabels:
  42. type: tenant
  43. user: test-user
  44. # optional
  45. # One of toUser, toParty, toParty should be specified.
  46. toUser: user1 | user2
  47. toParty: party1 | party2
  48. toTag: tag1 | tag2
  49. EOF

信息

  • wechatApiCorpId 即您的企业微信 ID。
  • wechatApiAgentId 即在您的企业微信中向用户发送消息的应用的 ID。
  • wechatApiSecret 即该应用的密钥。您可以在企业微信的应用管理中获取这两个参数。
  • 想接收通知的任意用户、团体或标签必须在该应用的允许用户列表中。

wechatConfigSelector 是一个选择器,用于选择微信接收器的 WechatConfig。如果没有设置 wechatConfigSelector,微信接收器将使用默认微信配置。您可以创建一个默认微信配置,如下所示:

  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: v1
  3. data:
  4. wechat: dGVzdA==
  5. kind: Secret
  6. metadata:
  7. labels:
  8. app: notification-manager
  9. name: default-wechat-secret
  10. namespace: kubesphere-monitoring-system
  11. type: Opaque
  12. ---
  13. apiVersion: notification.kubesphere.io/v1alpha1
  14. kind: WechatConfig
  15. metadata:
  16. name: default-wechat-config
  17. namespace: kubesphere-monitoring-system
  18. labels:
  19. app: notification-manager
  20. type: default
  21. spec:
  22. wechatApiUrl: https://qyapi.weixin.qq.com/cgi-bin/
  23. wechatApiSecret:
  24. key: wechat
  25. name: default-wechat-secret
  26. wechatApiCorpId: wwfd76b24f06513578
  27. wechatApiAgentId: "1000002"
  28. EOF

带有 type: tenant 标签的微信接收器只接收来自指定租户可以访问的命名空间的通知。如果您想让接收器接收来自所有命名空间的通知,或者不带命名空间标签,您可以创建一个全局微信接收器,标签为 type: global,如下所示:

  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: notification.kubesphere.io/v1alpha1
  3. kind: WechatReceiver
  4. metadata:
  5. name: global-wechat-wechat
  6. namespace: kubesphere-monitoring-system
  7. labels:
  8. app: notification-manager
  9. type: global
  10. spec:
  11. # optional
  12. # One of toUser, toParty, toParty should be specified.
  13. toUser: global
  14. toParty: global
  15. toTag: global
  16. EOF

备注

全局微信接收器将使用默认微信配置。

Slack

Notification Manager 支持向 Slack 频道发送通知。如果一个名为 test-user 的租户想接收 Slack 通知,请创建一个 Slack 接收器,如下所示:

  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: v1
  3. data:
  4. token: dGVzdA==
  5. kind: Secret
  6. metadata:
  7. labels:
  8. app: notification-manager
  9. name: test-user-slack-secret
  10. namespace: kubesphere-monitoring-system
  11. type: Opaque
  12. ---
  13. apiVersion: notification.kubesphere.io/v1alpha1
  14. kind: SlackConfig
  15. metadata:
  16. name: test-user-config
  17. namespace: kubesphere-monitoring-system
  18. labels:
  19. app: notification-manager
  20. type: tenant
  21. user: test-user
  22. spec:
  23. slackTokenSecret:
  24. key: token
  25. name: test-user-slack-secret
  26. ---
  27. apiVersion: notification.kubesphere.io/v1alpha1
  28. kind: SlackReceiver
  29. metadata:
  30. name: test-user-slack
  31. namespace: kubesphere-monitoring-system
  32. labels:
  33. app: notification-manager
  34. type: tenant
  35. user: test-user
  36. spec:
  37. slackConfigSelector:
  38. matchLabels:
  39. type: tenant
  40. user: test-user
  41. channel: alert
  42. EOF

信息

  • 当您创建 Slack 应用时,Slack 令牌为 OAuth 访问令牌或者 Bot 用户 OAuth 访问令牌。
  • 该应用必须有作用域 chat:write
  • 创建该应用的用户或者 Bot 用户必须在您想发送通知的频道中。

slackConfigSelector 是一个选择器,用于选择 Slack 接收器的 SlackConfig。如果没有设置 slackConfigSelector,Slack 接收器将使用默认 Slack 配置。您可以创建一个默认 Slack 配置,如下所示:

  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: v1
  3. data:
  4. token: dGVzdA==
  5. kind: Secret
  6. metadata:
  7. labels:
  8. app: notification-manager
  9. name: default-slack-secret
  10. namespace: kubesphere-monitoring-system
  11. type: Opaque
  12. ---
  13. apiVersion: notification.kubesphere.io/v1alpha1
  14. kind: SlackConfig
  15. metadata:
  16. name: default-slack-config
  17. namespace: kubesphere-monitoring-system
  18. labels:
  19. app: notification-manager
  20. type: default
  21. spec:
  22. slackTokenSecret:
  23. key: token
  24. name: default-slack-secret
  25. EOF

带有 type: tenant 标签的 Slack 接收器只接收来自指定租户可以访问的命名空间的通知。如果您想让接收器接收来自所有命名空间的通知,或者不带命名空间标签,您可以创建一个全局 Slack 接收器,标签为 type: global,如下所示:

  1. cat <<EOF | kubectl apply -f -
  2. apiVersion: notification.kubesphere.io/v1alpha1
  3. kind: SlackReceiver
  4. metadata:
  5. name: global-slack-slack
  6. namespace: kubesphere-monitoring-system
  7. labels:
  8. app: notification-manager
  9. type: global
  10. spec:
  11. channel: global
  12. EOF

备注

全局 Slack 接收器将使用默认 Slack 配置。