如何将PVC的子路径挂载到容器中的特定路径

How to mount the sub path of PVC to the specific path in container

我有一个由云提供商提供的存储空间。它能够通过 PVC 将存储安装到容器。

有什么方法可以指定磁盘的子路径然后挂载到容器的特定路径。

我尝试了如下自定义值,但它不起作用。

Persistence:
  Enabled: true
  ## A manually managed Persistent Volume and Claim
  ## Requires Persistence.Enabled: true
  ## If defined, PVC must be created manually before volume will be bound
  ExistingClaim: ci-jenkins-data

  ## jenkins data Persistent Volume Storage Class
  ## If defined, storageClassName: <storageClass>
  ## If set to "-", storageClassName: "", which disables dynamic provisioning
  ## If undefined (the default) or set to null, no storageClassName spec is
  ##   set, choosing the default provisioner.  (gp2 on AWS, standard on
  ##   GKE, AWS & OpenStack)
  ##
  # StorageClass: "-"

  Annotations: {}
  AccessMode: ReadWriteOnce
  Size: 100Gi
  volumes:
    - name: ci-jenkins-data
      mountPath: /var/jenkins_home
      subPath: /volume/jenkins

由于一个 PVC 只能声明一次,我假设你想在单个 pod 中挂载卷中的子文件夹。我在 volumeMounts 部分指定了子路径,如下所示。

apiVersion: v1
kind: Pod
metadata:
  name: my-lamp-site
spec:
    containers:
    - name: mysql
      image: mysql
      env:
      - name: MYSQL_ROOT_PASSWORD
        value: "rootpasswd" 
      volumeMounts:
      - mountPath: /var/lib/mysql
        name: site-data
        subPath: mysql
    - name: php
      image: php:7.0-apache
      volumeMounts:
      - mountPath: /var/www/html
        name: site-data
        subPath: html
    volumes:
    - name: site-data
      persistentVolumeClaim:
        claimName: my-lamp-site-data

以上代码段复制自 https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath

我确定您可能已经尝试过主机路径,但是 1.9 原始块卷支持可能对您有所帮助。

让我知道这是否有帮助。

问候 苏达卡尔