Class HorizontalPodAutoscaler

  • All Implemented Interfaces:
    IApiEndpoint, IApiResource, IResource, software.amazon.jsii.JsiiSerializable, software.constructs.IConstruct, software.constructs.IDependable

    @Generated(value="jsii-pacmak/1.74.0 (build 6d08790)",
               date="2023-02-04T02:31:04.858Z")
    @Stability(Stable)
    public class HorizontalPodAutoscaler
    extends Resource
    A HorizontalPodAutoscaler scales a workload up or down in response to a metric change.

    This allows your services to scale up when demand is high and scale down when they are no longer needed.

    Typical use cases for HorizontalPodAutoscaler:

    • When Memory usage is above 70%, scale up the number of replicas to meet the demand.
    • When CPU usage is below 30%, scale down the number of replicas to save resources.
    • When a service is experiencing a spike in traffic, scale up the number of replicas to meet the demand. Then, when the traffic subsides, scale down the number of replicas to save resources.

    The autoscaler uses the following algorithm to determine the number of replicas to scale:

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

    HorizontalPodAutoscaler's can be used to with any Scalable workload:

    • Deployment
    • StatefulSet

    Targets that already have a replica count defined:

    Remove any replica counts from the target resource before associating with a HorizontalPodAutoscaler. If this isn't done, then any time a change to that object is applied, Kubernetes will scale the current number of Pods to the value of the target.replicas key. This may not be desired and could lead to unexpected behavior.

    Example:

     const backend = new kplus.Deployment(this, 'Backend', ...);
     const hpa = new kplus.HorizontalPodAutoscaler(chart, 'Hpa', {
      target: backend,
      maxReplicas: 10,
      scaleUp: {
        policies: [
          {
            replicas: kplus.Replicas.absolute(3),
            duration: Duration.minutes(5),
          },
        ],
      },
     });
     

    See Also:
    https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#implicit-maintenance-mode-deactivation
    • Constructor Detail

      • HorizontalPodAutoscaler

        protected HorizontalPodAutoscaler​(software.amazon.jsii.JsiiObjectRef objRef)
      • HorizontalPodAutoscaler

        protected HorizontalPodAutoscaler​(software.amazon.jsii.JsiiObject.InitializationMode initializationMode)
      • HorizontalPodAutoscaler

        @Stability(Stable)
        public HorizontalPodAutoscaler​(@NotNull
                                       software.constructs.Construct scope,
                                       @NotNull
                                       String id,
                                       @NotNull
                                       HorizontalPodAutoscalerProps props)
        Parameters:
        scope - This parameter is required.
        id - This parameter is required.
        props - This parameter is required.
    • Method Detail

      • getMaxReplicas

        @Stability(Stable)
        @NotNull
        public Number getMaxReplicas()
        The maximum number of replicas that can be scaled up to.
      • getMinReplicas

        @Stability(Stable)
        @NotNull
        public Number getMinReplicas()
        The minimum number of replicas that can be scaled down to.
      • getScaleDown

        @Stability(Stable)
        @NotNull
        public ScalingRules getScaleDown()
        The scaling behavior when scaling down.
      • getScaleUp

        @Stability(Stable)
        @NotNull
        public ScalingRules getScaleUp()
        The scaling behavior when scaling up.
      • getTarget

        @Stability(Stable)
        @NotNull
        public IScalable getTarget()
        The workload to scale up or down.
      • getMetrics

        @Stability(Stable)
        @Nullable
        public List<Metric> getMetrics()
        The metric conditions that trigger a scale up or scale down.