Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount a postgresql volume using Aws EBS in Kubernete

  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) :

test-postgresql.yaml

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?

like image 358
X.J Avatar asked Jul 04 '18 07:07

X.J


People also ask

How does volume work in Kubernetes?

A Volume in Kubernetes represents a directory with data that is accessible across multiple containers in a Pod. The container data in a Pod is deleted or lost when a container crashes or restarts, but when you use a volume, the new container can pick up the data at the state before the container crashes.


1 Answers

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 ... 
like image 67
Const Avatar answered Oct 07 '22 11:10

Const