azurekubernetesazure-akskubernetes-cronjobk8s-cronjobber

Azure Kubernetes based cronjob completion event


We are using AKS v1.26.6. Our deployed code is .Net Core 6/7 based.

We created a cronjob and deployed in AKS. This job runs at fixed schedule in evening. All is good so far, now we need to know IF this job execution was success, i mean we need an event notification so we can further work based on that event notification.

Does this feature out of box available for AKS Cron jobs?


Solution

  • AKS Cron jobs don't have a built-in feature to send notifications upon completion. However, you can set up automated notifications using Azure Monitor. Azure Monitor can monitor your AKS cluster and send alerts based on specific event patterns. You can configure alerts to trigger based on the completion of your Cron job. To set up Azure Monitor to trigger alerts based on the completion of Kubernetes CronJobs in AKS, First, ensure that Azure Monitor for containers is enabled for your AKS cluster.

    To enable monitoring during AKS creation:

    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

    To enable monitoring on an existing AKS cluster:

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

    enter image description here

    Deploy a CronJob

    example

    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

    Create KQL Query for CronJob Completion Events

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

    With the query ready, you can set up an alert in Azure Monitor:

    1. Navigate to Monitor > Alerts > + New alert rule.
    2. Select the Log Analytics workspace where your cluster’s logs are stored.
    3. Under Condition, select Add condition. Use the custom log search and input your KQL query.
    4. Define the alert logic (e.g., whenever the count of events is greater than 0).
    5. Under Action Group, define how you want to be notified (email, SMS, webhook, etc.).
    6. Set the alert details such as name, severity, and description.
    7. Click Create alert rule.

    enter image description here enter image description here

    References: