kubernetes 上的 Pgadmin4:将用户和设置保存在一个卷中

Pgadmin4 on kubernetes: saving users and settings in a volume

我想将用户帐户和其他设置保存在 pgadmin4 k8s 实例的卷中,我这样做了:

    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

问题是当我重新启动 pod 时,即使 pv 绑定到 pod,创建的用户也会消失,我不确定这部分:

              volumeMounts:
                - mountPath: /
                  name: pgadmin-storage

我想我必须指定保存用户信息和设置的目录,我尝试了默认目录 /pgadmin4 但是 pod 崩溃了。

我建议使用 this helm chart 安装 pgAdmin。有了它,您将能够配置持久化数据的方式:

  • 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.

此外,还有一些 pgAdmin 图表的可配置参数可以调整以配置您的持久性选项:

  • 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

根据@Wytrzymały 的回答,我检查了用 helm 创建的部署,发现正确的 mountPath/var/lib/pgadmin,该部分应该是这样的:

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

另一件事是我必须更改该目录的所有者以便应用程序可以写入它,我为此使用 InitContainers (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
....

希望这可以帮助到一些人