3

I have a postgres pod deployed on OpenShift and a PVC that I think that I correctly attached but I could be wrong. This is my PVC and it is correctly Bound -

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
 name: postgres-pvc
spec:
 accessModes:
   - ReadWriteOnce
 resources:
   requests:
     storage: 1Gi
 storageClassName: xxxxx

I created the postgres pods with a PGDATA env set to /var/lib/postgresql/pgdata and mounted the PVC like so -

oc set volume dc/postgres --add --name=postgres-pvc --type=persistentVolumeClaim \
    --claim-name=postgres-pvc --mount-path=/var/lib/postgresql/pgdata --containers=postgres

I originally tried to attach the PVC to /var/lib/postgresql/data by overwriting the original container volume but it said there was issues with something like mounting directly to that data folder path so that's why I used the pgdata one.

oc set volume dc/postgres --add --overwrite --name=postgres-volume-1 --type=persistentVolumeClaim \
  --claim-name=postgres-pvc --mount-path=/var/lib/postgresql/data --containers=postgres

The error that I get now is when I try to scale up the pod/add a replica via the DC, it gives the following errors -

Unable to attach or mount volumes: unmounted volumes=[postgres-pvc], unattached volumes=[postgres-volume-1 postgres-pvc postgres-token-h7jvr]: timed out waiting for the condition

and

Error while attaching the device pv pvc-b87b49ff-2bce-495c-b17f-b45f51eab27b cannot be attached to the node xx.xx.xxx.xx. Error: PV pvc-b87b49ff-2bce-495c-b17f-b45f51eab27b is already attached to another node xx.xx.xxx.x and there are active pods [postgres-7-6p6sz] using that

Is it because I did an incorrect mount of my PVC? Or do I need to create a new PVC and then manually update the new pod to that newly created PVC?

animusdx
  • 380
  • 3
  • 16

1 Answers1

4

as you can see the error

already attached to another node xx.xx.xxx.x and there are active pods [postgres-7-6p6sz] using that

you are utilizing a block storage as pv which has accessModes of ReadWriteOnce that means at any given time volume could be attached to a single kubernetes node, on that node then, a pod can mount it. now if you want to attach to another pod, you need to remove the existing pod which will make pv to unattach from previous node, reattach to the new node.

for further details persistent-volumes/

Suresh Vishnoi
  • 17,341
  • 8
  • 47
  • 55
  • Yeah I see that now after doing some digging. I was trying to create a new PVC with `ReadWriteMany` but it turns out that the block storage class only supports `ReadWriteOnce`. Hmm, you don't happen to have any clarity on what I should do for the purposes of HA? Should I just create new `ReadWriteOnce` PVCs and then manually attach them for each new replica? – animusdx Apr 28 '21 at 19:27
  • 1
    I think, this is the caveat with ReadWriteOnce, I think I would suggest you to checkout statefulset which create a pod and its own pvc. – Suresh Vishnoi Apr 28 '21 at 19:35
  • 2
    Thanks, guess I need to do a lot more reading to understand the technicalities of it. In essence when using StatefulSets these replicas are just standby replicas correct? Since they're each using their own PVC do I need to write some type of side container or something that replicates data between these pods? – animusdx Apr 28 '21 at 20:17
  • no worreis:) that’s where intelligence of application matter. If stateful application itself can not replicate the data among its peer or cluster then there are some are kind of meta application which assist to achieve this goal. I would suggest you read about operator pattern and specific operator to postgres. – Suresh Vishnoi Apr 29 '21 at 06:02
  • It might be useful https://postgres-operator.readthedocs.io/en/latest/ – Suresh Vishnoi Apr 29 '21 at 06:03