3.4 KiB
Custom HTTP Probe
Context
An application is deployed in OpenShift and has no built-in liveness/readiness probes. This example shows how to implement a custom probe.
The sample application
This sample app has three probes:
- a standard liveness probe
- a standard readiness probe
- a custom probe
The app takes about 30 seconds to boot and then exhibit two endpoints:
/please-diewill switch the liveness probe to "dead"/please-resuscitatewill switch the liveness probe to "alive"
The custom probe is a mix of everything you can find in a business app:
- when the app is alive, it returns a non-200 HTTP status with JSON content
- when the app is dead, it returns a 500 HTTP status with HTML content
Deploy the sample application
oc new-app nodejs~https://github.com/nmasse-itix/OpenShift-Examples.git --name myapp --context-dir=Custom-HTTP-Probe
oc expose svc/myapp
Play with it
export SAMPLE_APP_URL="http://$(oc get route myapp -o jsonpath='{.spec.host}')" && echo $SAMPLE_APP_URL
Print some help:
curl $SAMPLE_APP_URL/ -D -
Check if the app is ready:
curl $SAMPLE_APP_URL/probe/readiness -D -
Let the app die:
curl $SAMPLE_APP_URL/please-die -D -
Check if the app is alive:
curl $SAMPLE_APP_URL/probe/liveness -D -
Let the app resuscitate:
curl $SAMPLE_APP_URL/please-resuscitate -D -
Inspect the custom probe:
curl $SAMPLE_APP_URL/please-die -D -
curl $SAMPLE_APP_URL/probe/custom -D -
curl $SAMPLE_APP_URL/please-resuscitate -D -
curl $SAMPLE_APP_URL/probe/custom -D -
Use the standard probes
Add the liveness probe:
oc patch dc myapp --type=json -p '[ { "op": "add", "path": "/spec/template/spec/containers/0/livenessProbe", "value": { "initialDelaySeconds": 5, "timeoutSeconds": 5, "httpGet": { "path": "/probe/liveness", "port": 8080 } } } ]'
Add the readiness probe:
oc patch dc myapp --type=json -p '[ { "op": "add", "path": "/spec/template/spec/containers/0/readinessProbe", "value": { "initialDelaySeconds": 20, "timeoutSeconds": 5, "httpGet": { "path": "/probe/readiness", "port": 8080 } } } ]'
Watch the deployment:
oc get pods -w
Let the app die:
curl $SAMPLE_APP_URL/please-die -D -
Watch the app being restarted:
oc get pods -w
Use the custom probe
Review the probe.py code.
Create a configMap containing the custom probe code:
oc create configmap myapp-probe --from-file probe.py
Mount the configMap on /opt/probe:
oc volume dc/myapp --add --overwrite --type=configMap --configmap-name=myapp-probe --default-mode=755 --mount-path=/opt/probe --name probe --confirm
Replace the standard liveness and readiness probes by the custom one:
oc patch dc myapp --type=json -p '[ { "op": "replace", "path": "/spec/template/spec/containers/0/livenessProbe", "value": { "initialDelaySeconds": 5, "timeoutSeconds": 5, "exec": { "command": [ "/opt/probe/probe.py", "http://localhost:8080/probe/custom" ] } } } ]'
oc patch dc myapp --type=json -p '[ { "op": "replace", "path": "/spec/template/spec/containers/0/readinessProbe", "value": { "initialDelaySeconds": 5, "timeoutSeconds": 5, "exec": { "command": [ "/opt/probe/probe.py", "http://localhost:8080/probe/custom" ] } } } ]'
Let the app die:
curl $SAMPLE_APP_URL/please-die -D -
Watch the app being restarted:
oc get pods -w