How to add Kubernetes livenessprob and readinessprob to my .net core console application?

1.5k Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

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.