The time-triggered Azure Function allows us to schedule time for executing the function. It means that it triggers the function on a specified time. It works as CRON expressions work. When creating a time-triggered function, we need to specify a time in CRON format that determines when the trigger will execute. The CRON expression consists of six parts - second, minute, hour, day, month, and day of the week in Azure Function and each part is separated by space. Unlike HTTP-triggered Azure functions, timing info is stored in an Azure storage account so for any trigger functions you need to create an Azure storage account, for more information about creating Azure storage account see this Link
mkdir TimerTrigger
cd TimerTrigger
func init . --docker
Select dotnet
func new
Select TimerTrigger then select a name for your function.
func start
for deploy keda on your kubernetes cluster the yaml file would be like this (see complete file):
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: timer-trigger-function-with-keda
namespace: keda-namespace
spec:
scaleTargetRef:
name: timer-trigger-function-with-keda
minReplicaCount: 0
maxReplicaCount: 20
triggers:
- type: cron
metadata:
timezone: Asia/Tehran #Required
start: 0 0 * * * #Required
end: 59 23 * * * #Required
desiredReplicas: "10" #Required
there are some important things in KEDA to be explain
kind: ScaledObject
tells kubernetes that this service is for keda and use for auto scallingscaleTargetRef
consider that KEDA should trigger which service that run on kubernetes for detecting auto scalling when service was busy and needs more pods (in this case we first create deployment namedtimer-trigger-function-with-keda
)triggers
consider type of scaled object, there several trigger types, for Timer Trigger function use Crontimezone
One of the acceptable values from the IANA Time Zone Database. The list of timezones can be found herestart
Cron expression indicating the start of the cron schedule.end
Cron expression indicating the end of the cron schedule.desiredReplicas
Number of replicas to which the resource has to be scaled between the start and end of the cron schedule.
Notice: Start and end should not be same.
For example, the following schedule is not valid:
start: 30 * * * *
end: 30 * * * *
Having all that configured we can now deploy the YAML file to AKS.
kubectl apply -f .\timer-trigger-deploy.yml
To check if everything is working, check the deployment.
> kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
timer-trigger-function-with-keda 0/0 0 0 20h
> kubectl get ScaledObject
NAME SCALETARGETKIND SCALETARGETNAME TRIGGERS AUTHENTICATION READY ACTIVE AGE
timer-trigger-function-with-keda apps/v1.Deployment timer-trigger-function-with-keda cron True False 20h