<p>Kubernetes Version</p>
<pre><code>Client Version: version.Info{Major:&quot;1&quot;, Minor:&quot;21&quot;, GitVersion:&quot;v1.21.1&quot;, GitCommit:&quot;5e58841cce77d4bc13713ad2b91fa0d961e69192&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2021-05-13T02:40:46Z&quot;, GoVersion:&quot;go1.16.3&quot;, Compiler:&quot;gc&quot;, Platform:&quot;linux/amd64&quot;}
Server Version: version.Info{Major:&quot;1&quot;, Minor:&quot;20&quot;, GitVersion:&quot;v1.20.7&quot;, GitCommit:&quot;e1d093448d0ed9b9b1a48f49833ff1ee64c05ba5&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2021-06-03T00:20:57Z&quot;, GoVersion:&quot;go1.15.12&quot;, Compiler:&quot;gc&quot;, Platform:&quot;linux/amd64&quot;}

</code></pre>
<p>I have a Kubernetes crobjob that serves the purpose of running some Azure cli commands on a time based schedule.</p>
<p>Running the container locally works fine, however, manually triggering the Cronjob through <a href="https://k8slens.dev/" rel="nofollow noreferrer">Lens</a>, or letting it run per the schedule results in weird behaviour (Running in the cloud as a job yeilds unexpected results).</p>
<p>Here is the cronjob definition:</p>
<pre><code>---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: development-scale-down
  namespace: development
spec:
  schedule: &quot;0 22 * * 0-4&quot;
  concurrencyPolicy: Allow
  startingDeadlineSeconds: 60
  failedJobsHistoryLimit: 5
  jobTemplate:
    spec:
      backoffLimit: 0 # Do not retry
      activeDeadlineSeconds: 360 # 5 minutes
      template:
        spec:
          containers:
          - name: scaler
            image: myimage:latest
            imagePullPolicy: Always
            env: ...
          restartPolicy: &quot;Never&quot;

</code></pre>
<p>I ran the cronjob manually and it created job <code>development-scale-down-manual-xwp1k</code>. Describing this job after it completed, we can see the following:</p>
<pre><code>$ kubectl describe job development-scale-down-manual-xwp1k
Name:                     development-scale-down-manual-xwp1k
Namespace:                development
Selector:                 controller-uid=ecf8fb47-cd50-42eb-9a6f-888f7e2c9257
Labels:                   controller-uid=ecf8fb47-cd50-42eb-9a6f-888f7e2c9257
                          job-name=development-scale-down-manual-xwp1k
Annotations:              &lt;none&gt;
Parallelism:              1
Completions:              1
Start Time:               Wed, 04 Aug 2021 09:40:28 +1200
Active Deadline Seconds:  360s
Pods Statuses:            0 Running / 0 Succeeded / 1 Failed
Pod Template:
  Labels:  controller-uid=ecf8fb47-cd50-42eb-9a6f-888f7e2c9257
           job-name=development-scale-down-manual-xwp1k
  Containers:
   scaler:
    Image:      myimage:latest
    Port:       &lt;none&gt;
    Host Port:  &lt;none&gt;
    Environment:
      CLUSTER_NAME:    ...
      NODEPOOL_NAME:   ...
      NODEPOOL_SIZE:   ...
      RESOURCE_GROUP:  ...
      SP_APP_ID:       &lt;set to the key 'application_id' in secret 'scaler-secrets'&gt;      Optional: false
      SP_PASSWORD:     &lt;set to the key 'application_pass' in secret 'scaler-secrets'&gt;    Optional: false
      SP_TENANT:       &lt;set to the key 'application_tenant' in secret 'scaler-secrets'&gt;  Optional: false
    Mounts:            &lt;none&gt;
  Volumes:             &lt;none&gt;
Events:
  Type     Reason                Age   From            Message
  ----     ------                ----  ----            -------
  Normal   SuccessfulCreate      24m   job-controller  Created pod: development-scale-down-manual-xwp1k-b858c
  Normal   SuccessfulCreate      23m   job-controller  Created pod: development-scale-down-manual-xwp1k-xkkw9
  Warning  BackoffLimitExceeded  23m   job-controller  Job has reached the specified backoff limit
</code></pre>
<p>This differs from <a href="https://stackoverflow.com/questions/55083361/pod-deletes-automatically-with-kubernetes-cronjob">other issues I have read</a>, where it does not mention a &quot;SuccessfulDelete&quot; event.</p>
<p>The events received from <code>kubectl get events</code> tell an interesting story</p>
<pre><code>$ ktl get events | grep xwp1k
3m19s       Normal    Scheduled                  pod/development-scale-down-manual-xwp1k-b858c   Successfully assigned development/development-scale-down-manual-xwp1k-b858c to aks-burst-37275452-vmss00000d
3m18s       Normal    Pulling                    pod/development-scale-down-manual-xwp1k-b858c   Pulling image &quot;myimage:latest&quot;
2m38s       Normal    Pulled                     pod/development-scale-down-manual-xwp1k-b858c   Successfully pulled image &quot;myimage:latest&quot; in 40.365655229s
2m23s       Normal    Created                    pod/development-scale-down-manual-xwp1k-b858c   Created container myimage
2m23s       Normal    Started                    pod/development-scale-down-manual-xwp1k-b858c   Started container myimage
2m12s       Normal    Killing                    pod/development-scale-down-manual-xwp1k-b858c   Stopping container myimage
2m12s       Normal    Scheduled                  pod/development-scale-down-manual-xwp1k-xkkw9   Successfully assigned development/development-scale-down-manual-xwp1k-xkkw9 to aks-default-37275452-vmss000002
2m12s       Normal    Pulling                    pod/development-scale-down-manual-xwp1k-xkkw9   Pulling image &quot;myimage:latest&quot;
2m11s       Normal    Pulled                     pod/development-scale-down-manual-xwp1k-xkkw9   Successfully pulled image &quot;myimage:latest&quot; in 751.93652ms
2m10s       Normal    Created                    pod/development-scale-down-manual-xwp1k-xkkw9   Created container myimage
2m10s       Normal    Started                    pod/development-scale-down-manual-xwp1k-xkkw9   Started container myimage
3m19s       Normal    SuccessfulCreate           job/development-scale-down-manual-xwp1k         Created pod: development-scale-down-manual-xwp1k-b858c
2m12s       Normal    SuccessfulCreate           job/development-scale-down-manual-xwp1k         Created pod: development-scale-down-manual-xwp1k-xkkw9
2m1s        Warning   BackoffLimitExceeded       job/development-scale-down-manual-xwp1k         Job has reached the specified backoff limit
</code></pre>
<p>I cant figure out why the container was killed, the logs all seem fine and there are no resource constraints. The container is removed very quickly meaning I have very little time to debug. The more verbose event line reads as such</p>
<pre><code>3m54s       Normal    Killing                    pod/development-scale-down-manual-xwp1k-b858c   spec.containers{myimage}   kubelet, aks-burst-37275452-vmss00000d                                 Stopping container myimage                                                                                                                                                       3m54s        1       development-scale-down-manual-xwp1k-b858c.1697e9d5e5b846ef

</code></pre>
<p>I note that the image pull takes a good few seconds (40) initially, might this aid in exceeding the startingDeadline or another cron spec?</p>
<p>Any thoughts or help appreciated, thank you</p>

TheQueenIsDead
<p>Reading logs! Always helpful.</p>
<h2>Context</h2>
<p>For context, the job itself scales an AKS nodepool. We have two, the default <code>system</code> one, and a new user controlled one. The cronjob is meant to scale the new <code>user</code> (Not <code>system</code> pool).</p>
<h2>Investigating</h2>
<p>I noticed that the <code>scale-down</code> job always takes longer compared to the <code>scale-up</code> job, this is due to the image pull always happening when the scale down job runs.</p>
<p>I also noticed that the <code>Killing</code> event mentioned above originates from the kubelet. (<code>kubectl get events -o wide</code>)</p>
<p>I went to check the kubelet logs on the host, and realised that the host name was a little atypical (<code>aks-burst-XXXXXXXX-vmss00000d</code>) in the sense that most hosts in our small development cluster usually has numbers on the end, not <code>d</code></p>
<p>There I realised the naming was different because this node was not part of the default nodepool, and I could not check the kubelet logs because the host had been removed.</p>
<h2>Cause</h2>
<p>The job scales down compute resources. The scale down would fail, because it was always preceeded by a scale up, in which point a new node was in the cluster. This node had nothing running on it, so the next Job was scheduled on it. The Job started on the new node, told Azure to scale down the new node to 0, and subsequently the Kubelet killed the job as it was running.</p>
<p>Always being scheduled on the new node explains why the image pull happened each time as well.</p>
<h2>Fix</h2>
<p>I changed the spec and added a NodeSelector so that the Job would always run on the <code>system</code> pool, which is more stable than the <code>user</code> pool</p>
<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: development-scale-down
  namespace: development
spec:
  schedule: &quot;0 22 * * 0-4&quot;
  concurrencyPolicy: Allow
  startingDeadlineSeconds: 60
  failedJobsHistoryLimit: 5
  jobTemplate:
    spec:
      backoffLimit: 0 # Do not retry
      activeDeadlineSeconds: 360 # 5 minutes
      template:
        spec:
          containers:
          - name: scaler
            image: myimage:latest
            imagePullPolicy: Always
            env: ...
          restartPolicy: &quot;Never&quot;
          nodeSelector:
            agentpool: default
</code></pre>
