kubernetes.net-coreconsole-applicationlivenessprobe

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


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?


Solution

  • 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:

    If you wish you can define a liveness probe 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.

    livenessProbe:
      exec:
        command: 
        - sh 
        - -c 
        - /health/ping_liveness_local.sh
    ...
    
      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:

    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.