<p>I am using Google Kubernetes Engine and some of my deployments are more important than others (For example, the staging environment can run on less replicas or even stop if needed).</p>
<p>I want to dynamically change the amount of replicas of a deployment depending on the available resources, especially memory.</p>
<p>Ideally, I would set a default number of replicas which is used normally and if the cluster is running low on resources, it should reduce the number of replicas of that deployment.</p>
<p>This should happen for some deployments but not all of them.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: my-deployment
  name: my-deployment
  namespace: my-namespace
spec:
  replicas: 3 # This should be lower if the cluster is running on low resources (memory or CPU)
  selector:
    matchLabels:
      app: my-deployment
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-deployment
    spec:
      containers:
      - image: my/image:version
        name: my-deployment
</code></pre>
<p>It should even be possible to reduce the number of replicas down to 0 for some deployments but not for others.</p>
<p>Note that my deployments are distributed across multiple namespaces (if that matters).</p>

dan1st
<blockquote>
<p>I want to dynamically change the amount of replicas of a deployment
depending on the available resources, especially memory.</p>
</blockquote>
<p>You can use the Kubernetes HPA (Horizonatal POD autoscaling), which which dynamically changes the number of replicas based on the <strong>CPU/Memory</strong> <strong>utilization</strong>.</p>
<p>While you can also set the default number of replicas for your deployment for <strong>deployment</strong> you have <strong>1 scaling to 3</strong> for <strong>staging</strong> you have a minimum <strong>3 running</strong> scaling up to <strong>5</strong> and for <strong>Prod</strong> minimum running you <strong>4</strong> and <strong>scaling</strong> to <strong>10</strong> etc.</p>
<p>Read more about the HPA : <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/</a></p>
<p><strong>Example</strong></p>
<pre><code>kubectl autoscale deployment &lt;Deployment-name&gt; --cpu-percent=50 --min=1 --max=10
</code></pre>
<p>Example ref : <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/</a></p>
<p>If there is less traffic <strong>auto-scale</strong> down to zero is also possible and based on CPU usage it can <strong>scale up</strong> pods auto without user input.</p>
<p>If you not looking for scaling or just for templating you are facing issues ignore above answer and you can use the <strong>scaffold</strong> or <strong>helm</strong> to mange the <strong>YAML</strong> templating dynamically.</p>
<p><strong>Deployment.yaml</strong></p>
<pre><code>replicas: {{ .Values.replicas}}
</code></pre>
<p><strong>values-dev.yaml</strong></p>
<pre><code>replicas=3
</code></pre>
<p><strong>values-staging.yaml</strong></p>
<pre><code>replicas=5
</code></pre>
<p>based on the environment you can pass the <strong>values-*.yaml</strong> replica to helm and it will create the template for you.</p>
