kubegres icon indicating copy to clipboard operation
kubegres copied to clipboard

Override primary_init_script.sh does not work

Open n-able-consulting opened this issue 2 years ago • 2 comments

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: POSTGRES_PASSWORD
        valueFrom:
           secretKeyRef:
              name: mysecretresource
              key: superUserPassword

      - name: POSTGRES_REPLICATION_PASSWORD
        valueFrom:
           secretKeyRef:
              name: mysecretresource
              key: replicationUserPassword
        
      - name: POSTGRES_MY_DB_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecretresource
            key: myDbUserPassword

      - name: MY_OTHER_VAR
        value: "any_value"
---
EOF
  1. This runs. I get 3 pods (3 statefull sets) and a service:
NAME                 READY   STATUS    RESTARTS      AGE   IP             NODE      NOMINATED NODE   READINESS GATES
pod/mypostgres-1-0   1/1     Running   1 (28m ago)   28m   10.1.54.215    w3.k108   <none>           <none>
pod/mypostgres-2-0   1/1     Running   0             28m   10.1.177.152   w7.k108   <none>           <none>
pod/mypostgres-3-0   1/1     Running   0             28m   10.1.25.210    w1.k108   <none>           <none>
pod/ubuntu           1/1     Running   0             52m   10.1.54.214    w3.k108   <none>           <none>

NAME                         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE   SELECTOR
service/kubernetes           ClusterIP   10.2.0.1     <none>        443/TCP    54m   <none>
service/mypostgres           ClusterIP   None         <none>        5432/TCP   28m   app=mypostgres,replicationRole=primary
service/mypostgres-replica   ClusterIP   None         <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
  1. I run an ubuntu pod: k run ubuntu --image=ubuntu sleep 99999999; k exec -it ubuntu -- /bin/bash
  2. Install psql: apt-get install -y postgresql-client
  3. Try to jump in my created db: psql postgresql://my_username:54WvEAhp1VTI0MpzlUkvJI65SIjPfTV5Hqcqc7gY3z8ZD03tRqL10OwTctStNgx5TL8s1wyI02C9Q02ewZddVnUTV0ZRorqrK6@argo-postgres.argo.svc.cluster.local:5432/my_app_db
  4. 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"
  5. Using superuser works: psql postgresql://postgres:54WvEAhp1VTI0MpzlUkvJI65SIjPfTV5Hqcqc7gY3z8ZD03tRqL10OwTctStNgx5TL8s1wyI02C9Q02ewZddVnUTV0ZRorqrK6@mypostgres.default.svc.cluster.local:5432/postgres
  6. 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)
  1. requesting all present users:
\du
  Role name  |                         Attributes                         | Member of 
-------------+------------------------------------------------------------+-----------
 postgres    | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 replication | Replication                                                | {}

n-able-consulting avatar Jan 25 '23 09:01 n-able-consulting

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

bhinderbaljeet avatar Feb 16 '23 23:02 bhinderbaljeet

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

bhinderbaljeet avatar Feb 16 '23 23:02 bhinderbaljeet