<p>I have a HorizontalPodAutoscalar to scale my pods based on CPU. The minReplicas here is set to <code>5</code>:</p>
<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: myapp-web
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myapp-web
  minReplicas: 5 
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 50

</code></pre>
<p>I've then added Cron jobs to scale up/down my horizontal pod autoscaler based on time of day:</p>
<pre><code>kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: production
  name: cron-runner
rules:
- apiGroups: [&quot;autoscaling&quot;]
  resources: [&quot;horizontalpodautoscalers&quot;]
  verbs: [&quot;patch&quot;, &quot;get&quot;]

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: cron-runner
  namespace: production
subjects:
- kind: ServiceAccount
  name: sa-cron-runner
  namespace: production
roleRef:
  kind: Role
  name: cron-runner
  apiGroup: rbac.authorization.k8s.io

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: sa-cron-runner
  namespace: production
---

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: django-scale-up-job
  namespace: production
spec:
  schedule: &quot;56 11 * * 1-6&quot;
  successfulJobsHistoryLimit: 0 # Remove after successful completion
  failedJobsHistoryLimit: 1 # Retain failed so that we see it
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: sa-cron-runner
          containers:
          - name: django-scale-up-job
            image: bitnami/kubectl:latest
            command:
            - /bin/sh
            - -c
            - kubectl patch hpa myapp-web --patch '{&quot;spec&quot;:{&quot;minReplicas&quot;:8}}'
          restartPolicy: OnFailure
----
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: django-scale-down-job
  namespace: production
spec:
  schedule: &quot;30 20 * * 1-6&quot;
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 0 # Remove after successful completion
  failedJobsHistoryLimit: 1 # Retain failed so that we see it
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: sa-cron-runner
          containers:
          - name: django-scale-down-job
            image: bitnami/kubectl:latest
            command:
            - /bin/sh
            - -c
            - kubectl patch hpa myapp-web --patch '{&quot;spec&quot;:{&quot;minReplicas&quot;:5}}'
          restartPolicy: OnFailure
</code></pre>
<p>This works really well, except that now when I deploy it overwrites this <code>minReplicas</code> value with the minReplicas in the HorizontalPodAutoscaler spec (in my case, this is set to 5)</p>
<p>I'm deploying my HPA using <code>kubectl apply -f ~/autoscale.yaml</code></p>
<p>Is there a way of handling this situation? Do I need to create some kind of shared logic so that my deployment scripts can work out what the minReplicas value should be? Or is there a simpler way of handling this?</p>

<p>I think you could also consider the following two options:</p>
<hr />
<h3>Use helm to manage the life-cycle of your application with lookup function:</h3>
<p>The main idea behind this solution is to query the state of specific cluster resource (here <code>HPA</code>) before trying to create/recreate it with <code>helm</code> <code>install</code>/<code>upgrade</code> commands.</p>
<ul>
<li><em><a href="https://helm.sh/docs/chart_template_guide/functions_and_pipelines/#using-the-lookup-function" rel="nofollow noreferrer">Helm.sh: Docs: Chart template guide: Functions and pipelines: Using the lookup function</a></em></li>
</ul>
<p>I mean to check the current <code>minReplicas</code> value each time before you upgrade your application stack.</p>
<hr />
<h3>Manage the <code>HPA</code> resource separately to application manifest files</h3>
<p>Here you can handover this task to a dedicated <code>HPA</code> operator, which can coexist with your <code>CronJobs</code> that adjust <code>minReplicas</code> according specific schedule:</p>
<ul>
<li><em><a href="https://banzaicloud.com/blog/k8s-hpa-operator/" rel="nofollow noreferrer">Banzaicloud.com: Blog: K8S HPA Operator</a></em></li>
</ul>
