# K8s HPA (horizontal pod autoscaling)

**URL:** <https://discuss.openedx.org/t/k8s-hpa-horizontal-pod-autoscaling/7762>\
**Category:** Site Operations Help\
**Tags:** tutor, k8s, hpa\
**Created:** [July 8, 2022, 9:47am UTC](https://discuss.openedx.org/t/k8s-hpa-horizontal-pod-autoscaling/7762 "2022-07-08T09:47:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andres.Aulasneo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/andres.aulasneo/32/984_2.png) [@Andres.Aulasneo](https://discuss.openedx.org/u/Andres.Aulasneo)\
**Post date:** [July 8, 2022, 9:47am UTC](https://discuss.openedx.org/t/k8s-hpa-horizontal-pod-autoscaling/7762/1 "2022-07-08T09:47:37Z")

</div>

Hi all,

I am trying to implement [HPA](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-metrics-apis) in K8s.

I have set hpa with

```auto
kubectl autoscale deployment lms --cpu-percent=80 --min=1 --max=10

```

But it doesn’t work:

```auto
% kubectl describe hpa -n capnet-prod
Name: lms
Namespace: <my namespace>
Labels: <none>
Annotations: <none>
CreationTimestamp: Fri, 08 Jul 2022 05:44:12 -0300
Reference: Deployment/lms
Metrics: ( current / target )
  resource cpu on pods (as a percentage of request): <unknown> / 80%
Min replicas: 1
Max replicas: 10
Deployment pods: 1 current / 0 desired
Conditions:
  Type Status Reason Message
  ---- ------ ------ -------
  AbleToScale True SucceededGetScale the HPA controller was able to get the target's current scale
  ScalingActive False FailedGetResourceMetric the HPA was unable to compute the replica count: failed to get cpu utilization: missing request for cpu

```

The reason seems to be that the [pod specification](https://github.com/overhangio/tutor/blob/9a63dc70ceb2044ceb2027025914ab1fe7a5a025/tutor/templates/k8s/deployments.yml#L197-L199) does not include a request for cpu.

My questions are:

- Is it possible to add a cpu request using a plugin? I don’t see a patch there to modify the deployment.
- Which would be a good request for CPU? 500m sounds good?

If the answer to the first one is no, I would raise a PR with the following change to the lms deployment to something like:

```auto
apiVersion: apps/v1
kind: Deployment
metadata:
  name: lms
  labels:
    app.kubernetes.io/name: lms
spec:
  template:
    spec:
      containers:
        - name: lms
...
          resources:
            requests:
              memory: 2Gi
{% if LMS_CPU_REQUEST is defined %}
              cpu: {{ LMS_CPU_REQUEST }}
{% endif %}

```

Similarly it can be repeated for cms and the workers.

What do you think?

---

<div class="post-metadata">

**Author:** ![MoisesGonzalezS](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/moisesgonzalezs/32/1996_2.png) [@MoisesGonzalezS](https://discuss.openedx.org/u/MoisesGonzalezS)\
**Post date:** [July 8, 2022, 1:15pm UTC](https://discuss.openedx.org/t/k8s-hpa-horizontal-pod-autoscaling/7762/2 "2022-07-08T13:15:19Z")

</div>

A [PR](https://github.com/overhangio/tutor/pull/686) that adds the patch `k8s-override` was merged recently. Now you can leverage the [patching functionality](https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/#customizing) of kustomize to modify your deployments.

The folks at OpenCraft are [using it](https://gitlab.com/opencraft/dev/tutor-contrib-grove/-/blob/d531bf4b96cd3ac1637563710da5326fc97dce87/tutorgrove/patches/k8s-override) to implement HPA.

---

<div class="post-metadata">

**Author:** ![Andres.Aulasneo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/andres.aulasneo/32/984_2.png) [@Andres.Aulasneo](https://discuss.openedx.org/u/Andres.Aulasneo)\
**Post date:** [September 7, 2022, 8:51pm UTC](https://discuss.openedx.org/t/k8s-hpa-horizontal-pod-autoscaling/7762/3 "2022-09-07T20:51:50Z")

</div>

Thanks @MoisesGonzalezS for your tips!

Based on all this information, I’ve created a simple plugin [tutor-contrib-hpa](http://github.com/aulasneo/tutor-contrib-hpa) to implement HPA. It’s highly inspired in OpenCraft’s [Grove](https://gitlab.com/opencraft/dev/tutor-contrib-grove/-/tree/d531bf4b96cd3ac1637563710da5326fc97dce87), but limited to only implement HPA.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/openedx/original/2X/e/e0015f89e879bc75925b71fd11133ab15fe98297.png) [@system](https://discuss.openedx.org/u/system)\
**Post date:** [December 6, 2022, 8:52pm UTC](https://discuss.openedx.org/t/k8s-hpa-horizontal-pod-autoscaling/7762/4 "2022-12-06T20:52:32Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
