【问题标题】:Minikube mount postgres data folderMinikube 挂载 postgres 数据文件夹
【发布时间】:2019-10-07 20:19:49
【问题描述】:

我的目标是在 minikube 上运行 postgres,/var/lib/postgresql/data 是从我的笔记本电脑安装的。 我关注了很多关于如何到达那里的帖子,但还没有成功,我得到的最接近的是这里:

首先,我将本地 /data/ 挂载到 minikube 并确认 - 据我了解,/data 具有 root 权限,因此我需要执行 sudo mkdir -p /data/postgres-pvsudo cp -R <source_path>/data/* /data/postgres-pv 将数据复制到那里。

我的 PV 是:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv
  namespace: demo
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/postgres-pv

为了调试,我能够运行一个busybox pod并确保我看到了正确的数据,我厌倦了在我的笔记本电脑上的/data/postgres-pv添加文件并立即使用以下命令在busybox安装路径中看到它们声明:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pvc
  namespace: demo
  labels:
    type: local
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  volumeName: postgres-pv

我用来调试的busybox:

kind: Pod
apiVersion: v1
metadata:
  name: busybox
  namespace: demo
spec:
  containers:
    - name: busybox
      image: busybox
      command:
        - sleep
        - "3600"
      volumeMounts:
      - mountPath: "/data"
        name: postgres-pvc
  volumes:
    - name: postgres-pvc
      persistentVolumeClaim:
        claimName: postgres-pvc

当我尝试将相同的文件夹从本地笔记本电脑加载到运行 postgres 的 pod 中并覆盖 /var/lib/postgresql/data 时出现错误,我尝试了以下不同的变体,包括 subPath 和 PGDATA 变量,在许多情况下都可以看到帖子(例如here

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: admindb
  namespace: demo # should be replaced
spec:
  template:
    metadata:
      labels:
        app: admindb
    spec:
      containers:
      - name: postgres
        image: postgres:9.6.5
        ports:
        - containerPort: 5432
        env:
#        - name: POSTGRES_DB
#          valueFrom:
#            secretKeyRef:
#              name: admindb-secret-config
#              key: dbname
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: postgres-credentials
              key: user
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-credentials
              key: password
#        - name: PGDATA # overriding the default mount so we can load our own data from PVC
#          value: /var/lib/postgresql/data/pgdata/
        volumeMounts:
          - mountPath: /var/lib/postgresql/data
            name: postgres-pvc
#            subPath: pgdata
      volumes:
        - name: postgres-pvc
          persistentVolumeClaim:
            claimName: postgres-pvc

我检查日志时得到的错误是:

+ kubectl logs -n demo admindb-546d55d9b5-ddr4f
chown: cannot read directory ‘/var/lib/postgresql/data/pg_multixact’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/pgdata/pgdata’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/pgdata’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_wal’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_snapshots’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_commit_ts’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_stat’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/PG_VERSION’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_stat_tmp’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/pg_hba.conf’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postmaster.pid’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_logical’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_notify’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_subtrans’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_serial’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_replslot’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postgresql.conf’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postgres/pgdata’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postgres’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_tblspc’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postgresql.auto.conf’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_twophase’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_xact’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/pg_dynshmem’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/postmaster.opts’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/pg_ident.conf’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/global’: Input/output error
chown: cannot read directory ‘/var/lib/postgresql/data/base’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data’: Input/output error

如果我修改部署以包含

- name: PGDATA
  value: /var/lib/postgresql/data/pgdata/

然后错误变为:

+ kubectl logs -n demo admindb-6dc94659dd-4kc9t
chown: changing ownership of ‘/var/lib/postgresql/data/pgdata/pgdata’: Input/output error
chown: changing ownership of ‘/var/lib/postgresql/data/pgdata/’: Input/output error

确实感觉我在所有不同的帖子之间兜圈子,而在这个过程中错过了一些基本的东西,非常感谢任何帮助或指向正确方向的点 - 感觉我需要在我的部署,它会工作!或更改我笔记本电脑上的权限。

【问题讨论】:

    标签: postgresql minikube


    【解决方案1】:

    将您的 volumeMounts 更改为:

            volumeMounts:
              - mountPath: /var/lib/postgresql/something
                name: postgres-pvc
    

    您可以阅读PostgreSQL 9.6部分18.2. Creating a Database Cluster的文档:

    18.2.1。使用辅助文件系统

    许多安装在计算机的“根”卷之外的文件系统(卷)上创建它们的数据库集群。如果您选择这样做,建议不要尝试使用辅助卷的最顶层目录(安装点)作为数据目录。最佳实践是在 PostgreSQL 用户拥有的挂载点目录中创建一个目录,然后在其中创建数据目录。这样可以避免权限问题,尤其是对于 pg_upgrade 等操作,并且还可以确保在辅助卷脱机时彻底失败。

    【讨论】:

    • stackoverflow.com/users/3156333/crou 太好了!!您能否评论使该/某事成为主要数据的最佳做法?即在我连接到数据库后,我仍然看不到我预先填充的架构,我的第一个猜测是修改 postgres.conf 以将 /something 指向数据目录,除非你有任何其他指针
    • 我认为你应该使用 initContainer 从 PV 加载架构,或者创建一个 ConfigMap 来存储你的 postgres.conf 文件。
    猜你喜欢
    • 2021-08-05
    • 2017-10-11
    • 2021-12-04
    • 2019-04-22
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2016-05-31
    相关资源
    最近更新 更多