Automating Database Updates on Azure Kubernetes Service (AKS) with CronJobs: A Step-by-Step Guide

NHAILA Achraf
2 min readFeb 19, 2024

--

In this step-by-step guide, we’ll walk through the process of setting up CronJobs on AKS to automate database updates.

cronjob aks

Introduction: Automating routine database updates is crucial for ensuring data integrity and system reliability. By utilizing CronJobs on Azure Kubernetes Service (AKS), we can streamline this process, reducing manual intervention and potential errors.

Step 1: Setting Up AKS
Before diving into automation, ensure you have an AKS cluster provisioned. If you haven’t done this yet, follow Microsoft’s official documentation to create your cluster

Step 2 : Adding Necessary Libraries and Parameters
Before proceeding, make sure you have all the required libraries and parameters configured. This step ensures smooth execution of database update tasks within the AKS environment.

Step 3: Defining the CronJob
Now, let’s define the CronJob responsible for orchestrating our database updates. Below is the YAML configuration (cronjob.yaml) detailing the CronJob specifications:

apiVersion: batch/v1
kind: CronJob
metadata:
name: cronmysql
spec:
schedule: "1 * * * *"
jobTemplate:
metadata:
creationTimestamp: null
spec:
activeDeadlineSeconds: 1800
backoffLimit: 0
template:
metadata:
labels:
app: cronmysql
spec:
containers:
- name: cronmysql
image: ubuntu:22.04
imagePullPolicy: Always
command: ["/bin/bash"]
args:
- -c
- |
apt-get update
apt -y install mysql-client wget -y
wget --no-check-certificate https://dl.cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem
echo "UPDATE table set status = 0 WHERE is_active = 1 " | mysql -h {{ .Values.database.ENDPOINT }} -u {{ .Values.database.USER }} -p{{ .Values.database.PASSWORD }} -A --ssl-mode=REQUIRED --ssl-ca=DigiCertGlobalRootCA.crt.pem {{ .Values.database.DATABASE }} && echo "Query executed successfully" || echo "Query failed to execute"

restartPolicy: Never
concurrencyPolicy: Forbid
startingDeadlineSeconds: 240
failedJobsHistoryLimit: 1
successfulJobsHistoryLimit: 1

Step 4: Deploy CronJob to AKS
With the CronJob configuration defined, deploy it to your AKS cluster using the following command:

kubectl apply -f cronjob.yaml

Step 5: Verify Deployment
After deploying the CronJob, verify its status and monitor execution using Kubernetes commands:

kubectl get cronjobs
kubectl logs pod-name

Conclusion:
By following this guide, you’ve successfully set up a CronJob on AKS to automate MySQL database updates. This automation reduces manual effort and ensures timely maintenance, enhancing overall system reliability and efficiency.

i hope this will help ,If you have any further questions or run into issues, don’t hesitate to reach out.

Photo by Aziz Acharki on Unsplash

NHAILA Achraf

#devsecops #devops #cluster #datadog #azure #monitoring #kubernetes #aks #cronjobs

--

--