How to persist data using a postgres database, Docker, and Kubernetes?
In the end, it seems that the real problem was the fact that I was trying to create the database from my entrypoint script. Things such as creating a db or a user should be done at container creation time so I ended up using the standard Postgres image, which actually provides a simple and easy way to create an user and a db.
This is the fully functional configuration file for Postgres.
apiVersion: v1kind: Podmetadata: name: postgres labels: name: postgresspec: containers: - name: postgres image: postgres env: - name: PGDATA value: /var/lib/postgresql/data/pgdata - name: POSTGRES_USER value: myuser - name: POSTGRES_PASSWORD value: mypassword - name: POSTGRES_DB value: mydb ports: - containerPort: 5432 volumeMounts: - mountPath: /var/lib/postgresql/data name: pg-data volumes: - name: pg-data persistentVolumeClaim: claimName: pg-data-claim
Thanks to all those who helped me :)
- does your custom postgresql persist data at /var/lib/postgresql/data?
- are you able to get logs from your postgresql container and spot anything interesting?
- when your pod is running, can you see the mountpoints inside your container and check the persistent disk is there?
I followed this scenario and I was able to persist my data by changing the mountPath to /var/lib/postgresql and also reproduced using cassandra (i.e. /var/lib/cassandra for mountPath)
I was able to delete/restart pods from different nodes/hosts and still see my "users" table and the data I previously entered. However, I was not using a custom image, I just used standard docker images.