Skip to content

Commit

Permalink
feat(k8s): add a horizontal pod autoscaler
Browse files Browse the repository at this point in the history
this scales up when the average CPU utilization is at 95% of the
requested CPU.

We should remove the replicas key from the deployment, otherwise any time
we deploy it will overwrite the HPA (source:
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#migrating-deployments-and-statefulsets-to-horizontal-autoscaling)

Behaviour:

- every 15 seconds - The HPA controller checks the value of the metric used every 15 seconds per pod.
- every 3 minutes - The HPA scales up pods if the metric threshold has been continually exceeded for 3 mins.
- every 5 minutes - The HPA scales down pods if the metric threshold has not been exceeded for 5 mins.

Further info:

- https://user-guide.cloud-platform.service.justice.gov.uk/documentation/concepts/deploying.html#horizontal-pod-autoscaling-hpa
- https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale
  • Loading branch information
MatMoore committed Sep 4, 2024
1 parent dcce071 commit c1d31ca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/reusable-push-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ jobs:

run: |
cat deployments/templates/deployment.yml | envsubst > deployments/deployment.yml
cat deployments/templates/hpa.yml | envsubst > deployments/hpa.yml
cat deployments/templates/ingress.yml | envsubst > deployments/ingress.yml
cat deployments/templates/service.yml | envsubst > deployments/service.yml
cat deployments/templates/secrets.yml | envsubst > deployments/secrets.yml
Expand Down
1 change: 0 additions & 1 deletion deployments/templates/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ metadata:
labels:
app: find-moj-data
spec:
replicas: 2
strategy:
type: RollingUpdate
rollingUpdate:
Expand Down
19 changes: 19 additions & 0 deletions deployments/templates/hpa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: find-moj-data
namespace: ${NAMESPACE}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: find-moj-data-deployment
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 95

0 comments on commit c1d31ca

Please sign in to comment.