Warning
This page was translated from the original Japanese version by PLaMo Translate. The Japanese version is authoritative; the English translation may contain inaccuracies.
Automatic Horizontal Scaling of Workloads and Jobs Based on External Events
PFCP provides functionality for automatically scaling workloads and jobs horizontally using external event-driven triggers, utilizing KEDA (Kubernetes Event Driven Autoscaler).
Available KEDA Resources
The following three KEDA resources are supported for use with PFCP:
ScaledObject
The ScaledObject resource defines both the target workload to be scaled (e.g., Deployment, StatefulSet) and the triggering mechanism that controls the scaling process.
For detailed information, please refer to the official ScaledObject documentation.
ScaledJob
The ScaledJob resource defines the target job to be scaled and the triggering mechanism that controls its scaling.
For detailed information, please refer to the official ScaledJob documentation.
TriggerAuthentication
The TriggerAuthentication resource manages authentication credentials for external systems.
For detailed information, please refer to the official TriggerAuthentication documentation.
Scaling Using PFCP Prometheus Metrics
PFCP offers Prometheus-based metrics monitoring functionality. Below we explain how to use this feature to automatically scale the number of Pod replicas for a Deployment.
The following is an example KEDA configuration for scaling an application based on Prometheus metrics. It creates a ScaledObject resource targeting a Deployment named myapp. Since the Prometheus query always returns 100 with a threshold of 50, the Pod replica count will be scaled to 2.
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: app-scaler
spec:
scaleTargetRef:
name: myapp # Specifies the name of the Deployment to be scaled
maxReplicaCount: 3 # Specifies the maximum number of replicas
minReplicaCount: 0 # Specifies the minimum number of replicas
triggers:
- type: prometheus
metadata:
serverAddress: http://prometheus-k8s.monitoring-org-<org-name>.svc.cluster.local.:9090 # org-name: Organization name
query: "vector(100)" # Specifies the metric to monitor (here, a query that always returns 100)
threshold: "50" # Specifies the scaling condition (here, when the metric exceeds 50)
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: stefanprodan/podinfo
name: podinfo
ports:
- containerPort: 9898
For instructions on collecting Pod metrics with Prometheus, please refer to Metrics Monitoring and Alerting.
Scaling Using AWS Managed Prometheus Metrics
This section provides an example of automatically scaling the number of Pod replicas for a Deployment by utilizing AWS’s Managed Prometheus (AMP) metrics as an external resource.
AWS IAM Configuration
To use AMP metrics, KEDA must be configured to query AMP. The following steps outline the required configuration:
- Create an OpenID Connect (OIDC) identity provider in IAM by following the instructions in Configuring Identity Federation with Public Clouds.
- Note the ARN of your AMP workspace in use.
For AMP setup instructions, please refer to the AWS documentation.
- Create a policy that allows KEDA to query AMP metrics. Replace
<aws_region>and<aws_account_id>with your actual AMP workspace ARN.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "aps:QueryMetrics" ], "Resource": [ "arn:aws:aps:<aws_region>:<aws_account_id>:workspace/<workspace_id>" ] } ] } - Create an IAM role to attach this policy to and associate it with the KEDA system components. Set the
Principalto the OIDC provider created in step 1. The Namespace for the KEDA Operator in theConditionvaries by organization. Below is an example:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "sts:AssumeRoleWithWebIdentity", "Principal": { "Federated": "arn:aws:iam::<aws_account_id>:oidc-provider/token.<pfcp_cluster>.kubernetes.pfcomputing.com" }, "Condition": { "StringEquals": { "token.<pfcp_cluster>.kubernetes.pfcomputing.com:aud": [ "sts.amazonaws.com" ], "token.<pfcp_cluster>.kubernetes.pfcomputing.com:sub": [ "system:serviceaccount:keda-org-<pfcp_org_name>:keda-operator" ] } } } ] } - Note the ARN of the created role. This ARN will be used later in KEDA configuration.
Creating KEDA Resources
The following is an example KEDA configuration for scaling an application based on AMP metrics.
By specifying the ARN of the IAM role created earlier in .spec.podIdentity.roleArn for TriggerAuthentication, KEDA will use that role to query AMP metrics.
apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
name: amp-query-role
spec:
podIdentity:
provider: aws
roleArn: <roleArn> # Specifies the ARN of the IAM role created earlier
---
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: app-scaler
spec:
scaleTargetRef:
name: myapp
maxReplicaCount: 10
minReplicaCount: 0
triggers:
- type: prometheus
authenticationRef:
name: amp-query-role
metadata:
awsRegion: <aws_region> # Specifies the AMP region
serverAddress: https://aps-workspaces.<aws_region>.amazonaws.com/workspaces/<workspace_id> # Specifies the AMP endpoint
query: "sum(rate(http_requests_total[1m]))" # Specifies the metric to monitor (here, HTTP requests per minute)
threshold: "50" # Specifies the scaling condition (here, when the request count exceeds 50)
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: stefanprodan/podinfo
name: podinfo
ports:
- containerPort: 9898