【问题标题】:How to mount data file in kubernetes via pvc?如何通过 pvc 在 kubernetes 中挂载数据文件?
【发布时间】:2018-08-02 07:58:22
【问题描述】:

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

图片信息:

【问题讨论】:

    标签: kubernetes kubernetes-pvc


    【解决方案1】:

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

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

    一般来说,您可以选择:

    • 使用subPath 分隔配置文件。
    • 将卷/路径作为目录挂载到其他位置,然后将文件链接到 pod 中的特定位置(在极少数情况下,与其他配置文件或同一目录中的目录权限混合会出现问题,或者应用程序的启动/启动策略防止文件在 pod 启动时挂载,但在执行一些初始化后必须存在,真的是边缘情况)。
    • 使用ConfigMaps(甚至Secrets)来保存配置文件。请注意,如果将 subPath 与 configMap 和 Secret pod 一起使用,则不会自动在那里获得更新,但这是处理配置文件的更常见方式,而您的 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
      

    编辑:

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

    您可以调整此示例以满足您将具有任何权限的任何文件放置在任何所需文件夹中的需要。将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 中)...请注意两点:@987654336 @ 必须存在于 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
    

    【讨论】:

    • 好像subPath不能解决问题,subPath主要是挂载特定的子目录,不是整个目录,也不能是文件。
    • 我添加了一些使用我们在清单中使用的 subPath 装载数据文件的示例。
    • 我还添加了使用 subPath 将单个文件从持久卷挂载到已填充文件夹的示例。示例在我们的集群上进行了测试并按预期工作,但您需要考虑配置持久卷的不同方式(与 subPath 无关,只是为您提供调整示例以适应您的设置的指针)
    • 我尝试了配置映射,它可以挂载文件,但它没有解决我的问题,文件内容为空,文件内容无法预填充。就我而言,interpreter.json 是由 zeppelin 服务自动更新的,我想在服务之外挂载文件 interpreter.json 以保存状态以防服务重启。
    • 把问题弄清楚。问题是我想持久化某个服务的一个状态文件(服务生成的状态),以防服务重启时状态丢失,如何解决?
    猜你喜欢
    • 2019-10-09
    • 2017-11-26
    • 1970-01-01
    • 2022-01-21
    • 2019-01-31
    • 2019-04-10
    • 2020-06-04
    • 2019-10-18
    • 1970-01-01
    相关资源
    最近更新 更多