<h2>Background</h2>
<p>I have two Kubernetes nodepools - <code>A</code> and <code>B</code>.</p>
<p><code>A</code> nodepool is the right hardware and the preferred one. <code>B</code> be nodepool is less preferred since it using different hardware.</p>
<p>My pods allocation preferring nodepool <code>A</code>. But, <code>A</code> is not always available. Its available 70% of the time.</p>
<p>Once a pod spawned in nodepool <code>B</code>, it will stay there forever. This is inefficient for me.</p>
<h2>Question:</h2>
<p>I want to schedule auto termination of a pod, if its living inside of nodepool <code>B</code>. The auto termination can be done after a fix time (or even better random time between X to Y minutes).</p>
<p>How to implement this?</p>

No1Lives4Ever
<p>The need is very clear.</p>
<p>You can add this piece of code to your Deployment (.spec.affinity.nodeAffinity):</p>
<pre><code>    nodeAffinity:
      # Soft anti affinity between nodes A and B
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: nodepool-a
              operator: In
              values:
              - &quot;true&quot;
          topologyKey: &quot;kubernetes.io/hostname&quot;
</code></pre>
<p>Assuming that the nodepool-a has a label &quot;nodepool-a=true&quot; (it could be any other), adding this configuration the Pods will be assigned to the nodepool-a; if nodepool-a is not available, they are scheduled on other nodepools that don't match the label.</p>
<p>With this configuration, you just need to schedule a rollout restart of your Deployment to move the Pods to nodepool-a when it is available.</p>
<p>Unfortunately there is no other way to achieve your goal; maybe you could automate the Deployment rollout restart triggered by a monitoring event, which after some time &quot;notifies&quot; that the nodepool-a is available.</p>
<p><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>
<p>Taking a look in the <em>nodeaffinity</em> code, I understand that the <em>requiredDuringSchedulingRequiredDuringExecution</em> function will be implemented which should even eviction Pods that are on nodes that do not match the label.</p>
<p>There are no details on when it will be implemented though.</p>
<p><a href="https://github.com/kubernetes/kubernetes/blob/v1.26.3/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go#L36" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/blob/v1.26.3/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go#L36</a> (line 258)</p>
