如何在Kubernetes中挂载PostgreSQL数据目录?

时间:2018-08-25 10:26:14

标签: postgresql docker kubernetes minikube

我正在使用minikube在本地运行kubernetes。我的本地k8有两个Pod,其中一个是PostgreSQL,另一个是我自己的应用程序。我已经安装了PersistentVolumePersistentVolumeClaim以便为PostgreSQL创建有状态的容器:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/psql"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

这是PostgreSQL部署yaml文件:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: postgres
    spec:
      containers:
      - name: postgres
        imagePullPolicy: Never
        image: postgres:9.6
        ports:
        - name: postgres
          containerPort: 5432
        volumeMounts:
        - mountPath: /var/lib/postgresql
          name: postgres-persistent-storage
      volumes:
      - name: postgres-persistent-storage
        persistentVolumeClaim:
          claimName: postgres-pv-claim

问题是PostgreSQL服务无法启动,并且当我运行其pod时会发生此错误:

Error: /var/lib/postgresql/9.6/main is not accessible; please fix the directory permissions (/var/lib/postgresql/9.6/ should be world readable)
No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning).

我检查了PostgreSQL pod的内部,发现/var/lib/postgresql是空的,就像minikube主机中的/data/psql一样。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

更改:

volumeMounts:
  - mountPath: /var/lib/postgresql

volumeMounts:
  - mountPath: /var/lib/postgresql/data

使用错误的mountPoint postgres可执行文件被覆盖。

我将图像与从吊舱内部(左侧)和minikube空间(右侧,虚拟盒中的小外壳)中看到的数据相连接。

enter image description here