From c1d31ca465890b1358d97a4e96d77bfec445baa9 Mon Sep 17 00:00:00 2001 From: Mat Moore Date: Tue, 3 Sep 2024 16:42:55 +0100 Subject: [PATCH 1/2] feat(k8s): add a horizontal pod autoscaler 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 --- .../workflows/reusable-push-and-deploy.yml | 1 + deployments/templates/deployment.yml | 1 - deployments/templates/hpa.yml | 19 +++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 deployments/templates/hpa.yml diff --git a/.github/workflows/reusable-push-and-deploy.yml b/.github/workflows/reusable-push-and-deploy.yml index bac59a2a..c3a9f281 100644 --- a/.github/workflows/reusable-push-and-deploy.yml +++ b/.github/workflows/reusable-push-and-deploy.yml @@ -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 diff --git a/deployments/templates/deployment.yml b/deployments/templates/deployment.yml index 24324925..9e2f3a25 100644 --- a/deployments/templates/deployment.yml +++ b/deployments/templates/deployment.yml @@ -5,7 +5,6 @@ metadata: labels: app: find-moj-data spec: - replicas: 2 strategy: type: RollingUpdate rollingUpdate: diff --git a/deployments/templates/hpa.yml b/deployments/templates/hpa.yml new file mode 100644 index 00000000..7715dbed --- /dev/null +++ b/deployments/templates/hpa.yml @@ -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 From 93760b0ed8c73bfccdbf8f682442a6b2d96f99b1 Mon Sep 17 00:00:00 2001 From: Mat Moore Date: Wed, 4 Sep 2024 11:03:44 +0100 Subject: [PATCH 2/2] add resources to deployment --- deployments/templates/deployment.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/deployments/templates/deployment.yml b/deployments/templates/deployment.yml index 9e2f3a25..8367eccd 100644 --- a/deployments/templates/deployment.yml +++ b/deployments/templates/deployment.yml @@ -21,6 +21,13 @@ spec: containers: - name: find-moj-data image: ${IMAGE_PATH} + resources: + requests: + cpu: 20m + memory: 300Mi + limits: + cpu: 1000m + memory: 1000Mi ports: - name: http containerPort: 8000