How to scale based on specified time in Kubernetes ( keda cronjob )

How to scale based on specified time in Kubernetes ( keda cronjob )

here is the question : How can we Scale an APP based on time on kubernetes ?

the Solution isssssss : KEDA ( Kubernetes Event Driven Autoscale )


What is the scenario ?

In this scenario, we are going to scale an NGINX deployment to 10 replicas only between 6 and 8 AM.

What is KEDA ?

KEDA (Kubernetes-based Event Driven Autoscaler) is an open-source project that enables Kubernetes to scale workloads based on the number of events needing to be processed. It allows you to create event-driven applications on Kubernetes by scaling your deployments based on custom metrics and external event sources like message queues, databases, cloud services, and more ..


What is the scenario ?

In this scenario, we are going to scale an NGINX deployment to 10 replicas only between 6 and 8 AM.

1- Install keda

# install keda 
kubectl apply   --force-conflicts  --server-side -f https://github.com/kedacore/keda/releases/download/v2.12.0/keda-2.12.0.yaml

# check keda's pods
kubectl get pods -n keda        


2- Create a deployment that you want to scale

# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80        

3- Create object scale rule

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: time-based-scaler-packops
  namespace: default
spec:
  scaleTargetRef:
    name: nginx-deployment
    kind: Deployment
    apiVersion: apps/v1
  minReplicaCount: 1
  maxReplicaCount: 10
  cooldownPeriod: 30
  triggers:
  - type: cron
    metadata:
      # Required
      timezone: Asia/Tehran  # The acceptable values would be a value from the IANA Time Zone Database.
      start: 00 6 * * *        # At 6:00 AM
      end: 00 8 * * *         # At 8:00 PM
      desiredReplicas: "10"
        

4- Verify the Result

on specified time you can run some command in order to check the result

kuebctl get scaledobject
kuebctl get hpa 
kuebctl get pods        

Conclusion:

Using KEDA allows you to define custom metrics to scale your application based on metrics from sources like Redis, RabbitMQ, and more. This scenario specifically involved using KEDA's cron mode for time-based scaling. I hope this was helpful!


I made a GitHub repository based on the CKA exam. Don't forget to give me a star! :)

https://github.com/farshadnick/Mastering-Kubernetes/


About Author?:

Hi ??, I’m Farshad Nick (Farshad nickfetrat)



Muhamed Sadeq Rakhshani Moqadam

DevOps Engineer at Utravs.com | IT Professional with 12+ Years of Experience in Network & System Administration | IT Infrastructure Specialist

7 个月

Informative ? ?

khashayar panbeian

Site Reliability Engineer at Snapp! Box

7 个月

Interesting ??

Alireza Akrami

Linux system admin |DevOps engineer | Docker | Kubernetes

7 个月

Nice ??????

要查看或添加评论,请登录

Farshad Nick (????? ??? ????)的更多文章