Skip to content

Commit

Permalink
Added support for autoscaling based on custom metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianeicher committed Jul 3, 2022
1 parent da874c3 commit 9a5a7af
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
6 changes: 5 additions & 1 deletion charts/generic-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ app:
| `replicas` | `1` | The number of instances of the service to run (set at least `2` for Pod Disruption Budget) |
| `autoscaling.enabled` | `false` | Enables automatic starting of additional instances based on CPU load |
| `autoscaling.maxReplicas` | `3` | The maximum number of instances to run (must be larger than `replicas`) |
| `autoscaling.targetCpu` | `50` | The desired average CPU load in percent |
| `autoscaling.metric.type` | `Resource` | The type of metric to use for scaling (`Resource`, `Pods`, `Object` or `External`) |
| `autoscaling.metric.name` | `cpu` | The name of the metric to use for scaling |
| `autoscaling.metric.object` | req. if `type` = `Object` | Reference to the Kubernetes object the metric describes |
| `autoscaling.metric.selector` | `{}` | Labels for selecting the metric as a key-value map |
| `autoscaling.targetValue` | `80` | The desired value of the metric to achieve through scaling (e.g., CPU utilization in percent) |
| `autoscaling.behavior` | `{}` | Scaling behavior configuration (see `HorizontalPodAutoscalerBehavior`) |
| `nodeSelector` | `{}` | Node labels required for scheduling this service, also used as tolerations |
| `persistence.enabled` | `false` | Enables persistent storage for the service |
Expand Down
13 changes: 13 additions & 0 deletions charts/generic-service/ci/autoscaling-external-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Autoscaling external metric test

image:
repository: jwilder/whoami
tag: latest

autoscaling:
enabled: true
metric:
type: External
name: some_metric
selector:
key: value
21 changes: 18 additions & 3 deletions charts/generic-service/templates/autoscaler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,26 @@ spec:
maxReplicas: {{ .Values.autoscaling.maxReplicas }}
behavior: {{ .Values.autoscaling.behavior | toYaml | nindent 4 }}
metrics:
- type: Resource
- type: {{ .Values.autoscaling.metric.type }}
{{- if eq .Values.autoscaling.metric.type "Resource" }}
resource:
name: cpu
name: {{ .Values.autoscaling.metric.name }}
target:
type: Utilization
averageUtilization: {{ .Values.autoscaling.targetCpu }}
averageUtilization: {{ .Values.autoscaling.targetCpu | default .Values.autoscaling.targetValue }}
{{- else }}
{{ .Values.autoscaling.metric.type | lower }}:
metric:
name: {{ .Values.autoscaling.metric.name }}
{{- if eq .Values.autoscaling.metric.type "Object" }}
describedObject: {{ .Values.autoscaling.metric.object | required "autoscaling.metric.object required when autoscaling.metric.type is Object" | toYaml | nindent 10 }}
{{- else if eq .Values.autoscaling.metric.type "External" }}
selector:
matchLabels: {{ .Values.autoscaling.metric.selector | toYaml | nindent 14 }}
{{- end }}
target:
type: Value
value: {{ .Values.autoscaling.targetValue }}
{{- end }}

{{- end }}
35 changes: 33 additions & 2 deletions charts/generic-service/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,41 @@
"default": 3,
"description": "The maximum number of instances to run (must be larger than replicas)"
},
"targetCpu": {
"metric": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["Resource", "Pods", "Object", "External"],
"default": "Resource",
"description": "The type of metric to use for scaling"
},
"name": {
"type": "string",
"default": "cpu",
"description": "The name of the metric to use for scaling"
},
"object": {
"type": ["null", "object"],
"properties": {
"apiVersion": {"type": "string"},
"kind": {"type": "string"},
"name": {"type": "string"}
},
"required": ["apiVersion", "kind", "name"],
"description": "Reference to the Kubernetes object the metric describes (required if type = Object)"
},
"selector": {
"type": "object",
"additionalProperties": {"type": "string"},
"description": "Labels for selecting the metric"
}
}
},
"targetValue": {
"type": "integer",
"default": 80,
"description": "The desired average CPU load in percent"
"description": "The desired value of the metric to achieve through scaling (e.g., CPU utilization in percent)"
},
"behavior": {
"type": "object",
Expand Down
7 changes: 6 additions & 1 deletion charts/generic-service/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ replicas: 1
autoscaling:
enabled: false
maxReplicas: 3
targetCpu: 80
metric:
type: Resource
name: cpu
object:
selector: {}
targetValue: 80
behavior: {}

nodeSelector: {}
Expand Down

0 comments on commit 9a5a7af

Please sign in to comment.