基于 Azure Kubernetes 的 cronjob 完成事件

问题描述 投票:0回答:1

我们使用的是 AKS v1.26.6。我们部署的代码是基于 .Net Core 6/7 的。

我们创建了一个 cronjob 并部署在 AKS 中。该作业在晚上按固定时间表运行。到目前为止一切都很好,现在我们需要知道此作业执行是否成功,我的意思是我们需要一个事件通知,以便我们可以根据该事件通知进一步工作。

此功能开箱即用可用于 AKS Cron 作业吗?

azure kubernetes azure-aks kubernetes-cronjob k8s-cronjobber
1个回答
0
投票

AKS Cron 作业没有在完成后发送通知的内置功能。但是,你可以使用 Azure Monitor 设置自动通知。 Azure Monitor 可以监视 AKS 群集并根据特定事件模式发送警报。您可以配置警报以根据 Cron 作业的完成情况触发。要设置 Azure Monitor 以根据 AKS 中 Kubernetes CronJobs 的完成情况触发警报,首先,请确保为 AKS 群集启用了适用于容器的 Azure Monitor。

要在 AKS 创建期间启用监视:

az aks create \
  --resource-group myResourceGroup \
  --name myAKSCluster \
  --generate-ssh-keys \
  --enable-addons monitoring \
  --workspace-resource-id <Log-Analytics-workspace-resource-id>

enter image description here

要在现有 AKS 群集上启用监视:

az aks enable-addons \
  --addons monitoring \
  --name myAKSCluster \
  --resource-group myResourceGroup \
  --workspace-resource-id <Log-Analytics-workspace-resource-id>

enter image description here

部署 CronJob

示例

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

enter image description here

为 CronJob 完成事件创建 KQL 查询

KubeEvents
| where TimeGenerated > ago(1d)
| where Namespace == "default" and Name contains "hello"
| where Reason == "SuccessfulCreate"
| project TimeGenerated, Name, Message

查询准备就绪后,您可以在 Azure Monitor 中设置警报:

  1. 导航至 监控 > 警报 > + 新警报规则
  2. 选择存储集群日志的Log Analytics 工作区
  3. 条件下,选择添加条件。使用自定义日志搜索并输入您的 KQL 查询。
  4. 定义警报逻辑(例如,每当事件计数大于 0 时)。
  5. 操作组下,定义您希望如何收到通知(电子邮件、短信、Webhook 等)。
  6. 设置警报详细信息,例如名称、严重性和描述。
  7. 单击创建警报规则

enter image description here enter image description here

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.