DEV Community

Rudney eduardo souza vieira
Rudney eduardo souza vieira

Posted on • Updated on

Keda in EKS

Keda on EKS allows for unparalleled scaling flexibility, where its easy configuration and strengths regarding infrastructure architecture will be demonstrated in this article

When to use Keda ?

  • Your AutoScalling cant handle event driven cenarios
  • Need to handle varying levels of traffic
  • Need to cara about costs in the EKS cluster

What problem Keda will solve in this case cenario ?

  • Event Driven requests to start new pods in eks
  • Manage to save costs in stand by pods

How to install keda in eks:

We can download it from here and apply it with:

kubectl apply -f keda-2.13.0-core.yaml
Enter fullscreen mode Exit fullscreen mode

or just execute it like this

kubectl apply --server-side -f https://github.com/kedacore/keda/releases/download/v2.13.0/keda-2.13.0-core.yaml
Enter fullscreen mode Exit fullscreen mode

Making it work in EKS:

Then when it is installed we can go and make an sample Python App

import json
from fastapi import FastAPI
import boto3

#Sample Python App for getting queue messages

app = FastAPI()

def getQueue(queue: str):
    sqs = boto3.resource('sqs', region_name='')
    # Get the queue
    print(queue)
    queue = sqs.get_queue_by_name(QueueName='')
    QMessage = []
    for message in queue.receive_messages():
        QMessage.append(message.body)
        message.delete()
    return QMessage

@app.get("/getQueue")
def root(queue: str):
    returnList = getQueue(queue)
    return {"queue Message list": returnList}

@app.get("/ping")
def ping():
    return {"status": "ok"}
Enter fullscreen mode Exit fullscreen mode

Now lets make an Deployment file for it:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-app
  namespace: keda
spec:
  selector:
    matchLabels:
      app: python-app
  replicas: 1
  template:
    metadata:
      labels:
        app: python-app
    spec:
      serviceAccount: keda-operator
      containers:
        - name: python-app
          image: ecr_url
          command: ["uvicorn"]
          args :  ["app.main:app", "--host", "0.0.0.0", "--port", "80"]
          resources:
            limits:
              cpu: "1"
              memory: "2Gi"
            requests:
              cpu: "1"
              memory: "2Gi"
          ports:
            - containerPort: 80


Enter fullscreen mode Exit fullscreen mode

Then add the Service Account for permissions:

apiVersion: v1
kind: ServiceAccount
automountServiceAccountToken: true
metadata:
  labels:
    app.kubernetes.io/name: keda-operator
    app.kubernetes.io/part-of: keda-operator
    app.kubernetes.io/version: main
  name: keda-operator
  namespace: keda
  annotations:
   eks.amazonaws.com/role-arn:  arn:aws:iam::accountId:role/KedaSqsExecutionRole
Enter fullscreen mode Exit fullscreen mode

And then add the scaled object for the SQS Queue and the Auth Trigger:

apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
  name: keda-trigger-auth-aws-credentials
  namespace: keda
spec:
  podIdentity:
    provider: aws-eks
---
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: aws-sqs-queue-scaledobject
  namespace: keda
spec:
  minReplicaCount: 0  #-> min pods that will be when there is smt in queue
  maxReplicaCount: 5 #-> max pods
  pollingInterval: 10 
  cooldownPeriod:  25 
  scaleTargetRef:
    name: python-app
  triggers:
  - type: aws-sqs-queue
    authenticationRef:
      name: keda-trigger-auth-aws-credentials
    metadata:
      queueURL: sqs_queue_url
      queueLength: "1"
      awsRegion: "aws_regiion"
      identityOwner: operator
Enter fullscreen mode Exit fullscreen mode

Conclusion

Keda gives a lot of flexibility when event driven is needed, because of it various built in scalers, like in this article with AWS SQS, and could scale in various other cenarios, as example, with AWS DynamoDB, granting a cost friendly way to handle EKS needs to scale with your business.

Top comments (0)