<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Raghu Manne on Medium]]></title>
        <description><![CDATA[Stories by Raghu Manne on Medium]]></description>
        <link>https://medium.com/@raghumanne?source=rss-fc070458da9a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6mLB07rWxN2R5DnxzySjPg.jpeg</url>
            <title>Stories by Raghu Manne on Medium</title>
            <link>https://medium.com/@raghumanne?source=rss-fc070458da9a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 17:49:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raghumanne/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[KEDA; autoscale Kubernetes workloads the way you want!]]></title>
            <link>https://raghumanne.medium.com/keda-autoscale-kubernetes-workloads-the-way-you-want-2b1779ef7b89?source=rss-fc070458da9a------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b1779ef7b89</guid>
            <dc:creator><![CDATA[Raghu Manne]]></dc:creator>
            <pubDate>Mon, 11 Mar 2024 11:28:05 GMT</pubDate>
            <atom:updated>2025-04-06T14:31:40.324Z</atom:updated>
            <content:encoded><![CDATA[<h3>Autoscaling in Kubernetes:</h3><p>Before getting into KEDA, let’s get a brief of autoscaling in Kubernetes in general. If your application workloads start receiving more traffic then there is a need to scale up resources to meet the certain demand and scale down while the traffic is low.</p><p>Note: In this article we will only discuss autoscaling in Kubernetes with respect to workloads but not cluster autoscaler which will add more compute resources to the cluster to accommodate all the workloads. More about cluster autoscaler <a href="https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/README.md">here</a>.</p><p>When we talk about autoscaling of workloads in Kubernetes we have two components</p><ol><li>Horizontal Pod Autoscaler (HPA):</li></ol><p>HPA adjusts the number of pods backed by controllers such as deployments and Statefulsets depending upon the certain metrics. The common metrics are CPU and Memory usage of all the pods of that particular controller.</p><p>2. Vertical Pod Autoscaler (VPA):</p><p>In contrast to HPA; VPA allocates more resources i.e., CPU and Memory to existing pods to meet the traffic demand.</p><p>What if you want to scale based on other metrics? <a href="https://keda.sh/">KEDA</a> enters!</p><h3>What is KUBERNETES EVENT DRIVEN AUTOSCALING (KEDA)?</h3><p>As the name reads, KEDA is a lightweight controller which autoscales the Kubernetes workloads responding to certain events. Uses Native Horizontal Pod Autoscaler to feed external metrics coming out of the Kubernetes cluster. It will autoscale native Kubernetes workloads such as Deployments, Statefulsets, Jobs and it can also autoscales Custom Resource Definitions(CRD).</p><p>KEDA is a <a href="https://landscape.cncf.io/?item=orchestration-management--scheduling-orchestration--keda">graduated</a> project under CNCF!</p><p>Authenticates well with managed Kubernetes services like GKE(Workload Identity), EKS(Pod Identity) etc., for retrieving metrics from event sources.</p><p>Though you can autoscale based on custom metrics with custom metrics adapter implementations of certain Kubernetes service providers; many of these providers, such as <a href="https://github.com/Azure/azure-k8s-metrics-adapter">AKS</a> and <a href="https://github.com/amazon-archives/k8s-cloudwatch-adapter">EKS</a>, have stopped developing custom metrics adapters because projects like KEDA serve these use cases and offer more features.</p><p>KEDA didn’t reinvent the wheel, it just passes the events from event source to the native Kubernetes horizontal pod autoscaler (HPA). But it takes the burden of authentication to the event sources.</p><p>If you are trying to use KEDA with managed Kubernetes services like GKE, EKS, AKS etc., you can leverage their native identity authentication methods like GCP Workload Identity, Azure Workload identity to talk to their services and retrieve metrics from them.</p><h3>Architecture:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xbCBP0Z8med-Zx65" /></figure><p><strong>Admission Webhook:</strong> To avoid misconfiguration scenarios like multiple HPA for a single targeted resource.</p><h3>Why KEDA?</h3><p>You can scale native Kubernetes workloads such as Deployments and Statefulsets; can scale Kubernetes jobs. It can even scale Custom Resources.</p><p>You don’t need to install multiple custom metric adaptors into your cluster, you can use multiple KEDA scalers that can be used to autoscale your workloads.</p><h3>What are Scalers?</h3><p>Scalers define where to get metrics from; or you can say like event sources depending upon which you want to scale your workloads.</p><p>We have more than 60+ scalers ranging from different sources</p><ol><li>Messaging queues such as pub-sub, RabbitMQ, NATS, AWS SQS, Azure Service Bus etc.,</li><li>Monitoring solutions such as GCP Stack driver(Monitoring),AWS CloudWatch,Prometheus,New Relic etc.,</li><li>Databases such as MySQL,MSSQL, PostgreSQL, Redis,AWS DynamoDB etc.,</li><li>Storage solutions such as GCS, Azure Blob storage etc.,</li></ol><p>And more..</p><h3>Components of KEDA</h3><p>All the components of KEDA are CRD’s that get installed when you install the KEDA setup onto your cluster.</p><ol><li>ScaledObject: Specification that defines how you scale your Deployments and Statefulsets responding to your Events.</li><li>ScaledJob: Specification that defines how you scale your Jobs(run to completion workloads).</li><li>TriggerAuthentication: Specifies the authentication methods to retrieve events from event sources.</li><li>ClusterTriggerAuthentication: Trigger Authentication is limited to a namespace, if you want to refer it across namespaces we can use this resource.</li></ol><h3>Demo:</h3><p>Let’s try out KEDA using a sample GCP pubsub scaler that scale our workloads responding to unacknowledged messages in the PubSub queue.</p><h4>Prerequisites:</h4><ul><li>Access to GCP Project where you can create a GKE cluster and PubSub topic and subscriptions and IAM service account and role binding to the service account</li><li>Client tools <a href="https://cloud.google.com/sdk/docs/install">gcloud</a>, <a href="https://kubernetes.io/docs/tasks/tools/#kubectl">kubectl</a>, <a href="https://helm.sh/docs/intro/install/">helm</a> installed</li></ul><h4>Creating a GKE cluster with Workload Identity Enabled:</h4><pre>PROJECT_ID=$(gcloud config get-value project)<br>gcloud container clusters create keda-demo-cluster - workload-pool=$PROJECT_ID.svc.id.goog - workload-metadata=GKE_METADATA - machine-type=e2-medium - max-nodes=2 - zone us-central1-a</pre><p>Make sure workload identity is enabled; we use workload identity to authenticate to the pub-sub resources.</p><h4>Installing KEDA:</h4><p>We can deploy KEDA to our cluster using Helm chart</p><pre>helm repo add kedacore https://kedacore.github.io/charts<br>helm repo update<br>helm install keda kedacore/keda - namespace keda - create-namespace</pre><p>All the components of keda will be deployed to a keda namespace.</p><h4>Creating IAM Service Account and bindings:</h4><pre>SERVICE_ACCOUNT_NAME=keda-gcppubsubtest<br>PROJECT_ID=$(gcloud config get-value project)<br>SERVICE_ACCOUNT_FULL_NAME=$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com<br><br>gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME - display-name &quot;KEDA PubSub S&quot;<br>gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:$SERVICE_ACCOUNT_FULL_NAME --role roles/monitoring.viewer<br>gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:$SERVICE_ACCOUNT_FULL_NAME --role roles/pubsub.subscriber</pre><h4>Binding GCP IAM Service Account and k8s service account:</h4><p>We need to authenticate the keda operator to be able to retrieve metrics from Pub-Sub, for this we will be using GKE Workload Identity setup. Wait until the cluster is up and running and run below commands.</p><pre>gcloud iam service-accounts add-iam-policy-binding $SERVICE_ACCOUNT_FULL_NAME \<br>--role roles/iam.workloadIdentityUser --member &quot;serviceAccount:$PROJECT_ID.svc.id.goog[keda/keda-operator]&quot;<br><br>kubectl annotate serviceaccount keda-operator --namespace keda \<br>iam.gke.io/gcp-service-account=$SERVICE_ACCOUNT_FULL_NAME<br><br>#Access to consume pubsub messages from GKE Workloads<br>gcloud iam service-accounts add-iam-policy-binding $SERVICE_ACCOUNT_FULL_NAME \<br>--role roles/iam.workloadIdentityUser \<br>--member &quot;serviceAccount:$PROJECT_ID.svc.id.goog[default/default]&quot;<br><br>kubectl annotate serviceaccount default --namespace default \<br>iam.gke.io/gcp-service-account=$SERVICE_ACCOUNT_FULL_NAME</pre><h4>Creating PubSub topic and subscription:</h4><pre>SUBSCRIPTION_NAME=keda-test-subscription<br>TOPIC_NAME=keda-test-topic<br><br>gcloud pubsub topics create $TOPIC_NAME<br>gcloud pubsub subscriptions create $SUBSCRIPTION_NAME --topic $TOPIC_NAME<br>gcloud pubsub subscriptions add-iam-policy-binding $SUBSCRIPTION_NAME \<br>--member=serviceAccount:$SERVICE_ACCOUNT_FULL_NAME --role=roles/pubsub.subscriber</pre><p>Let’s deploy a sample deployment that consumes messages from pubsub along keda resources triggerauthentication and scaledobject</p><pre>raghu_manne@cloudshell:~$ k apply -f https://raw.githubusercontent.com/raghu-manne/keda-blog/main/manifests/deploy.yaml<br>scaledobject.keda.sh/pubsub-test created<br>triggerauthentication.keda.sh/pubsub-test created<br>deployment.apps/consume-pubsub-messages created<br>raghu_manne@cloudshell:~$ kubectl set env deployment consume-pubsub-messages PROJECT_ID=YOUR-ACTUAL-GCP-PROJECT-ID<br>#Note: Set your actual GCP Project ID in the above command</pre><p>Let’s see the deployment</p><pre>raghu_manne@cloudshell:~$ k get deployments.apps<br>NAME                    READY UP-TO-DATE AVAILABLE  AGE<br>consume-pubsub-messages 0/0       0           0     67s</pre><p>If you see the deployment, the replicas are zero despite we mentioned the replica count as 1.</p><p>Reason being the KEDA scales down the pod count to zero if there are no messages to be consumed in the pub sub queue.</p><pre>raghu_manne@cloudshell:~$ k describe scaledobjects.keda.sh pubsub-test<br>Name: pubsub-test<br>Namespace: default<br>Labels: scaledobject.keda.sh/name=pubsub-test<br>…<br>Events:<br>Type Reason Age From Message<br> - - - - - - - - - - - - -<br>Normal KEDAScalersStarted 4m34s keda-operator Scaler gcp-pubsub is built.<br>Normal KEDAScalersStarted 4m34s keda-operator Started scalers watch<br>Normal ScaledObjectReady 4m34s keda-operator ScaledObject is ready for scaling<br>Normal KEDAScaleTargetDeactivated 4m34s keda-operator Deactivated apps/v1.Deployment default/consume-pubsub-messages from 1 to 0</pre><p>We will now publish some messages to the pub-sub.</p><pre>raghu_manne@cloudshell:~ $ TOPIC_NAME=keda-test-topic<br>raghu_manne@cloudshell:~ $ for x in {1..20}; do gcloud beta pubsub topics publish $TOPIC_NAME --message &quot;Test Message ${x}&quot;; done<br>messageIds:<br>- &#39;10436458032317676&#39;<br>messageIds:<br>- &#39;10437016284640303&#39;<br>messageIds:<br>- &#39;10436245226109532&#39;<br>messageIds:<br>- &#39;10436726361647818&#39;<br>messageIds:<br>- &#39;10438699104065498&#39;<br>messageIds:<br>- &#39;10435599104878680&#39;<br>messageIds:<br>- &#39;10437114777032952&#39;<br>messageIds:<br>- &#39;10437054531865816&#39;<br>.<br>.<br>.</pre><p>When we start publishing messages to the pub-sub queue, keda will notice that there are unacknowledged messages in the queue and will pass the same information to the HPA to scale the workloads. If you observe the architecture of KEDA scaling down to zero replicas and to one is the responsibility of the KEDA operator. After one replica the HPA will take over, because HPA doesn’t have capability to scale down to zero.</p><pre>raghu_manne@cloudshell:~$ k describe scaledobjects.keda.sh pubsub-test <br>Name:         pubsub-test<br>Namespace:    default<br>...<br>Events:<br>  Type    Reason                      Age                From           Message<br>  ----    ------                      ----               ----           -------<br>  Normal  KEDAScaleTargetActivated    22m                keda-operator  Scaled apps/v1.Deployment default/consume-pubsub-messages from 0 to 1</pre><p>HPA now will scale up the replicas to consume all the messages in the queue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mWsBM2oNwXf3XH6N9FLscw.png" /><figcaption>kubectl describe hpa keda-hpa-pubsub-test</figcaption></figure><p>Once there are no messages left in the queue then HPA will eventually scale down the replicas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AnlI9m1yeZxidV_HDNJy8Q.png" /><figcaption>kubectl describe hpa keda-hpa-pubsub-test</figcaption></figure><p>And then ScaledObject kicks in and scales down the remaining one pod to zero.</p><p>All the commands and scripts used can be found <a href="https://github.com/raghu-manne/keda-blog/tree/main">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b1779ef7b89" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>