reactive-tech / kubegres

Kubegres is a Kubernetes operator allowing to deploy one or many clusters of PostgreSql instances and manage databases replication, failover and backup.
https://www.kubegres.io
Apache License 2.0
1.32k stars 74 forks source link

Override primary_init_script.sh does not work #147

Open n-able-consulting opened 1 year ago

n-able-consulting commented 1 year ago

Trying to override primary init does not work at all. It does result in a postgres db running, but not with the requested db and user in place.

procedure followed:

  1. I install the kubegres operator in Kubernetes
  2. apply following (in line with your documentation):
    
    kubectl apply -f - <<EOF
    apiVersion: v1
    kind: Secret
    metadata:
    name: mysecretresource
    namespace: default
    type: Opaque
    stringData:
    superUserPassword: 54WvEAhp1VTI0MpzlUkvJI65SIjPfTV5Hqcqc7gY3z8ZD03tRqL10OwTctStNgx5TL8s1wyI02C9Q02ewZddVnUTV0ZRorqrK6
    replicationUserPassword: UdbkJmsGIUEm0Y99OeQ9hNBx45zgDlU01JKby1r2oDDTO8757QU52ErrJzqvONH7GYUHqY9oHgwBzHFi3KfwxN1kgjPybiGLRC
    myDbUserPassword: 54WvEAhp1VTI0MpzlUkvJI65SIjPfTV5Hqcqc7gY3z8ZD03tRqL10OwTctStNgx5TL8s1wyI02C9Q02ewZddVnUTV0ZRorqrK6
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: mypostgres-conf
    namespace: default

data:

primary_init_script.sh: |

!/bin/bash

set -e

# This script assumes that the env-var $POSTGRES_MY_DB_PASSWORD contains the password of the custom user to create.
# You can add any env-var in your Kubegres resource config YAML.

dt=$(date '+%d/%m/%Y %H:%M:%S');
echo "$dt - Running init script the 1st time Primary PostgreSql container is created...";

customDatabaseName="my_app_db"
customUserName="my_username"

echo "$dt - Running: psql -v ON_ERROR_STOP=1 --username $POSTGRES_USER --dbname $POSTGRES_DB ...";

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE DATABASE $customDatabaseName;
CREATE USER $customUserName WITH PASSWORD '$POSTGRES_MY_DB_PASSWORD';
GRANT ALL PRIVILEGES ON DATABASE "$customDatabaseName" to $customUserName;
EOSQL

echo "$dt - Init script is completed";

apiVersion: kubegres.reactive-tech.io/v1 kind: Kubegres metadata: name: mypostgres namespace: default

spec:

replicas: 3 image: postgres:14.1 port: 5432

database: size: 200Mi

customConfig: mypostgres-conf

env:

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/kubernetes ClusterIP 10.2.0.1 443/TCP 54m service/mypostgres ClusterIP None 5432/TCP 28m app=mypostgres,replicationRole=primary service/mypostgres-replica ClusterIP None 5432/TCP 28m app=mypostgres,replicationRole=replica

NAME READY AGE CONTAINERS IMAGES statefulset.apps/mypostgres-1 1/1 28m mypostgres-1 postgres:14.1 statefulset.apps/mypostgres-2 1/1 28m mypostgres-2 postgres:14.1 statefulset.apps/mypostgres-3 1/1 28m mypostgres-3 postgres:14.1

5. I run an ubuntu pod: ```k run ubuntu --image=ubuntu sleep 99999999; k exec -it ubuntu -- /bin/bash```
6. Install psql: ```apt-get install -y postgresql-client```
7. Try to jump in my created db: ```psql postgresql://my_username:54WvEAhp1VTI0MpzlUkvJI65SIjPfTV5Hqcqc7gY3z8ZD03tRqL10OwTctStNgx5TL8s1wyI02C9Q02ewZddVnUTV0ZRorqrK6@argo-postgres.argo.svc.cluster.local:5432/my_app_db```
8. Get error: ```psql: error: connection to server at "argo-postgres.argo.svc.cluster.local" (10.1.54.212), port 5432 failed: FATAL:  password authentication failed for user "my_username"```
9. Using superuser works: ```psql postgresql://postgres:54WvEAhp1VTI0MpzlUkvJI65SIjPfTV5Hqcqc7gY3z8ZD03tRqL10OwTctStNgx5TL8s1wyI02C9Q02ewZddVnUTV0ZRorqrK6@mypostgres.default.svc.cluster.local:5432/postgres```
10. requesting all present tables: 

\l Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)

11. requesting all present users:

\du Role name | Attributes | Member of -------------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} replication | Replication | {}

bhinderbaljeet commented 1 year ago

I am seeing the same issue, did you figure out the solution yet?

bhinderbaljeet commented 1 year ago

My bad it was this https://github.com/reactive-tech/kubegres/issues/37#issuecomment-919897298