How to mount a postgresql volume using Aws EBS in Kubernete
Asked Answered
M

4

46
  1. I've created the persistent volume (EBS 10G) and corresponding persistent volume claim first. But when I try to deploy the postgresql pods as below (yaml file) :

enter image description here

Receive the errors from pod:

initdb: directory "/var/lib/postgresql/data" exists but is not empty It contains a lost+found directory, perhaps due to it being a mount point. Using a mount point directly as the data directory is not recommended. Create a subdirectory under the mount point.

Why the pod can't use this path? I've tried the same tests on minikube. I didn't meet any problem.

  1. I tried to change volume mount directory path to "/var/lib/test/data", the pods can be running. I created a new table and some data on it, and then killed this pod. Kubernete created a new pod. But the new one didn't preserve the previous data and table.

So what's the way to correctly mount a postgresql volume using Aws EBS in Kubernete, which allows the recreated pods can reuse initial data base stored in EBS?

Mesa answered 4/7, 2018 at 7:55 Comment(0)
I
92

So what's the way to correctly mount a postgresql volume using Aws EBS

You are on a right path...

Error you get is because you want to use root folder of mounted volume / as postgresql Data dir and postgresql complains that it is not best practice to do so since it is not empty and contains already some data inside (namely lost+found directory).

It is far better to locate data dir in separate empty subfolder (/postgres for example) and give postgresql clean slate when creating its file structure. You didn't get same thing on minicube since you most probably mounted host folder that didn't have anything inside (was empty) and didn't trigger such a complaint.

To do so, you would need initially empty subPath of your volume (empty /postgres subfolder on your PV for example) mounted to appropriate mount point (/var/lib/posgresql/data) in your pod. Note that you can name subPath and mount point end folder the same name, they are different here just as an example where test-db-volume/postgres folder would be mounted on pod to /var/lib/postgresql/data folder:

...
volumeMounts:
- mountPath: /var/lib/postgresql/data
  name: test-db-volume
  subPath: postgres
...
Initiatory answered 4/7, 2018 at 13:2 Comment(3)
This is a great answer. I tried many solutions including deleting lost+found after mounting but eventually found this simple working solution.World
This works for volumes on Azure Managed Disks (not Files) as wellSienna
Works for volumes on Okteto. Thanks!Domenic
G
14

I fixed this by telling postgres where i want the data base to be created with the PGDATA env. It creates the empty directory and inits the DB. If you dont have this then it assumes you want to create it in the room mount directory which for me had the ;ost+found directory that postgres did not like

containers:
  - name: postgres
    imagePullPolicy: Always
    image: postgres:9.6
    ports:
    - containerPort: 5432
      name: postgres
    env:
    - name: POSTGRES_DB
      value: "mydb"
    - name: PGDATA
      value: /var/lib/postgresql/data/pgdata
    volumeMounts:
      - mountPath: /var/lib/postgresql/data
        name: postgres-data
Gaze answered 20/3, 2019 at 14:41 Comment(0)
A
1

this is from dockerhub description...

PGDATA This optional variable can be used to define another location - like a subdirectory - for the database files. The default is /var/lib/postgresql/data, but if the data volume you're using is a filesystem mountpoint (like with GCE persistent disks), Postgres initdb recommends a subdirectory (for example /var/lib/postgresql/data/pgdata ) be created to contain the data.

so, create one more deeper dicerctory and it works

Atrabilious answered 23/5, 2019 at 16:43 Comment(0)
S
1

Use the subpath as described below

- name: postgredb
      mountPath: /var/lib/postgresql/data
     #setting subPath - it can be any arbitrary name
      subPath: postgres 
Sabra answered 5/10, 2022 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.