K8s monitor resource utilization then create container automatically

Tried to find a solution for a project, not sure can K8s do that by itself or need a third-party tool to get involved.

For example, I have 3 Nginx containers to provide load balance for our web service. I would like to let K8s monitor CPU/Memory/Network Traffic usage of the Pod and set up the metric. Once the utilization over than metric the master will create more Nginx automatically.

I am very new to K8s, any help is appreciated!


Solution 1:

What you are looking for is the Horizontal Pod Autoscaler:

The Horizontal Pod Autoscaler automatically scales the number of Pods in a replication controller, deployment, replica set or stateful set based on observed CPU utilization (or, with custom metrics support, on some other application-provided metrics).

The docs above will provide you with info regarding the idea behind that mechanism and the ways of implementing it.

If you would like to scale based on metrics other than just CPU usage than you need to use the custom metrics:

Kubernetes 1.6 adds support for making use of custom metrics in the Horizontal Pod Autoscaler. You can add custom metrics for the Horizontal Pod Autoscaler to use in the autoscaling/v2beta2 API. Kubernetes then queries the new custom metrics API to fetch the values of the appropriate custom metrics.

Below you will find some useful sources with guides and examples:

  • kubectl autoscale

  • Horizontal Pod Autoscaler Walkthrough

  • Autoscaling on multiple metrics and custom metrics

  • Autoscaling on metrics not related to Kubernetes objects