2

I have a simple consol application I want there is rabbitmq connection as a consumer and there is a database connection to SQL Server.

Before Containarize the application. There is some commands I have implemented, For example if I enterned X the application will exit so internaly I stop the consumer first and make sure there is no pending message and then gracefully I exit the application.

After Containarize the application I did successfully containarize the application and working perfectly under kuberenetes cluster but my problem is that How to send command to my application ?

For example I want to implement Livenessprob: check the database connection

Before terminating the pod I want to write x to my running console to implement gracefully exit the application without messages loss.

      postStart:
        exec:
          command: []
      preStop:
        exec:
          command: []

I expecting the solution to be a specific command which is writing to my running console application But how to send that command? Or how to make my console able to receive commands from kubernetes?

Victor Faltas
  • 168
  • 1
  • 9
  • Hi Victor, are you aware of the `kubectl attach -i` command? You can attach to any running process inside a Pod. – mozello Apr 05 '22 at 13:46
  • And it is not clear what exactly you want to add: liveness/readiness probes or preStop hook? – mozello Apr 05 '22 at 14:01
  • what I want to implement I want to handle the preStop and liveness prob in my .net core consol application to gracefully close the application. – Victor Faltas Apr 05 '22 at 14:15
  • One of the work arournd. Is to use file creation and filewatcher to handle the kubernetes prestop event For example :- in preStop Event I will write a command to create a file in a specific directly and the console application will watch that directory if the file of closing app is created I will do the logic of gracefully closing the application. But I am not sure if there is any other solution or better solution to this problem or not – Victor Faltas Apr 05 '22 at 14:19
  • So, you are aware of k8s liveness and readiness probes, and you want to know how to implement endpoints for each probe in your app, right? Check [this link](https://andrewlock.net/deploying-asp-net-core-applications-to-kubernetes-part-6-adding-health-checks-with-liveness-readiness-and-startup-probes/#health-checks-in-asp-net-core), please. – mozello Apr 06 '22 at 16:30
  • Thank you Mozello for your reply. I have checked your link yes it helps how to implement healthy check for web based application but my problem how to apply that on a Console application. – Victor Faltas Apr 07 '22 at 17:29

1 Answers1

0

In Kubernetes, you can set up Liveness, Readiness, and Startup Probes. Check the official Kubernetes Documentation page on this topic.

The Kubelet Node agent can perform these probes on running Pods using 3 different methods:

  • HTTP: The Kubelet probe performs an HTTP GET request against an endpoint (like /health), and succeeds if the response status is between 200 and 399
  • TCP: The Kubelet probe attempts to connect to your container on a specified port. If it can establish a TCP connection, then the probe succeeds.
  • Container Command: The Kubelet probe executes a command inside of the running container. If the exit code is 0, then the probe succeeds.

If you wish you can define a liveness probe command:

  • you can run some command:
livenessProbe:
  exec:
    command:
    - cat
    - /tmp/healthy
  initialDelaySeconds: 10
  periodSeconds: 5

To perform a probe, the kubelet executes the command cat /tmp/healthy in the target container. If the command succeeds, it returns 0, and the kubelet considers the container to be alive and healthy. If the command returns a non-zero value, the kubelet kills the container and restarts it.

  • you can run some bash script located in the container:
livenessProbe:
  exec:
    command: 
    - sh 
    - -c 
    - /health/ping_liveness_local.sh
...
  • or write the bash script in the probe section:
  livenessProbe:
    exec:
      command:
      - /bin/sh
      - -c
      - |-
        echo 'if the app writes a PID file, we could check that it is still alive';
        [ -f /run/my-application-web.pid ] && ps -A | grep my-application-web
 ...

Container Lifecycle Hooks (docs)

There are two types of hook handlers that can be implemented for Containers:

  • HTTP - Executes an HTTP request against a specific endpoint on the Container.
  • Exec - Executes a specific command, such as pre-stop.sh, inside the cgroups and namespaces of the Container.

Example:

spec:
  containers:
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
      preStop:
        exec:
          command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]

And as you mentioned in comments you can use the touch /someDir/someFile preStop command to create a file and watch it in your app to shutdown gracefully.

mozello
  • 1,083
  • 3
  • 8
  • I have tried your solution but I faced many problems with the implementation of C# 1 - file watcher library is not working on the linux environment so I can not apply events based on file creation or removal So I decided to go with ports. Ports working fine for liveness and readiness prob but for preStop event it didn't work. 2- My second problem is prestop is not triggered at all on my application – Victor Faltas May 25 '22 at 02:31