我们使用的是 AKS v1.26.6。我们部署的代码是基于 .Net Core 6/7 的。
我们创建了一个 cronjob 并部署在 AKS 中。该作业在晚上按固定时间表运行。到目前为止一切都很好,现在我们需要知道此作业执行是否成功,我的意思是我们需要一个事件通知,以便我们可以根据该事件通知进一步工作。
此功能开箱即用可用于 AKS Cron 作业吗?
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>
要在现有 AKS 群集上启用监视:
az aks enable-addons \
--addons monitoring \
--name myAKSCluster \
--resource-group myResourceGroup \
--workspace-resource-id <Log-Analytics-workspace-resource-id>
部署 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
为 CronJob 完成事件创建 KQL 查询
KubeEvents
| where TimeGenerated > ago(1d)
| where Namespace == "default" and Name contains "hello"
| where Reason == "SuccessfulCreate"
| project TimeGenerated, Name, Message
查询准备就绪后,您可以在 Azure Monitor 中设置警报:
参考资料: