> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Automatic Scaling of Apinizer Environments

> You can configure horizontal auto-scaling operations for pods, optimize resource usage by automatically increasing and decreasing pod count according to application demands, and configure scaling policies and thresholds.

This document explains how to configure horizontal auto-scaling **(horizontal auto-scaling)** operations for pods.

This horizontal scaling feature optimizes resource usage by automatically increasing and decreasing pod count according to application demands.

To use the scaling feature in your Kubernetes cluster, a metric-server must be installed. If it is not present, you can [click here](/en/setup/kubernetes/kubernetes-metric-server) for installation.

## Configuration

Create yaml file:

```bash theme={null}
sudo vi hpa-autoscaling.yaml
```

<Tip>
  Edit `NAMESPACE` and `DEPLOYMENT_NAME` fields in yaml according to your application.
</Tip>

```yaml theme={null}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: apinizer-hpa
  namespace: <NAMESPACE>
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: <DEPLOYMENT_NAME>
  minReplicas: 1
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 70
  behavior:
    scaleDown:
      selectPolicy: Max
      policies:
        - type: Pods
          value: 1
          periodSeconds: 60
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
        - type: Pods
          value: 1
          periodSeconds: 60
      selectPolicy: Max
```

Apply yaml file:

```bash theme={null}
kubectl apply -f manager-hpa-autoscaling.yaml
```

## Configuration Parameters

| Field                | Description                                                                                                                                                                                                    |
| -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `scaleTargetRef`     | This field specifies which deployment scaling will work for. Example: manager.                                                                                                                                 |
| `minReplicas`        | Minimum number of pods that should exist. Example: 2.                                                                                                                                                          |
| `maxReplicas`        | Maximum number of pods that can exist in the system. Example: 10.                                                                                                                                              |
| `averageUtilization` | In this field, you can enter a percentage value for cpu and memory usage. It will create pods when it exceeds the specified level.                                                                             |
| `scaleDown`          | When it falls below the target usage level, it will reduce pods by the given value amount every 60 seconds from the pods it newly created.                                                                     |
| `scaleUp`            | When it exceeds the target usage level, it is checked for 60 seconds, which is the **stabilizationWindowSeconds** value specified. If the condition is met, one pod is added to your cluster every 60 seconds. |

<Tip>
  To disable scaleDown feature, change it to **"selectPolicy: Disabled"**.
</Tip>
