I am trying to execute the very standard SELECT 1
command as a readiness
probe command.
I've been through numerous posts on Postgres, root user and Kubernetes but I still can't get it right although this might be trivial.
I defined ENV variables POSTGRES_USER=postgres
and POSTGRES_DB=mydb
in the config manifest of my Postgres StatefulSet manifest.
I understood I had to use bash
to execute a command since ENV variables are not interpolated.
All following commands fail with FATAL, role "root
bash -c pg_isready -U $POSTGRES_USER -p 5432 -> FATAL
bash -c psql -w -U $POSTGRES_USER -d $POSTGRES_DB -c 'SELECT 1;' -> FATAL
# bash doesn't read ENV ?
bash -c psql -w -U $POSTGRES_USER -d mydb -c 'SELECT 1;' -> FATAL
# maybe current user from host? "me" from "whoami"
bash -c psql -w -U me -d postgres -c 'SELECT 1;' -> FATAL
Then I execute the commands to check if bash
gets the ENV variables -> NO
kubectl exec -it pg-dep-0 -- bash -c psql -U $POSTGRES_USER -d $POSTGRES_DB -c 'SELECT 1;' -> FATAL
kubectl exec -it pg-dep-0 -- bash -c psql -U $POSTGRES_USER -d $POSTGRES_DB -c 'SELECT 1;' -> FATAL
kubectl exec -it pg-dep-0 -- psql -U $POSTGRES_USER -d $POSTGRES_DB -c 'SELECT 1;' -> FATAL
but:
kubectl exec -it pg-dep-0 -- psql -U postgres -c 'SELECT 1;' <-- correct
kubectl exec -it pg-dep-0 -- sh
/ # echo $POSTGRES_USER
postgres <-- correct
succeeds, without bash but no ENV variable.
Finally, setting directly the values and not using bash
with psql -U postgres -c 'SELECT 1;
(without bash
) as the command for the readiness
probe also fails: Postgres is up, but the test returns "Readiness probe failed".
I used directly:
spec.tempalte.spec.containers:
[...]
envFrom:
- configMapRef:
name: config
readinessProbe:
exec:
command:
- bash
- "-c"
- |
psql -U "postgres" -d "mydb" -c 'SELECT 1;'
apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
POSTGRES_HOST: "pg-svc"
POSTGRES_DB: "mysb"
POSTGRES_USER: "postgres"
A bit lost, any advice?