Saturday, 19 April 2025

Linux AKS Scaled Jobs Handle Graceful Termination for dotnet App using IHostedService

 We have discussed "Windows AKS Scaled Jobs Handle Graceful Termination for dotnet App using IHostedService When Preemption" previously. However,  for Linux scaled jobs we have to handle the graceful termination, using a bash script intead of a PowerShell script.

We can create a script with bash as shown below.


Copyable code is below.

#!/bin/bash

waitBeforeShutDownSignal=$1;
sleep $waitBeforeShutDownSignal;

dotnet_pid=$(pgrep dotnet);

if [ -n "$dotnet_pid" ]; then
    while kill -0 "$dotnet_pid" 2>/dev/null; do
        sleep 1;
    done
fi

Copy the file to base image in the dockerfile and enable execution for the script.


Then we can use it in scaled job deployment manifest to setup the pre stop hook as shown below..

terminationGracePeriodSeconds: 15000
containers:
  - name: ${aks_app_name}$
    lifecycle:
      preStop:
        exec:
          command: ["/bin/sh","-c","/app/wait_linux_container_gracefully.sh 10"]

This will ensure the preemtion request will not terminate the Linux scaled job, until the termination grace period is over, or the job execution completed. If the termination grace period is setup sufficiently, the job execution to the completion can be guranteed.

No comments:

Popular Posts