Search code examples
postgresqlkubernetespgadmin-4persistent-volumes

Pgadmin4 on kubernetes: saving users and settings in a volume


I want to save users accounts and other settings in a volume for a pgadmin4 k8s instance, I did that:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: pgadmin
      namespace: pgadmin
    spec:
      selector:
       matchLabels:
        app: pgadmin
      replicas: 1
      template:
        metadata:
          labels:
            app: pgadmin
        spec:
          containers:
            - name: pgadmin4
              image: dpage/pgadmin4
              env:
               - name: PGADMIN_DEFAULT_EMAIL
                 value: "admin@example.com"
               - name: PGADMIN_DEFAULT_PASSWORD
                 value: "mysecpwd"
               - name: PGADMIN_PORT
                 value: "80"
              ports:
                - containerPort: 80
                  name: pgadminport
              volumeMounts:
                - mountPath: /
                  name: pgadmin-storage

          volumes:
               - name: pgadmin-storage
                 persistentVolumeClaim:
                   claimName: pgadmin-pv-claim
    ---
        kind: PersistentVolume
        apiVersion: v1
        metadata:
          name: pgadmin-pv-volume
          namespace: pgadmin
          labels:
            type: local
            app: pgadmin
        spec:
          storageClassName: manual
          capacity:
            storage: 5Gi
          accessModes:
            - ReadWriteMany
          hostPath:
            path: "/mnt/data"
    ---
        kind: PersistentVolumeClaim
        apiVersion: v1
        metadata:
          name: pgadmin-pv-claim
          namespace: pgadmin
          labels:
            app: pgadmin
        spec:
          storageClassName: manual
          accessModes:
            - ReadWriteMany
          resources:
            requests:
              storage: 5Gi

The problem is when I restart the pod the created users disappears even if the pv is bounded to the pod, I'm not sure about this section:

              volumeMounts:
                - mountPath: /
                  name: pgadmin-storage

I think I have to specify the directory where the user's information and settings are saved, I tried the default directory /pgadmin4 but the pod crashes.


Solution

  • based on @Wytrzymały answer, I checked the deployment created with helm, and I found that the correct mountPath is /var/lib/pgadmin, the section should be like so:

          ...
          volumeMounts:
            - mountPath: /var/lib/pgadmin
              name: pgadmin-storage
          ...
    

    another thing is that I had to change owner of that directory so the application can write to it, I used InitContainers for that (pgadmin uid = 5050):

    ...
    spec:
      initContainers:
        - name: volume-mount-hack
        image: busybox
        command: ["sh", "-c", "chown -R 5050:5050 /var/lib/pgadmin"]
        volumeMounts:
          - name: pgadmin-storage
            mountPath: /var/lib/pgadmin
    ....
    

    hope this can help somebody