From 0dc49c02ddca8cd081dbd1131275894eabdac663 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Tue, 30 Jan 2018 20:48:05 +0100 Subject: [PATCH] check-container: {live,ready}ness checks moved from templates Related: #226 --- 9.4/root/usr/libexec/check-container | 39 ++++++++++++++++++++ 9.5/root/usr/libexec/check-container | 39 ++++++++++++++++++++ examples/postgresql-ephemeral-template.json | 10 ++--- examples/postgresql-persistent-template.json | 10 ++--- examples/replica/postgresql_replica.json | 20 +++++----- latest/root/usr/libexec/check-container | 39 ++++++++++++++++++++ 6 files changed, 137 insertions(+), 20 deletions(-) create mode 100755 9.4/root/usr/libexec/check-container create mode 100755 9.5/root/usr/libexec/check-container create mode 100755 latest/root/usr/libexec/check-container diff --git a/9.4/root/usr/libexec/check-container b/9.4/root/usr/libexec/check-container new file mode 100755 index 00000000..bee6acc8 --- /dev/null +++ b/9.4/root/usr/libexec/check-container @@ -0,0 +1,39 @@ +#! /bin/sh + +# Try whether the PostgreSQL in container accepts connections. +# +# With --live, be tolerant to starting PG server. If the /bin/postgres binary +# has not been executed yet (the shell script is initializing the container), +# wait for it (this script might run forever, we expect that the timeout is +# maintained externally). + +test -z "$ENABLED_COLLECTIONS" || . scl_source enable $ENABLED_COLLECTIONS + +check=ready +case $1 in + --live) check=live ;; +esac + +while true; do + # Wait until the /bin/postgres is executed. + case $(readlink -f /proc/1/exe) in + *bash) sleep 0.2; continue ;; + *) break ;; + esac +done + +# Timeout is infinite (openshift template has the timeout pre-set) +pg_isready -q \ + -h 127.0.0.1 \ + ${POSTGRESQL_USER+-U "$POSTGRESQL_USER"} \ + ${POSTGRESQL_DATABASE+-d "$POSTGRESQL_DATABASE"} \ + --timeout 0 +rc=$? + +# we are ready only if the db accepts connections +test $rc -eq 0 && exit 0 + +# container is live also during pg cluster start-up +test $rc -eq 1 && test $check = live && exit 0 + +exit 1 diff --git a/9.5/root/usr/libexec/check-container b/9.5/root/usr/libexec/check-container new file mode 100755 index 00000000..bee6acc8 --- /dev/null +++ b/9.5/root/usr/libexec/check-container @@ -0,0 +1,39 @@ +#! /bin/sh + +# Try whether the PostgreSQL in container accepts connections. +# +# With --live, be tolerant to starting PG server. If the /bin/postgres binary +# has not been executed yet (the shell script is initializing the container), +# wait for it (this script might run forever, we expect that the timeout is +# maintained externally). + +test -z "$ENABLED_COLLECTIONS" || . scl_source enable $ENABLED_COLLECTIONS + +check=ready +case $1 in + --live) check=live ;; +esac + +while true; do + # Wait until the /bin/postgres is executed. + case $(readlink -f /proc/1/exe) in + *bash) sleep 0.2; continue ;; + *) break ;; + esac +done + +# Timeout is infinite (openshift template has the timeout pre-set) +pg_isready -q \ + -h 127.0.0.1 \ + ${POSTGRESQL_USER+-U "$POSTGRESQL_USER"} \ + ${POSTGRESQL_DATABASE+-d "$POSTGRESQL_DATABASE"} \ + --timeout 0 +rc=$? + +# we are ready only if the db accepts connections +test $rc -eq 0 && exit 0 + +# container is live also during pg cluster start-up +test $rc -eq 1 && test $check = live && exit 0 + +exit 1 diff --git a/examples/postgresql-ephemeral-template.json b/examples/postgresql-ephemeral-template.json index f29698d0..d4b58608 100644 --- a/examples/postgresql-ephemeral-template.json +++ b/examples/postgresql-ephemeral-template.json @@ -123,15 +123,15 @@ "timeoutSeconds": 1, "initialDelaySeconds": 5, "exec": { - "command": [ "/bin/sh", "-i", "-c", "psql -h 127.0.0.1 -U $POSTGRESQL_USER -q -d $POSTGRESQL_DATABASE -c 'SELECT 1'"] + "command": [ "/usr/libexec/check-container" ] } }, "livenessProbe": { + "timeoutSeconds": 10, + "initialDelaySeconds": 120, "exec": { - "command": [ "/bin/sh", "-i", "-c", "pg_isready -h 127.0.0.1 -p 5432" ] - }, - "timeoutSeconds": 1, - "initialDelaySeconds": 30 + "command": [ "/usr/libexec/check-container", "--live" ] + } }, "env": [ { diff --git a/examples/postgresql-persistent-template.json b/examples/postgresql-persistent-template.json index 7feeb704..abfe6d66 100644 --- a/examples/postgresql-persistent-template.json +++ b/examples/postgresql-persistent-template.json @@ -140,15 +140,15 @@ "timeoutSeconds": 1, "initialDelaySeconds": 5, "exec": { - "command": [ "/bin/sh", "-i", "-c", "psql -h 127.0.0.1 -U $POSTGRESQL_USER -q -d $POSTGRESQL_DATABASE -c 'SELECT 1'"] + "command": [ "/usr/libexec/check-container" ] } }, "livenessProbe": { + "timeoutSeconds": 10, + "initialDelaySeconds": 120, "exec": { - "command": [ "/bin/sh", "-i", "-c", "pg_isready -h 127.0.0.1 -p 5432" ] - }, - "timeoutSeconds": 1, - "initialDelaySeconds": 30 + "command": [ "/usr/libexec/check-container", "--live" ] + } }, "env": [ { diff --git a/examples/replica/postgresql_replica.json b/examples/replica/postgresql_replica.json index eb9bf4f6..775a2dc7 100644 --- a/examples/replica/postgresql_replica.json +++ b/examples/replica/postgresql_replica.json @@ -207,15 +207,15 @@ "timeoutSeconds": 1, "initialDelaySeconds": 5, "exec": { - "command": [ "/bin/sh", "-i", "-c", "psql -h 127.0.0.1 -U $POSTGRESQL_USER -q -d $POSTGRESQL_DATABASE -c 'SELECT 1'"] + "command": [ "/usr/libexec/check-container" ] } }, "livenessProbe": { + "timeoutSeconds": 10, + "initialDelaySeconds": 120, "exec": { - "command": [ "/bin/sh", "-i", "-c", "pg_isready -h 127.0.0.1 -p 5432" ] - }, - "timeoutSeconds": 1, - "initialDelaySeconds": 30 + "command": [ "/usr/libexec/check-container", "--live" ] + } }, "env": [ { @@ -318,15 +318,15 @@ "timeoutSeconds": 1, "initialDelaySeconds": 5, "exec": { - "command": [ "/bin/sh", "-i", "-c", "psql -h 127.0.0.1 -U $POSTGRESQL_USER -q -d $POSTGRESQL_DATABASE -c 'SELECT 1'"] + "command": [ "/usr/libexec/check-container" ] } }, "livenessProbe": { + "timeoutSeconds": 10, + "initialDelaySeconds": 120, "exec": { - "command": [ "/bin/sh", "-i", "-c", "pg_isready -h 127.0.0.1 -p 5432" ] - }, - "timeoutSeconds": 1, - "initialDelaySeconds": 30 + "command": [ "/usr/libexec/check-container", "--live" ] + } }, "env": [ { diff --git a/latest/root/usr/libexec/check-container b/latest/root/usr/libexec/check-container new file mode 100755 index 00000000..bee6acc8 --- /dev/null +++ b/latest/root/usr/libexec/check-container @@ -0,0 +1,39 @@ +#! /bin/sh + +# Try whether the PostgreSQL in container accepts connections. +# +# With --live, be tolerant to starting PG server. If the /bin/postgres binary +# has not been executed yet (the shell script is initializing the container), +# wait for it (this script might run forever, we expect that the timeout is +# maintained externally). + +test -z "$ENABLED_COLLECTIONS" || . scl_source enable $ENABLED_COLLECTIONS + +check=ready +case $1 in + --live) check=live ;; +esac + +while true; do + # Wait until the /bin/postgres is executed. + case $(readlink -f /proc/1/exe) in + *bash) sleep 0.2; continue ;; + *) break ;; + esac +done + +# Timeout is infinite (openshift template has the timeout pre-set) +pg_isready -q \ + -h 127.0.0.1 \ + ${POSTGRESQL_USER+-U "$POSTGRESQL_USER"} \ + ${POSTGRESQL_DATABASE+-d "$POSTGRESQL_DATABASE"} \ + --timeout 0 +rc=$? + +# we are ready only if the db accepts connections +test $rc -eq 0 && exit 0 + +# container is live also during pg cluster start-up +test $rc -eq 1 && test $check = live && exit 0 + +exit 1