1

I am trying to start a postgres pod on microk8s kubernetes cluster. At the moment the postgres container with all its data is started locally on the host machine.

The question is: Is it possible to map the current volume (from local docker volume ) to the kubernetes pod deployment?

I have used kompose to convert the docker-compose.yml to appropriate .yaml files for kubernetes deployment.

The above mentioned command kompose creates postgres-deployment.yaml, postgres-service.yaml, and 2 persistantvolumeclaims ( from the volumes mapped in the docker-compose one for the pg_data and the other one for the init_db script).

Do I need to generate PersistantVolume mappings alongside the PersistantVolumeClaims that were automatically generated by kompose and how would they look?

EDIT: Using the yaml below I made 2 volumes and 2 volumeclaims for the postgres container one for the data one for the init_db script. Running that and then exposing the service endpoints worked. WARNING: Because the database was running on docker host machine container and kubernetes pod in same time data corruption happened.

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/var/lib/docker/volumes/dummy_pgdata/_data"
Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
LexByte
  • 382
  • 4
  • 15
  • Just to clarify - you want to mount existing docker volume (created independently of Kubernetes) with some data to Kubernetes pod? – Mikolaj S. Sep 01 '21 at 09:41
  • @MikolajS. yes. And i want that volume to be propagated( updated on every change) be that on the host or the kubernetes pod . – LexByte Sep 01 '21 at 18:52
  • 1
    Could you please share a Docker compose `yaml` configuration file? Which `kompose` command are you using to convert? – Mikolaj S. Sep 02 '21 at 08:29
  • I have edited the post above @WytrzymałyWiktor – LexByte Sep 07 '21 at 13:53
  • @MikolajS. i have used the kompose convert and provided the docker-compose file. Although i have made some adjustments to the volumes and volumeMounts. Using the Persistant volume from the edit above i have been able to mount the data from an already running postgres db but like i stated a data corruption happened because 2 databases tried to insert into same volume ( the snapshot from the /var/lib/docker/volumes/dummy_pgdata/_data wasnt synchronizing for both of the databases) – LexByte Sep 07 '21 at 13:59
  • That's normal behaviour. Check [this answer](https://stackoverflow.com/questions/43574863/is-postgres-designed-to-write-to-shared-data-stores/43583234#43583234). – Mikolaj S. Sep 10 '21 at 15:36

1 Answers1

2

Posted community wiki for better visibility. Feel free to expand it.


There is possibility to share same docker volume with the Kubernetes pod by defining custom PersistentVolume with storageClassName and hostPath set as below:

storageClassName: manual
hostPath:
  path: "/var/lib/docker/volumes/{docker-volume}"

However, there is no possibility to share the same Postgres data files between different Postgres installations. It may cause data corruption. Check this answer for more details.

Mikolaj S.
  • 2,850
  • 1
  • 5
  • 17