<p>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.</p>
<p>The question is: Is it possible to map the current volume (from local docker volume ) to the kubernetes pod deployment?</p>
<p>I have used <code>kompose</code> to convert the <code>docker-compose.yml</code> to appropriate <code>.yaml</code> files for kubernetes deployment.</p>
<p>The above mentioned command <code>kompose</code> creates <code>postgres-deployment.yaml</code>, <code>postgres-service.yaml</code>, and 2 <code>persistantvolumeclaims</code> ( from the volumes mapped in the docker-compose one for the pg_data and the other one for the init_db script).</p>
<p>Do I need to generate <code>PersistantVolume</code> mappings alongside the <code>PersistantVolumeClaims</code> that were automatically generated by <code>kompose</code> and how would they look?</p>
<p>EDIT: Using the yaml below I made 2 <code>volumes</code> and 2 <code>volumeclaims</code> 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.</p>
<pre><code>kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: &quot;/var/lib/docker/volumes/dummy_pgdata/_data&quot;
</code></pre>

LexByte
<p>Posted community wiki for better visibility. Feel free to expand it.</p>
<hr />
<p>There is possibility to share same docker volume with the Kubernetes pod by <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#create-a-persistentvolume" rel="nofollow noreferrer">defining custom PersistentVolume</a> with <code>storageClassName</code> and <code>hostPath</code> set as below:</p>
<pre><code>storageClassName: manual
hostPath:
  path: &quot;/var/lib/docker/volumes/{docker-volume}&quot;
</code></pre>
<p><strong>However, there is no possibility to share the same Postgres data files between different Postgres installations.</strong> It may cause data corruption. Check <a href="https://stackoverflow.com/a/43583234/16391991">this answer</a> for more details.</p>
