【问题标题】:Connect Kubernetes Postgres pod to existing database stored on persistent volume将 Kubernetes Postgres pod 连接到存储在持久卷上的现有数据库
【发布时间】:2020-06-25 21:13:37
【问题描述】:

我正在尝试将在 Kubernetes 中运行的新 Wiki.js 应用程序实例连接到从以前的应用程序实例创建并存储在持久卷中的 Postgres 数据库。我得到一个CrashLoopBackOff 尝试启动 postgres pod,查看它们显示的日志:

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

initdb: directory "/var/lib/postgresql/data" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/var/lib/postgresql/data" or run initdb
with an argument other than "/var/lib/postgresql/data".

如您所见,它尝试调用 initdb 失败,因为已经存在数据...我的主要问题是如何配置以使用现有数据而不是尝试创建新数据库?

我的 kubernetes postgres 部署配置:

Name:                   postgres
Namespace:              wiki
CreationTimestamp:      Fri, 23 Aug 2019 22:17:44 +0000
Labels:                 app=postgres
Annotations:            deployment.kubernetes.io/revision: 36
Selector:               app=postgres
Replicas:               1 desired | 1 updated | 1 total | 0 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  1 max unavailable, 1 max surge
Pod Template:
  Labels:  app=postgres
  Containers:
   postgres:
    Image:      postgres:11.5
    Port:       5432/TCP
    Host Port:  0/TCP
    Environment:
      POSTGRES_USER:      <redacted>
      POSTGRES_PASSWORD:  <redacted>
      POSTGRES_DB:        postgresdb
    Mounts:
      /var/lib/postgresql/data from postgresdb (rw)
  Volumes:
   postgresdb:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  postgres-persistant-storage-postgres-0
    ReadOnly:   false

如前所述,只是尝试指向现有的 postgres 数据库,而不是让它尝试创建和初始化一个全新的数据库,任何帮助将不胜感激!


更新

当我进入 Postgres pod 并检查 /var/lib/postgresql/data 目录时,我确实看到了 PG_VERSION 文件以及一堆其他 db 文件(屏幕截图)。 PG_VERSION 文件本身包含字符串11,这很有意义,因为我们使用的是 Postgres 11.5。不幸的是,由于某种原因它仍然无法识别它:(

这也是我的 deployment.yaml 规范:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "60"
  creationTimestamp: "2019-08-23T22:17:44Z"
  generation: 184
  labels:
    app: postgres
  name: postgres
  namespace: wiki
  resourceVersion: "91329506"
  selfLink: /apis/extensions/v1beta1/namespaces/wiki/deployments/postgres
  uid: d441d0d8-c5f3-11e9-bc53-9676559ad387
spec:
  progressDeadlineSeconds: 2147483647
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: postgres
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: postgres
    spec:
      containers:
      - env:
        - name: POSTGRES_USER
          value: postgresadmin
        - name: POSTGRES_PASSWORD
          value: <redacted>
        - name: POSTGRES_DB
          value: wiki
        image: postgres:11.5
        imagePullPolicy: IfNotPresent
        name: postgres
        ports:
        - containerPort: 5432
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgresdb
      dnsPolicy: ClusterFirst
      hostname: postgrespod
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      subdomain: postgres
      terminationGracePeriodSeconds: 30
      volumes:
      - name: postgresdb
        persistentVolumeClaim:
          claimName: wiki-data

【问题讨论】:

  • 这其实和 Kubernetes 没什么关系,更多的是一个关于如何使用现有数据卷和postgres 容器镜像的问题。
  • 你能分享你的 postgres yaml 文件吗?
  • @Crou 刚刚将我的部署规范添加到帖子中,谢谢

标签: postgresql kubernetes azure-aks persistent-volumes wiki.js


【解决方案1】:

看起来图像检查的是一个名为$PGDATA/PG_VERSION 的匹配文件,以决定它是否需要初始化。见https://github.com/docker-library/postgres/blob/04f28840134bdaf72e4cd6e4a5300e9ceb5ea013/docker-entrypoint.sh#L211-L214

您必须使该文件存在于您的卷中。

【讨论】:

  • 感谢您的回复,不幸的是 PG_VERSION 文件存在(以及一堆其他 db 文件)。我用文件夹内容的附加信息和屏幕截图更新了我的帖子。再次感谢...
  • 所见即所得,跟踪启动脚本,看看它为什么调用 initdb。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-29
  • 2018-10-15
  • 2019-08-11
  • 2018-07-26
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
相关资源
最近更新 更多