mkdir /mnt/data: 只读文件系统退避重启失败的 postgres 容器

mkdir /mnt/data: read-only file system Back-off restarting failed postgres container

我是 Kubernetes 的新手,我尝试应用 yaml 文件在 GKE 中创建 Postgres,我收到错误消息“错误:无法启动容器“postgres”:来自守护进程的错误响应:创建挂载时出错源路径“/mnt/data”:mkdir /mnt/data:只读 文件系统退避重启失败的容器。

我认为我需要将权限授予 RWX,当我尝试登录到 pod,即在容器内时。它不允许登录。 任何人都请帮助我!!

这是我的 Postgres Yaml 文件:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: postgres
    spec:
      selector:
       matchLabels:
        app: postgres
      replicas: 1
      template:
        metadata:
          labels:
            app: postgres
        spec:
          containers:
            - name: postgres
              image: postgres:latest
              imagePullPolicy: "IfNotPresent"
              envFrom:
                - configMapRef:
                    name: postgres-config
              volumeMounts:
                - mountPath: /var/lib/postgresql/data
                  name: postgredb
          volumes:
            - name: postgredb
              persistentVolumeClaim:
                claimName: postgres-pv-claim

---
    kind: PersistentVolume
    apiVersion: v1
    metadata:
      name: postgres-pv-volume
      labels:
        type: local
        app: postgres
    spec:
      storageClassName: manual
      capacity:
        storage: 5Gi
      accessModes:
        - ReadWriteMany
      hostPath:
        path: "/mnt/data"
---
   

     kind: PersistentVolumeClaim
        apiVersion: v1
        metadata:
          name: postgres-pv-claim
          labels:
            app: postgres
        spec:
          storageClassName: manual
          accessModes:
            - ReadWriteMany
          resources:
            requests:
              storage: 5Gi
        
    ---
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: postgres-config
          labels:
            app: postgres
        data:
          POSTGRES_DB: postgresdb
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: root
    ---
        apiVersion: v1
        kind: Service
        metadata:
          name: postgres
          labels:
            app: postgres
        spec:
           ports:
            - name: postgres
              port: 5432
              nodePort: 30432
           type: NodePort
           selector:
            app: postgres

在你的 Persistent Volume you are using type: local which means that you want to create directory in /mnt. Local also do not support dynamic volume provisioning 中。如果你将 SSH 连接到你的任何节点,你会发现这个文件夹是 ReadOnly file system.

/mnt $ mkdir 东西 mkdir:无法创建目录‘something’:只读文件系统

作为最快的解决方法,您只需更改 PV YAML

    - ReadWriteMany
  hostPath:
    path: /mnt/data

收件人:

    - ReadWriteMany
  hostPath:
    path: /var/lib/data

示例:

$ kubectl apply -f pv-pvc.yaml
persistentvolume/postgres-pv-volume created
persistentvolumeclaim/postgres-pv-claim created
$ kubectl apply -f pos.yaml
deployment.apps/postgres created
$ kubectl get po
NAME                        READY   STATUS    RESTARTS   AGE
postgres-65d9cbd495-pcqf5   1/1     Running   0          2s

$ kubectl exec -ti postgres-65d9cbd495-pcqf5 -- /bin/bash
root@postgres-65d9cbd495-pcqf5:/# cd /var/lib/postgresql/data
root@postgres-65d9cbd495-pcqf5:/var/lib/postgresql/data# ls
base    pg_commit_ts  pg_hba.conf    pg_logical    pg_notify    pg_serial     pg_stat      pg_subtrans  pg_twophase  pg_wal   postgresql.auto.conf  postmaster.opts
global  pg_dynshmem   pg_ident.conf  pg_multixact  pg_replslot  pg_snapshots  pg_stat_tmp  pg_tblspc    PG_VERSION   pg_xact  postgresql.conf       postmaster.pid
root@postgres-65d9cbd495-pcqf5:/var/lib/postgresql/data# echo "Hello from postgress pod" > data.txt
root@postgres-65d9cbd495-pcqf5:/var/lib/postgresql/data# cat data.txt
Hello from postgress pod

现在,如果您将通过 SSH 连接到托管此 pod 的节点,您将能够访问此文件夹和文件。

user@gke-cluster-1-default-pool-463f9615-gxhl ~ $ sudo su
gke-cluster-1-default-pool-463f9615-gxhl /home/user # cd /var/lib/data
gke-cluster-1-default-pool-463f9615-gxhl /var/lib/data # ls
PG_VERSION    pg_dynshmem    pg_notify     pg_stat_tmp  pg_xact
base          pg_hba.conf    pg_replslot   pg_subtrans  postgresql.auto.conf
data.txt      pg_ident.conf  pg_serial     pg_tblspc    postgresql.conf
global        pg_logical     pg_snapshots  pg_twophase  postmaster.opts
pg_commit_ts  pg_multixact   pg_stat       pg_wal       postmaster.pid
gke-cluster-1-default-pool-463f9615-gxhl /var/lib/data # cat data.txt 
Hello from postgress pod

编辑

我用过 YAML。

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: postgresdb
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: root
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: NodePort
  selector:
    app: postgres
  ports:
  - name: postgres
    port: 5432
    nodePort: 30432
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    app: postgres
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /var/lib/data
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  labels:
    app: postgres
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:latest
          imagePullPolicy: "IfNotPresent"
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
            
configmap/postgres-config created
service/postgres created
persistentvolume/postgres-pv-volume created
persistentvolumeclaim/postgres-pv-claim created
deployment.apps/postgres created

$ kubectl get po
NAME                        READY   STATUS    RESTARTS   AGE
postgres-65d9cbd495-wxx4h   1/1     Running   0          19s

如果您使用的是 GKE,只需创建 PVC,它会自行创建 PV,根据您的需要工作。

我这样解决了我的问题。