kubernetesnats.iolivenessprobe

curl and grep Command in liveness probes in kuberenetes


I am new to Kubernetes. I have written a deployment for my python console application. this app is subscribed to NATS (message queue).

I have written command in liveness prob to check the connection_id(my app connection id) in the nats server is present. otherwise, restart the pod as the application is not running properly, in that case.

I have tried different commands. for example

        livenessProbe:
          exec:
            command:
            - sh
            - -c
            #- curl -s nats:8222/connz?cid=$(cat /tmp/cid) | python3 -c "import sys, json; print(json.load(sys.stdin)['connections'][0]['cid'])" | echo

            #- curl -s http://nats:8222/connz?cid=$(cat /tmp/cid) | grep "$(cat /tmp/cid)"
            - curl -s http://nats:8222/connz?cid=$(cat /tmp/cid) | grep "cid"
          initialDelaySeconds: 10
          periodSeconds: 10

and another few curl commands. then when I remove /tmp/cid file. it should fail, right? But it does not.

If I run this command curl -s http://nats:8222/connz?cid=$(cat /tmp/cid) | grep -c "\"cid\": $(cat /tmp/cid)"

I get io.k8s.api.core.v1.ExecAction.command: got "map", expected "string" this issue.

Any suggestion?


Solution

  • curl -s http://localhost:8222/connz?cid=$(cat /tmp/cid) | grep -c "cid.* $(cat /tmp/cid),"
    

    This finally worked for me.

    Stuck with the issue for 2 days :(