John Davidson

docker - Liveness probe for long running PHP CLI job

0 comments
Message:


we have a batch data processing script inside a container and want to check it is alive and actually working or whether it should be restarted.


It is PHP command line script and doesn't expose any kind of server. Currently running in Docker soon in Kubernetes.


How can we monitor liveness of such script without introducing unnecessary features/libraries?



My Answer:
One way to monitor the liveness of a long running PHP CLI job inside a Docker container is to use a simple health check script that periodically checks if the job is still running and functioning properly. Here's an example of how you can implement this:

1. Create a simple health check script in PHP that checks if the job is still running. This script can be as simple as checking if a specific file exists or if a specific process is running. For example, you can create a file called `healthcheck.php` with the following content:

php

// Check if the process with PID 1 is still running
$pid = getmypid();
if (posix_kill($pid, 0)) {
echo "OK";
} else {
echo "FAIL";
}


2. Add a liveness probe to your Kubernetes deployment configuration that runs the health check script periodically. This can be done by adding the following lines to your deployment configuration:

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: your-deployment
spec:
template:
spec:
containers:
- name: your-container
image: your-image
livenessProbe:
exec:
command:
- php
- /path/to/healthcheck.php
initialDelaySeconds: 5
periodSeconds: 10


This configuration will run the health check script every 10 seconds and restart the container if the script returns a non-successful status.

By implementing a simple health check script and adding a liveness probe to your Kubernetes deployment configuration, you can monitor the liveness of your long running PHP CLI job without introducing unnecessary features or libraries.

Rate this post

4 of 5 based on 9740 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us