简体   繁体   English

将 Kubernetes Postgres pod 连接到存储在持久卷上的现有数据库

[英]Connect Kubernetes Postgres pod to existing database stored on persistent volume

I am trying to connect a new Wiki.js app instance running in Kubernetes to a Postgres DB created from a prevous app instance and stored in a persistent volume.我正在尝试将在 Kubernetes 中运行的新 Wiki.js 应用程序实例连接到从先前应用程序实例创建并存储在持久卷中的 Postgres 数据库。 I get a CrashLoopBackOff try to start the postgres pod, looking at the logs they show:我得到一个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".

As you can see it tries to call initdb which fails since there is already data present... my main question is how can I configure to use the existing data instead of trying to create a new database ???如您所见,它尝试调用initdb失败,因为已经存在数据...我的主要问题是如何配置以使用现有数据而不是尝试创建新数据库???

My kubernetes postgres deployment configuration:我的 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

As mentioned simply trying to point to the existing postgres database instead of having it try to create and initialize a completely new one, any help would be greatly appreciated!如前所述,只是试图指向现有的 postgres 数据库,而不是让它尝试创建和初始化一个全新的数据库,任何帮助将不胜感激!


UPDATE更新

When I bash into the Postgres pod and examine the /var/lib/postgresql/data directory I do see the PG_VERSION file as well as a bunch of other db files (screenshot).当我猛击 Postgres pod 并检查/var/lib/postgresql/data目录时,我确实看到了 PG_VERSION 文件以及一堆其他 db 文件(屏幕截图)。 The PG_VERSION file itself contains the string 11 which makes sense since we're on Postgres 11.5. PG_VERSION 文件本身包含字符串11 ,这很有意义,因为我们使用的是 Postgres 11.5。 Unfortunately it still does not recognize it for some reason :(不幸的是,由于某种原因,它仍然无法识别它:(

在此处输入图片说明

Here is also my deployment.yaml spec:这也是我的 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

It looks like the thing the image checks for is a matching file named $PGDATA/PG_VERSION to decide if it needs to initialize or not.看起来图像检查的是一个名为$PGDATA/PG_VERSION的匹配文件,以决定它是否需要初始化。 See https://github.com/docker-library/postgres/blob/04f28840134bdaf72e4cd6e4a5300e9ceb5ea013/docker-entrypoint.sh#L211-L214https://github.com/docker-library/postgres/blob/04f28840134bdaf72e4cd6e4a5300e9ceb5ea013/docker-entrypoint.sh#L211-L214

You'll have to make that file exist in your volume.您必须使该文件存在于您的卷中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM