首页 > 解决方案 > 用于 PgAdmin 的 Kubernetes 持久卷挂载

问题描述

我正在尝试为我的 pgadmin 部署创建一个持久卷声明,以便在每次从 CD 管道部署后推出更新时,我可以保留我的设置、服务器等。

在我的日志中,我收到以下错误:

...
[2020-10-05 00:54:56 +0000] [91] [INFO] Worker exiting (pid: 91)
WARNING: Failed to set ACL on the directory containing the configuration database:
           [Errno 1] Operation not permitted: '/var/lib/pgadmin'
HINT   : You may need to manually set the permissions on
         /var/lib/pgadmin to allow pgadmin to write to it.
ERROR  : Failed to create the directory /var/lib/pgadmin/sessions:
           [Errno 13] Permission denied: '/var/lib/pgadmin/sessions'
HINT   : Create the directory /var/lib/pgadmin/sessions, ensure it is writeable by
         'pgadmin', and try again, or, create a config_local.py file
         and override the SESSION_DB_PATH setting per
         https://www.pgadmin.org/docs/pgadmin4/4.26/config_py.html

只是一堆写权限失败:

PGAdmin 部署

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgadmin
spec:
  selector:
   matchLabels:
    app: pgadmin
  replicas: 1
  template:
    metadata:
      labels:
        app: pgadmin
    spec:
      containers:
        - name: pgadmin4
          image: dpage/pgadmin4
          volumeMounts:
            - mountPath: /var/lib/pgadmin
              name: pgadminstorage
          env:
           - name: PGADMIN_DEFAULT_EMAIL
             valueFrom:
               secretKeyRef:
                 name: un
                 key: un
           - name: PGADMIN_DEFAULT_PASSWORD
             valueFrom:
               secretKeyRef:
                 name: pw 
                 key: pw
           - name: PGADMIN_PORT
             value: "80"
          ports:
            - containerPort: 80
              name: pgadminport
      volumes:
        - name: pgadminstorage
          persistentVolumeClaim:
            claimName: pgadmin-persistent-volume-claims-cfg

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pgadmin-persistent-volume-claims-cfg
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

我在这里能错过什么?

更新:

这可能是 digitalocean 特有的问题并且无法设置权限。PVC 会将 perms 设置为 root,但是以 pgadmin 的形式写入会导致启动时出现问题将此添加到我的 pgadmin 部署中修复了所有问题

      initContainers:
        - name: pgadmin-data-permission-fix
          image: busybox
          command: ["/bin/chown", "-R", "5050:5050", "/var/lib/pgadmin"]
          volumeMounts:
          - name: pgadminstorage
            mountPath: /var/lib/pgadmin

您也可以在目录上进行 chmod 递归,也可以。

标签: postgresqlkubernetespgadmin

解决方案


我已经复制了你的问题。根本原因是PgAdmin问题,而不是 Kubernetes。Pod 将毫无问题地部署。您将收到错误,因为容器将无法在文件夹中创建文件夹/var/lib。如果您要检查pgadminpod 日志 -kubectl logs <pgadmin-pod>您将看到如下错误:

$ kubectl logs pgadmin-d569b67fd-8rnkc
WARNING: Failed to set ACL on the directory containing the configuration database:
           [Errno 1] Operation not permitted: '/var/lib/pgadmin'
HINT   : You may need to manually set the permissions on
         /var/lib/pgadmin to allow pgadmin to write to it.
ERROR  : Failed to create the directory /var/lib/pgadmin/sessions:
           [Errno 13] Permission denied: '/var/lib/pgadmin/sessions'
HINT   : Create the directory /var/lib/pgadmin/sessions, ensure it is writeable by
         'pgadmin', and try again, or, create a config_local.py file
         and override the SESSION_DB_PATH setting per
         https://www.pgadmin.org/docs/pgadmin4/4.26/config_py.html
sudo: setrlimit(RLIMIT_CORE): Operation not permitted

如果您要检查/var/lib/文件夹权限,您将看到您只能ReadExecute,因此您将无法在此文件夹中创建任何内容(默认情况下,您将以pgadmin用户身份登录)。

drwxr-xr-x    1 root     root          4096 Sep  5 14:01 lib

根据您的需要,您可以通过几种方式解决它。作为最快的解决方法,您只需将路径更改为允许的文件夹Write,例如tmp.

drwxrwxrwt    1 root     root          4096 Oct  5 14:28 tmp

YAML看起来像:

  containers:
    - name: pgadmin4
      image: dpage/pgadmin4
      volumeMounts:
        - mountPath: /var/tmp/pgadmin
          name: pgadminstorage

当您检查日志时,不会有任何问题。

$ kubectl logs pgadmin-6bb74cffb8-6q9tr
NOTE: Configuring authentication for SERVER mode.

sudo: setrlimit(RLIMIT_CORE): Operation not permitted
[2020-10-05 14:28:15 +0000] [1] [INFO] Starting gunicorn 19.9.0
[2020-10-05 14:28:15 +0000] [1] [INFO] Listening at: http://[::]:80 (1)
[2020-10-05 14:28:15 +0000] [1] [INFO] Using worker: threads
/usr/local/lib/python3.8/os.py:1023: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
  return io.open(fd, *args, **kwargs)
[2020-10-05 14:28:15 +0000] [89] [INFO] Booting worker with pid: 89
user@cloudshell:~/pgadmin (project)$

关于PgAdmin权限问题,已经有一些主题StackOverflowGithub类似: OSError: [Errno 13] Permission denied: '/var/lib/pgadmin'

pgadmin 退出代码 3 PermissionError: [Errno 13] Permission denied: '/var/lib/pgadmin/sessions'

/var/lib/pgadmin/sessions 中的 [stable/pgadmin] 文件使 pod 崩溃

简而言之,您可以尝试手动更改权限或使用特定用户。

另外,如果您使用云环境,您可以考虑使用CloudSQL,而不是尝试将数据库放入云端。例如PostgreSQL with GKE

编辑

根据此答案下方的@Ryan 评论,您还可以使用Init Containers更改/var/lib/权限。每个都init container必须在下一个开始之前成功完成,并且在pod.

在 Pod 中的应用程序容器之前运行的专用容器。初始化容器可以包含应用程序映像中不存在的实用程序或设置脚本。


推荐阅读