0

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?

NevD
  • 265
  • 2
  • 10
  • Instead of making up your own `env` variables why not use the ones `libpq` understands [Env](https://www.postgresql.org/docs/current/libpq-envars.html) and then you could use the `libpq` based clients(psql, pg_isready) without going through bash as you did with: `psql -U postgres -c 'SELECT 1;'` Which works by the way because not specifying `-d` defaults to using the user name as the database name. – Adrian Klaver Aug 14 '21 at 20:01
  • Because I followed the [Docker image](https://hub.docker.com/_/postgres) conventions. I will have a look. Another point, the default to the user name as database name shouldn't work since they are different, no? – NevD Aug 14 '21 at 20:04
  • Also these `bash -c -w -U ..` are going to fail as there is no program e.g. `psql` to use the connection settings. And this `FATAL, role "root` would indicate that the scripts are being run as `root` and the `env` variables are not being set. `psql -U postgres` means it will connect to database `postgres`. `psql` has no knowledge of your made up `env` variables so it will not use `POSTGRES_DB=mydb`. – Adrian Klaver Aug 14 '21 at 20:11
  • ah sorry, my mistake in writing down, yes, of course there is `psql` in front. And yes, the ENV vars are not read since `kubectl exec -it pg-red-0 -- bash -c echo $POSTGRES_USER` returns nothing. But people seem to use this! – NevD Aug 14 '21 at 20:19
  • If you have a look at [this link](https://github.com/kubernetes/kubernetes/issues/40846), it says that using `bash`, Postgres should be aware of his environment, so read $POSTGRES. – NevD Aug 14 '21 at 20:28
  • I don't use Kubernetes, but for those that do and could be of more help, I would say you need to add to your question the configuration file you are using to declare them. – Adrian Klaver Aug 14 '21 at 21:01
  • I'm not seeing anywhere in the config where you making them `env` variables. I think you need to take a look at either [Env var](https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/) or [ConfigMap](https://kubernetes.io/docs/concepts/configuration/configmap/) – Adrian Klaver Aug 14 '21 at 21:19
  • I believe I am indeed using a ConfigMap – NevD Aug 14 '21 at 21:55
  • Yes but from the link I provided it does not look like you are defining the `env` variables correctly. Pretty sure `data:` is not the way to do this. – Adrian Klaver Aug 14 '21 at 22:03
  • No, this is correct. The app receives them correctly otherwise nothing would work, not just that. – NevD Aug 14 '21 at 22:16
  • It is correct in that you can specify `data:`, it is incorrect in that as you have shown the `env` variables are not set. Read the links I posted. Also nowhere in the link you posted do they use `data:` they use `env:`. – Adrian Klaver Aug 14 '21 at 22:32
  • They are set. `kubectl exec -it pg-dep-0 -- sh / # echo $POSTGRES_USER postgres <-- correct` – NevD Aug 14 '21 at 22:35
  • Link from [kubernetes configmap] (https://kubernetes.io/docs/concepts/configuration/configmap/#configmaps-and-pods). `Env` is in a pod. The problem is not here. – NevD Aug 14 '21 at 22:37
  • Look at this [Config](https://www.golinuxcloud.com/kubernetes-configmaps/) Example-3: Use Kubernetes ConfigMap to declare environment variables. – Adrian Klaver Aug 14 '21 at 22:56
  • Many thanks for the help, but again, I showed you that the pods **know** these variables so this is not the problem. You may read more in the official documentation if you want to learn Kubernetes, and so should I. – NevD Aug 15 '21 at 15:13

0 Answers0