首页 > 解决方案 > 如何通过 pvc 在 kubernetes 中挂载数据文件?

问题描述

我想通过 pvc 在 kubernetes 中使用 glusterfs 持久化数据文件,我挂载了目录并且它会工作,但是当我尝试挂载文件时,它会失败,因为文件已挂载到目录类型,我该怎么做在k8s中挂载数据文件?

图片信息:错误日志

pod yaml 文件

标签: kuberneteskubernetes-pvc

解决方案


如何在 k8s 中挂载数据文件?

这通常是特定于应用程序的,有几种方法可以做到这一点,但主要是您想了解subPath

通常,您可以选择:

  • 使用subPath分隔配置文件。
  • 将卷/路径作为目录挂载到其他位置,然后将文件链接到 pod 中的特定位置(在极少数情况下,与其他配置文件或同一目录中的目录权限混合会出现问题,或者应用程序的启动/启动策略会阻止文件在 pod 启动时安装,但在执行一些初始化后需要存在,真的是边缘情况)。
  • 使用ConfigMaps(甚至Secrets)来保存配置文件。请注意,如果使用带有 configMap 和 Secret pod 的 subPath 不会自动获得更新,但这是处理配置文件的更常见方式,并且您conf/interpreter.json看起来像是一个很好的例子......

要记住的注意事项:

  • 挂载是“重叠”的底层路径,因此您必须将文件挂载到文件点才能与其他文件共享其文件夹。最多共享一个文件夹将为您提供包含单个文件的文件夹,这通常不是必需的。
  • 如果您使用 ConfigMaps,那么您必须使用 subPath 引用单个文件才能挂载它,即使您在 ConfigMap 中有一个文件。像这样的东西:

    containers:
    - volumeMounts:
      - name: my-config
        mountPath: /my-app/my-config.json
        subPath: config.json
    volumes:
    - name: my-config
      configMap:
        name: cm-my-config-map-example
    

编辑:

使用 .将单个example.sh脚本文件挂载到/bin容器目录的完整示例ConfigMap

您可以调整此示例以满足将具有任何权限的任何文件放置在任何所需文件夹中的需要。替换my-namespace为任何想要的(或完全删除default一个)

配置图:

kind: ConfigMap
apiVersion: v1
metadata:
  namespace: my-namespace
  name: cm-example-script
data:
  example-script.sh: |
     #!/bin/bash
     echo "Yaaaay! It's an example!"

部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: my-namespace
  name: example-deployment
  labels:
    app: example-app
spec:
  selector:
    matchLabels:
      app: example-app
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - image: ubuntu:16.04
        name: example-app-container
        stdin: true
        tty: true
        volumeMounts:
          - mountPath: /bin/example-script.sh
            subPath: example-script.sh
            name: example-script
      volumes:
      - name: example-script
        configMap:
          name: cm-example-script
          defaultMode: 0744

使用持久卷将单个test.txt文件挂载到/bin容器目录的完整示例(文件已存在于卷的根目录中)。

但是,如果您希望使用持久卷而不是 configMap 进行挂载,这里是另一个以相同方式挂载的示例(test.txt 挂载在 /bin/test.txt 中)...注意两点:test.txt必须存在于 PV 和我使用 statefulset 只是为了使用自动配置的 pvc 运行,您可以相应地进行调整...

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: my-namespace
  name: ss-example-file-mount
spec:
  serviceName: svc-example-file-mount
  replicas: 1
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
        - image: ubuntu:16.04
          name: example-app-container
          stdin: true
          tty: true
          volumeMounts:
            - name: persistent-storage-example
              mountPath: /bin/test.txt
              subPath: test.txt
  volumeClaimTemplates:
  - metadata:
      name: persistent-storage-example
    spec:
      storageClassName: sc-my-storage-class-for-provisioning-pv
      accessModes: [ ReadWriteOnce ]
      resources:
        requests:
          storage: 2Gi

推荐阅读