System Reboot Engineer System Reboot Engineer
首页
运维
编程

小布江

首页
运维
编程
  • Kubernetes

  • 日常

    • K8s-Minio集群迁移
    • Argocd-ingress 资源状态一直 Progressing
    • Jenkins job卡住导致页面提示Jenkins即将关闭
    • Docker构建多架构镜像
    • Minio备份及恢复
    • Jenkins构建消息webhook发送
    • cert-manager自动签发Lets Encrypt
    • Ansible批量发送密钥
    • ArgoCD 消息通知
      • Containerd配置私有Harbor镜像仓库
      • kvm虚拟机修改密码
      • Nexus
      • Nginx之tcp转发
      • Arthas
      • 开启telnet登录
      • CPU亲和
      • Harbor复制镜像
      • KVM虚拟机根目录扩容
    • Prometheus

    • Ci

    • 运维
    • 日常
    小布江
    2024-09-12
    目录

    ArgoCD 消息通知


    随着项目的增多,频繁的迭代......主要是非运维测说同步很慢,xxxxxxx,其实是服务有问题...,相比于反驳不如事实来说话,上通知!


    # 一 . 消息通知

    这里我们就以 ArgoCD Notifications 为例来说明如何使用 企业微信 来通知 Argo CD 的同步状态通知。


    # 二. ArgoCD Notifications 默认已经随着 Argo CD 安装了
    [root@k8s-uat-manager ~]# kubectl get pod -n argocd
    NAME                                                READY   STATUS    RESTARTS   AGE
    argocd-application-controller-0                     1/1     Running   0          162d
    argocd-applicationset-controller-59bf7b4f84-mtn7c   1/1     Running   0          162d
    argocd-dex-server-7788568745-9c7fc                  1/1     Running   0          121d
    argocd-notifications-controller-779c5f97bb-kf8fz    1/1     Running   0          17h
    argocd-redis-cfb94f77-vkbcs                         1/1     Running   0          121d
    argocd-repo-server-7d5c6fcfb-mfksj                  1/1     Running   0          121d
    argocd-server-6559b8c4f6-z9lcx                      1/1     Running   0          121d
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 三. 企业微信群里创建一个机器人,注:企业微信的markdown格式不支持@所有人

    image-20240912093002371

    # 四. 修改argocd-notifications-cm,官网 (opens new window)
    [root@k8s-uat-manager ~]# cat argocd-notifications-cm.yaml
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: argocd-notifications-cm
      namespace: argocd
    data:
      service.webhook.qywx: |
        url: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=06a0xxxx # 企业微信的webhook地址
        headers:
          - name: Content-Type
            value: application/json
      context: |
        argocdUrl: http://agrocd.xxx.com # argocd的ui地址
      template.app-sync-change: |
        webhook:
          qywx:
            method: POST
            body: |
              {
                  "msgtype": "markdown",
                  "markdown": {
                      "content": "
                      ### ArgoCD服务发版成功
                      > <font color=\"info\">项目名称</font>: {{.app.metadata.name}}
                      > <font color=\"info\">app同步状态</font>: {{.app.status.operationState.phase}}
                      > <font color=\"info\">app服务状态</font>: {{.app.status.health.status}}
                      > <font color=\"info\">发布人</font>: {{(call .repo.GetCommitMetadata .app.status.sync.revision).Author}}
                      > <font color=\"info\">提交信息</font>: {{(call .repo.GetCommitMetadata .app.status.sync.revision).Message}}
                      > <font color=\"info\">时间</font>: {{ (call .time.Parse .app.status.operationState.startedAt).Local.Format "2006-01-02 15:04:05" }}
                      > <font color=\"info\">URL</font>: [点击跳转ArgoCD]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true)"
                  }
              }
      template.app-sync-degraded: |
        webhook:
          qywx:
            method: POST
            body: |
              {
                  "msgtype": "markdown",
                  "markdown": {
                      "content": "
                      ### ArgoCD服务发版失败
                      > <font color=\"warning\">服务名称</font>: {{.app.metadata.name}}
                      > <font color=\"warning\">app同步状态</font>: {{.app.status.operationState.phase}}
                      > <font color=\"warning\">app服务状态</font>: {{.app.status.health.status}}
                      > <font color=\"info\">时间</font>: {{ (call .time.Parse .app.status.operationState.startedAt).Local.Format "2006-01-02 15:04:05" }}
                      > <font color=\"warning\">URL</font>: [点击跳转ArgoCD]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true)"
                  }
              }
      trigger.on-deployed: |
        - description: Application is synced and healthy. Triggered once per commit.
          send: [app-sync-change]
          when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
      trigger.on-health-degraded: |
        - description: Application has degraded
          send: [app-sync-degraded]
          when: app.status.health.status == 'Degraded'
      trigger.on-sync-failed: |
        - description: Application syncing has failed
          send: [app-sync-degraded]
          when: app.status.operationState.phase in ['Error', 'Failed']
      trigger.on-sync-status-unknown: |
        - description: Application status is 'Unknown'
          send: [app-sync-degraded]
          when: app.status.sync.status == 'Unknown'
      subscriptions: |
        - recipients: [qywx]  # 可能有bug,正常应该是webhook:qywx
          triggers: [on-deployed, on-sync-failed, on-health-degraded,on-sync-status-unknown      
    ----------------------------------------------------------------------------------------------------------------
    该触发器定义包括名称、条件和通知模板引用:
    send:表示通知内容使用的模板名称
    description:当前触发器的描述信息
    when:条件表达式,如果应发送通知,则返回 true
    ----------------------------------------------------------------------------------------------------------------
    该模板用于生成通知内容,该模板利用 Golang 中的 html/template 包定义,允许定义通知标题和正文,可以重用,并且可以由多个触发器引用。每个模板默认都可以访问以下字段:
    
    app:保存应用程序对象
    context:是用户定义的字符串映射,可能包含任何字符串键和值,这里用于拼接argocd的web UI地址
    notificationType 保留通知服务类型名称,该字段可用于有条件地呈现服务特定字段
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    # 五. 修改argocd-notifications-controller时区,webhook的时间是根据argocd-notifications-controller来取
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: argocd-notifications-controller
    spec:
      template:
        spec:
          containers:
          - name: argocd-notifications-controller
            env:
            - name: TZ
              Values:Asia/Shanghai
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    六. 更新资源文件,重启并查看有无异常,参考 (opens new window)

    [root@k8s-uat-manager ~]# kubectl apply -f argocd-notifications-cm.yaml
    [root@k8s-uat-manager ~]# kubectl delete pod -n argocd argocd-notifications-controller-779c5f97bb-kf8fz
    [root@k8s-uat-manager ~]# kubectl logs -f -n argocd argocd-notifications-controller-779c5f97bb-8nbc8
    
    1
    2
    3

    image-20240912094720109

    #argocd
    上次更新: 2025/04/25, 03:40:17
    Ansible批量发送密钥
    Containerd配置私有Harbor镜像仓库

    ← Ansible批量发送密钥 Containerd配置私有Harbor镜像仓库→

    最近更新
    01
    Harbor复制镜像
    04-15
    02
    CPU亲和
    04-10
    03
    开启telnet登录
    04-09
    更多文章>
    Theme by Vdoing
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式