# Context
Based on the following documentation: [Kubernetes Event-driven Autoscaling (KEDA) (Preview) - Azure Kubernetes Service](https://learn.microsoft.com/en-us/azure/aks/keda-about) and [KEDA | Prometheus](https://keda.sh/docs/2.10/scalers/prometheus/), we can utilize Prometheus and KEDA to scale our services based on network metrics.
# Prometheus
## Setup
To set up a Prometheus server that provides metrics, we can use Helm with the predefined values from community chart. You can customize the configuration by editing `values.yaml`. Some common changes when applying to other environments include:
- **Ingress Configuration**: Change the hostname of Prometheus and AlertManager along with any required ingress annotations.
- **Persistence Configuration**: Adjust this if you need to change the capacity of the data volume for the Prometheus server.
- **AlertManager Configuration**: Modify `alertmanagerFiles` to edit notification details or other settings.
[Additional Resources:](https://learn.microsoft.com/en-us/azure/aks/keda-about)
- [Azure KEDA Documentation](https://learn.microsoft.com/en-us/azure/aks/keda-about)
- [KEDA Prometheus Scalers Documentation](https://keda.sh/docs/2.10/scalers/prometheus/)
Using Helm to provision a new Prometheus server:
```bash
# Add the community repository
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
# Update the local repositories
helm repo update
# Install the Prometheus server
helm upgrade --install prometheus prometheus-community -f values.yaml --version 15.10.4 -n monitoring
```
## Network Metrics
Prometheus provides several network traffic-related metrics, including:
- container_network_receive_bytes_total
- container_network_receive_packets_total
- container_network_transmit_bytes_total
- container_network_transmit_packets_total
To monitor network traffic during high traffic times, we can use the following query:
```
avg(delta((container_network_receive_bytes_total{pod!="", pod=~"<pod_name>.*"}[5m]))) / 1024^3
```
Explanation:
- `container_network_receive_bytes_total{pod!="", pod=~"<pod_name>.*"}[5m]`: Retrieves the total network bytes received by the service in the last 5 minutes.
- `delta()`: Calculates the difference between the last and first value to determine the total received network bytes within the 5-minute window.
- `avg()`: Averages the value across multiple replicas for better scaling.
- `/ 1024^3`: Converts the result to gigabytes (GB).
# KEDA
## Setup
To enable the KEDA plugin, follow the [official Azure documentation](https://learn.microsoft.com/en-us/azure/aks/keda-deploy-add-on-cli). This process requires a user with high permissions in AKS.
### Create ScaledObjects for Auto Scaling Service
After gathering the necessary metrics from Prometheus, we can create new KEDA `ScaledObjects` based on the defined query. Below is a sample `ScaledObject` YAML file:
```yaml
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: prometheus-scaledobject
namespace: default
spec:
scaleTargetRef:
name: my-deployment # The deployment name
triggers:
- type: prometheus
metadata:
serverAddress: https://<prometheus-host>:<prometheus-port>
metricName: container_network_receive_packets_total
threshold: '1' # Scaling occurs if the result exceeds 1 GB
query: avg(delta((container_network_receive_bytes_total{pod!="", pod=~"<pod_name>.*"}[5m]))) / 1024^3
```
To apply this ScaledObject, run the following command:
```bash
kubectl apply -f scaledobject.yaml
```
Once applied, the new ScaledObject will be monitored and scaled automatically by KEDA's Custom Resource Definitions (CRD). To inspect the details, use:
```bash
kubectl get scaledobject
kubectl describe scaledobject prometheus-scaledobject
```
Note: If the Prometheus server requires authentication, you must define the credentials according to KEDA's Prometheus documentation.
# Pricing
In a production environment, Prometheus metrics costs are based on the number of samples ingested and processed for queries. Metrics ingestion costs include 18 months of data retention. Billing for Prometheus metrics is not yet enabled.
KEDA is an Azure Kubernetes Service (AKS) add-on, and there are no additional charges for using it.