<p>I tried Keda with AKS and I really appreciate when pod are automatically instanciate based on Azure Dev Ops queue job for release &amp; build.</p>
<p>However I noticed something strange and often AKS/Keda remove pod while processing which makes workflow failed.
<a href="https://i.stack.imgur.com/vbsm7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vbsm7.png" alt="enter image description here" /></a></p>
<p>Message reads
<code>We stopped hearing from agent aks-linux-768d6647cc-ntmh4. Verify the agent machine is running and has a healthy network connection. Anything that terminates an agent process, starves it for CPU, or blocks its network access can cause this error. For more information, see: https://go.microsoft.com/fwlink/?linkid=846610</code></p>
<p>Expected behavior: pods must complete the tasks then Keda/AKS can remove this pod.</p>
<p>I share with you my keda yml file:</p>

<pre><code># deployment.yaml
apiVersion: apps/v1 # The API resource where this workload resides
kind: Deployment # The kind of workload we're creating
metadata:
  name: aks-linux # This will be the name of the deployment
spec:
  selector: # Define the wrapping strategy
    matchLabels: # Match all pods with the defined labels
      app: aks-linux # Labels follow the `name: value` template
  replicas: 1
  template: # This is the template of the pod inside the deployment
    metadata: # Metadata for the pod
      labels:
        app: aks-linux
    spec:
      nodeSelector:
        agentpool: linux
      containers: # Here we define all containers
        - image: &lt;My image here&gt;
          name: aks-linux
          env:
            - name: &quot;AZP_URL&quot;
              value: &quot;&lt;myURL&gt;&quot;
            - name: &quot;AZP_TOKEN&quot;
              value: &quot;&lt;MyToken&gt;&quot;
            - name: &quot;AZP_POOL&quot;
              value: &quot;&lt;MyPool&gt;&quot;
          resources:
            requests: # Minimum amount of resources requested
              cpu: 2
              memory: 4096Mi
            limits: # Maximum amount of resources requested
              cpu: 4
              memory: 8192Mi
</code></pre>
<p>I used latest version of AKS and Keda. Any idea ?</p>

<p>Check <a href="https://keda.sh/blog/2021-05-27-azure-pipelines-scaler/" rel="nofollow noreferrer">the official Keda docs</a>:</p>
<blockquote>
<p>When running your agents as a deployment you have no control on which pod gets killed when scaling down.</p>
</blockquote>
<p>So, to solve it you need to use <code>ScaledJob</code>:</p>
<blockquote>
<p>If you run your agents as a Job, KEDA will start a Kubernetes job for each job that is in the agent pool queue. The agents will accept one job when they are started and terminate afterwards. Since an agent is always created for every pipeline job, you can achieve fully isolated build environments by using Kubernetes jobs.</p>
</blockquote>
<p>See there how to implement it.</p>
