简体   繁体   English

Postgres on Azure kube.netes卷权限错误

[英]Postgres on Azure kubernetes volume permission error

I'm trying to deploy Postgresql on Azure Kube.netes with data persistency.我正在尝试在具有数据持久性的 Azure Kube.netes 上部署 Postgresql。 So I'm using PVC.所以我正在使用 PVC。 I searched lots of posts on here, most of them offered yaml files like below, but it's giving the error below;我在这里搜索了很多帖子,其中大多数提供了如下所示的 yaml 文件,但它给出了以下错误;

chmod: changing permissions of '/var/lib/postgresql/data/pgdata': 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 change permissions of directory "/var/lib/postgresql/data/pgdata": Operation not permitted
fixing permissions on existing directory /var/lib/postgresql/data/pgdata ...

deployment yaml file is below;部署 yaml 文件如下;

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgresql
  template:
    metadata:
      labels:
        app: postgresql
    spec:
      containers:
        - name: postgresql
          image: postgres:13.2
          securityContext:
            runAsUser: 999
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - secretRef:
                name: postgresql-secret
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb-kap
      volumes:
        - name: postgredb-kap
          persistentVolumeClaim:
            claimName: postgresql-pvc

Secret yaml is below;下面是秘密yaml;

apiVersion: v1
kind: Secret
metadata:
  name: postgresql-secret
type: Opaque
data:
  POSTGRES_DB: a2V5sd4=
  POSTGRES_USER: cG9zdGdyZXNhZG1pbg==
  POSTGRES_PASSWORD: c234Rw==
  PGDATA: L3Za234dGF0YQ==

pvc and sc yaml files are below: pvc 和 sc yaml 文件如下:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgresql-pvc
  labels:
    app: postgresql
spec:
  storageClassName: postgresql-sc
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
---
allowVolumeExpansion: true
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: postgresql-sc
mountOptions:
- dir_mode=0777
- file_mode=0777
- uid=1000
- gid=1000
parameters:
  skuName: Standard_LRS
provisioner: kubernetes.io/azure-file
reclaimPolicy: Retain

So when I use the mountpath like " - mountPath: /var/lib/postgresql/ ", it's working.因此,当我使用像“ - mountPath: /var/lib/postgresql/ ”这样的安装路径时,它正在工作。 I can reach the DB and it's good.我可以联系到数据库,这很好。 But when I delete the pod and recreating, there is no DB.但是当我删除 pod 并重新创建时,没有数据库。 So no data persistency.所以没有数据持久性。

Can you please help, what am I missing here?你能帮忙吗,我在这里错过了什么?

Thanks!谢谢!

One thing you could try is to change uid=1000,gid=1000 in mount options to 999 since this is the uid of postgres user in postgres conatiner (I didn't test this).您可以尝试的一件事是将挂载选项中的uid=1000,gid=1000更改为 999,因为这是 postgres conatiner 中的 postgres 用户的 uid(我没有对此进行测试)。


Another solution that will for certain solve this issue involves init conatainers.可以肯定解决此问题的另一种解决方案涉及 init 容器。

Postgres container requires to start as root to be able to chown pgdata dir since its mounted as root dir. Postgres 容器需要以 root 身份启动才能chown pgdata 目录,因为它安装为 root 目录。 After it does this, it drops root permisions and runs as postgres user.完成此操作后,它会删除 root 权限并以 postgres 用户身份运行。

But you can use init container (running as root) to chmod the volume dir so that you can run main container as non-root.但是您可以使用 init 容器(以 root 身份运行)对卷目录进行 chmod,以便您可以以非 root 身份运行主容器。

Here is an example:这是一个例子:

  initContainers:
    - name: init
      image: alpine
      command: ["sh", "-c", "chown 999:999 /var/lib/postgresql/data"]
      volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgredb-kap

Based on the helpful answer from Matt.基于马特的有用答案。 For bitnami postgresql the initContainer also works but with a slightly different configuration:对于 bitnami postgresql,initContainer 也可以工作,但配置略有不同:

      initContainers:
        - name: init
          image: alpine
          command: ["sh", "-c", "chown 1001:1001 /bitnami/postgresql"]
          volumeMounts:
            - mountPath: /bitnami/postgresql
              name: postgres-volume

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

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