How to autoscale an app instance with Kubernetes based on just own custom objects in own db (user, time)?

12/17/2019

I have read the custom metrics possibilities with Kubernetes, but it is not still clear to me how I can autoscale the Kubernetes pods with Docker image in it based on the custom objects in my postgres database.

Example: - Instantiate a new app instance if the current app instance reaches X users (a user is an object in psql) - Instantiate a new app instance if the current app instance reaches Y minuts (a time object in psql) - Terminate an app instance if no users are there - All app instances can be instantiated in the same VM (Windows Data center 2016)

Who can provide steps to reach the above?

-- JapanX Team
docker
kubernetes
windows

1 Answer

12/17/2019

In Kubernetes, autoscaling a pod is taken care by Horizontal Pod Autoscaling by default, it works out based on the following formula

desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]

Usually it is based on CPU Utilisation metrics, eg: currentMetricValue = 200m and desiredMetricValue = 100m K8s scale your application to 2 pods.

These can be defined in your deployment, here is an example on how horizontal pods auto scaler works. Horizontal auto scaler

But from V2Beta2 a newer api is introduced which allows a user to specify custom metrics for the Horizontal pod autoscaler to use.

You can either use these two apis to learn and build custom metrics based on your db queries.

Custom metrics api

External metrics api

However, there are few external metrics api already exists which might helpful for you to write the adapter for your needs. Existing custom metrics apis

Hope this is helpful.

-- BinaryMonster
Source: StackOverflow