Sobes.tech
Middle — Middle+

How can you dynamically manage the number of containers in Kubernetes depending on the system load?

sobes.tech AI

Answer from AI

In Kubernetes, the Horizontal Pod Autoscaler (HPA) is used for dynamic management of the number of containers (pod replicas) based on load. HPA automatically scales the number of pods in a Deployment, ReplicaSet, or StatefulSet based on metrics such as CPU load, memory, or custom metrics.

An example of HPA configuration that scales the number of pods based on CPU load:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: example-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: example-deployment
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

Here, HPA maintains an average CPU load of 50%, increasing or decreasing the number of pods within the range of 1 to 10.

You can also use the Vertical Pod Autoscaler (VPA) to modify pod resources, but for scaling the number of replicas, HPA is the most suitable.

How can you dynamically manage the number of… - sobes.tech