将附加持久卷应用到 YAML 文件时 Pod 崩溃

Pod crashes when applying attaching persistent volume to YAML file

这是我的持久卷定义

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

这是我的持久卷声明

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

这是我要部署的 pod

apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage

当我尝试使用 kubectl create -f task-pv-pod.yaml 部署 pod 时,出现此错误

failed to start container "52c5f707bb90d87b4178e8508d710ae0912d8ee7bdd7c4b9b802bd6b35f266de": Error response from daemon: error while creating mount source path '/mnt/data': mkdir /mnt/data: read-only file system: RunContainerError

我正在遵循本指南 https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/,我有另一个 pod 运行 一个不同的应用程序,并且希望在我启动这个 pod 后向该 pod 应用持久存储 运行 .

同时定义 Persistent Volume you are using type: local . This means that you want to create directory in /mnt. Local do not support dynamic volume provisioning。例如,当您将通过 SSH 连接到任何节点时,您会发现该文件夹是 ReadOnly 文件系统。

/mnt $ mkdir something mkdir: cannot create directory ‘something’: Read-only file system

您只需更改您的 PV YAML

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/var/lib/data"

注意 accessMode 和路径的变化。 同样在您的 PVC 定义中更改:

  accessModes:
    - ReadWriteMany

请记住,必须删除旧的 PV 和 PVC(如果它们不会消失,您可能还需要重新部署 nginx pod),因为在某些资源中,您无法在创建后更改值。

看看:.

阅读:gke-dynamics-provisioning.