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

时间:2020-03-13 21:35:30

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

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

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窗格并检查/var/lib/postgresql/data目录时,我确实看到了PG_VERSION文件以及许多其他数据库文件(屏幕截图)。 PG_VERSION文件本身包含字符串11,因为我们使用的是Postgres 11.5,所以这很有意义。不幸的是,由于某些原因,它仍然无法识别它:(

enter image description here

这也是我的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

1 个答案:

答案 0 :(得分:0)

该图像所检查的东西似乎是一个名为$PGDATA/PG_VERSION的匹配文件,用于确定是否需要初始化。参见https://github.com/docker-library/postgres/blob/04f28840134bdaf72e4cd6e4a5300e9ceb5ea013/docker-entrypoint.sh#L211-L214

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