kubernetes Minikube - Permission denied to access Postgres data /var/lib/postgresql/data

wkyowqbh  于 2023-06-21  发布在  Kubernetes
关注(0)|答案(1)|浏览(138)

postgres pod的日志显示权限错误:

chmod: changing permissions of '/var/lib/postgresql/data': Operation not permitted
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: error: could not access directory "/var/lib/postgresql/data": Permission denied

下面是我的pg.yaml清单:

apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: ClusterIP
  ports:
    - port: 5432
  selector:
    app: postgres
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /mnt/data
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:14
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_DB
              value: postgres
            - name: POSTGRES_USER
              value: admin
            - name: POSTGRES_PASSWORD
              value: admin
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb 
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim

我安装了本地路径,我想像这样持久化postgres数据:

minikube mount /home/myuser/myproject/data:/mnt/data

我试着按照另一篇文章的建议添加子路径,但没有帮助。我错过了什么?谢谢你!

编辑

Per @DavidMaze我测试了这个yaml文件。我没有得到权限错误,但postgres数据不持久。下面是文件:

apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: ClusterIP
  ports:
    - port: 5432
  selector:
    app: postgres

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      securityContext:
        runAsUser: 0
      containers:
        - name: postgres
          image: postgres:14
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_DB
              value: postgres
            - name: POSTGRES_USER
              value: admin
            - name: POSTGRES_PASSWORD
              value: admin
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb 
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
x8diyxa7

x8diyxa71#

问题是您根据指定的mountPath挂载了一个不同的文件夹。
因此,如果你像问题中那样在Minikube中挂载,请相应地编辑mountPath:

volumeMounts:
  - mountPath: /mnt/data
    name: postgredb

然后,检查文件夹/mnt/data是否存在并创建它:

mkdir /mnt/data

设置正确的权限并将您的用户名添加为所有者:

sudo chmod 775 /mnt/data
sudo chown $(whoami) /mnt/data

最后初始化数据库initdb /mnt/data

相关问题