Pgadmin4 on kubernetes: saving users and settings in a volume

3.9k views Asked by At

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: "[email protected]"
               - 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.

2

There are 2 answers

0
Khalil Meg On BEST ANSWER

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

1
Wytrzymały Wiktor On

I suggest installing pgAdmin with this helm chart. With it you will be able to Configure the way how to persistent data:

  • Disable: The data does not survive the termination of a pod.

  • Persistent Volume Claim(default): A default StorageClass is needed in the Kubernetes cluster to dynamic provision the volumes. Specify another StorageClass in the storageClass or set existingClaim if you have already existing persistent volumes to use.

Also, there are some configurable parameters of the pgAdmin chart that can be adjusted in order to configurate you persistance options:

  • persistence.enabled: Enable the data persistence or not

  • persistence.existingClaim: Provide an existing PersistentVolumeClaim, the value is evaluated as a template

  • persistence.storageClass: PVC Storage Class for PostgreSQL volume

  • persistence.accessMode: The access mode of the volume

  • persistence.size: The size of the volume