<p>I have a AWS EKS K8s setup with autoscaling group that uses spot instances.</p>
<p>Once a day there is a scheduled K8s cronjob which scales down a bunch of deployments, runs a script in a pod for 15min , then scales the deployments back up.</p>
<p>Issue is after deployments are scaled down, the script starts running and cluster autoscaling triggers to remove some nodes around 10min later which kills the script pod and moves it to different node.</p>
<p>This can't happen, how do I configure the cronjob script pod so that it never gets killed by autoscaler?</p>
<p>Thanks!</p>

robliv
<blockquote>
<p>This can't happen, how do I configure the cronjob script pod so that
it never gets killed by autoscaler?</p>
</blockquote>
<p>You can use the Node or POD affinity for scheduling the POD on specific Node so cronjob will not runt he PODs on Spot instances only you stateless workload will run on spot instance about which you don't care much.</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: test
spec:
  schedule: &quot;*/5 * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          affinity:
            podAntiAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
              - labelSelector:
                  matchExpressions:
                  - key: app
                    operator: In
                    values:
                    - web-store
                topologyKey: &quot;kubernetes.io/hostname&quot;
          containers:
            - name: hello
              image: bash
              command: [&quot;echo&quot;,  &quot;Hello world&quot;]
          restartPolicy: OnFailure
</code></pre>
<p>You can use the Node selector also to schedule the POD on specific type of Node group which is not scaling up &amp; down.</p>
<p>Node affinity example</p>
<pre><code>affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/e2e-az-name
            operator: In
            values:
            - e2e-az1
            - e2e-az2
</code></pre>
<p>Using the affinity you can plan on which node to schedule your POds.</p>
<p>Read more at : <a href="https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/</a></p>
