Schema initiation in Binami postgresql image for kubernetes cluster

1/24/2022

I am using bitnami PostgreSQL image to deploy StatfulSet inside my cluster node. I am not sure how to initiate schema for the PostgreSQL pod without building on top of bitnami image. I have looked around on the internet and someone said to use init containers but I am also not sure how exactly I would do that.

-- Ashish
bitnami
devops
kubernetes
kubernetes-statefulset
postgresql

1 Answer

1/25/2022

From the Github Readme of the Bitnami Docker image:

When the container is executed for the first time, it will execute the files with extensions .sh, .sql and .sql.gz located at /docker-entrypoint-initdb.d.

In order to have your custom files inside the docker image you can mount them as a volume.

You can just mount such scripts under that directory using a ConfigMap volume. An example could be the following:

First, create the ConfigMap with the scripts, for example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: p-init-sql
  labels:
    app: the-app-name
data:
  01_init_db.sql: |-
    # content of the script goes here
  02_second_init_db.sql: |-
    # more content for another script goes here

Second, under spec.template.spec.volumes, you can add:

volumes:
  - configMap:
      name: p-init-sql

Then, under spec.template.spec.containers[0].volumeMounts, you can mount this volume with:

volumeMounts:
  - mountPath: /docker-entrypoint-initdb.d
    name: p-init-sql

With this said, you may find out that it is more easy to use HELM Charts.

Bitnami provides HELM Charts for all its images which simplify the usage of such images by a lot (as everything is ready to be installed and configured from a simple values.yaml file)

For example, there is such a chart for postgresql which you can find here and that can be of inspiration in how to configure the docker image even if you decide to write your own Kubernetes resources around that image.

-- AndD
Source: StackOverflow