Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Kubernetes, how can I scale a Deployment to zero when idle

I'm running a fairly resource-intensive service on a Kubernetes cluster to support CI activities. Only a single replica is needed, but it uses a lot of resources (16 cpu), and it's only needed during work hours generally (weekdays, 8am-6pm roughly). My cluster runs in a cloud and is setup with instance autoscaling, so if this service is scaled to zero, that instance can be terminated.

The service is third-party code that cannot be modified (well, not easily). It's a fairly typical HTTP service other than that its work is fairly CPU intensive.

What options exist to automatically scale this Deployment down to zero when idle?

I'd rather not setup a schedule to scale it up/down during working hours because occasionally CI activities are performed outside of the normal hours. I'd like the scaling to be dynamic (for example, scale to zero when idle for >30 minutes, or scale to one when an incoming connection arrives).

like image 533
Patrick Avatar asked May 04 '20 16:05

Patrick


People also ask

How do you scale to zero in Kubernetes?

Scaling down to zero will stop your application. You can run kubectl scale --replicas=0, which will remove all the containers across the selected objects.

How do you get zero downtime in Kubernetes deployment?

Rolling Update policy Rolling updates allow deployments to take place with zero downtime by incrementally updating Pods instances with new ones. In association with Health Checks, it is possible to deploy a new version of the application without any downtime.

How do you scale deployment?

Scaling is accomplished by changing the number of replicas in a deployment. A replica is a copy of a pod that already contains a running service. By having multiple replicas of a pod, you can ensure that your deployment has the available resources to handle increasing load.

How do I scale a Kubernetes deployment using kubectl?

From the Kubernetes command line, you set the scale value by using the replicas parameter. kubectl scale --replicas=2 deployment/odm-instance-odm-decisionrunner Where odm-instance-odm-decisionrunner is the name of the deployment you want to scale. It is also possible to specify one or more preconditions for the scale action.

How do I scale to zero in Kubernetes?

Scale to Zero With KEDA KEDA is a Kubernetes-based Event-Driven Autoscaler. With KEDA, you can drive the scaling of any application in Kubernetes based on the number of events needing to be processed. KEDA acts as an agent to achieve the scale to zero capability by activating and deactivating deployments.

What is Horizontal Pod autoscaler in Kubernetes?

Kubernetes’ Horizontal Pod Autoscaler (HPA) automatically scales the application workload by scaling the number of Pods in deployment (or replication controller, replica set, stateful set), based on observed metrics like CPU utilization, memory consumption, or with custom metrics provided by the application.

How do I create a Kubernetes deployment?

Before you begin, make sure your Kubernetes cluster is up and running. Create the Deployment by running the following command: Note: You may specify the –record flag to write the command executed in the resource annotation kubernetes.io/change-cause. Run kubectl get deployments to check if the Deployment was created.


Video Answer


4 Answers

I ended up implementing a custom solution: https://github.com/greenkeytech/zero-pod-autoscaler

Compared to Knative, it's more of a "toy" project, fairly small, and has no dependency on istio. It's been working well for my use case, though I do not recommend others use it without being willing to adopt the code as your own.

like image 100
Patrick Avatar answered Oct 20 '22 09:10

Patrick


Actually Kubernetes supports the scaling to zero only by means of an API call, since the Horizontal Pod Autoscaler does support scaling down to 1 replica only.

Anyway there are a few Operator which allow you to overtake that limitation by intercepting the requests coming to your pods or by inspecting some metrics.

You can take a look at Knative or Keda. They enable your application to be serverless and they do so in different ways.

Knative, by means of Istio intercept the requests and if there's an active pod serving them, it redirects the incoming request to that one, otherwise it trigger a scaling.

By contrast, Keda best fits event-driven architecture, because it is able to inspect predefined metrics, such as lag, queue lenght or custom metrics (collected from Prometheus, for example) and trigger the scaling.

Both support scale to zero in case predefined conditions are met in a equally predefined window.

Hope it helped.

like image 36
amenic Avatar answered Oct 20 '22 07:10

amenic


There are a few ways this can be achieved, possibly the most "native" way is using Knative with Istio. Kubernetes by default allows you to scale to zero, however you need something that can broker the scale-up events based on an "input event", essentially something that supports an event driven architecture.

You can take a look at the offcial documents here: https://knative.dev/docs/serving/configuring-autoscaling/

like image 5
Tom.Bastianello Avatar answered Oct 20 '22 08:10

Tom.Bastianello


The horizontal pod autoscaler currently doesn’t allow setting the minReplicas field to 0, so the autoscaler will never scale down to zero, even if the pods aren’t doing anything. Allowing the number of pods to be scaled down to zero can dramatically increase the utilization of your hardware.

When you run services that get requests only once every few hours or even days, it doesn’t make sense to have them running all the time, eating up resources that could be used by other pods.

But you still want to have those services available immediately when a client request comes in.

This is known as idling and un-idling. It allows pods that provide a certain service to be scaled down to zero. When a new request comes in, the request is blocked until the pod is brought up and then the request is finally forwarded to the pod.

Kubernetes currently doesn’t provide this feature yet, but it will eventually.

like image 3
omricoco Avatar answered Oct 20 '22 08:10

omricoco