【问题标题】:Create and mount subdirectories in exported NFS directory in Kubernetes在 Kubernetes 导出的 NFS 目录中创建和挂载子目录
【发布时间】:2020-07-07 08:00:29
【问题描述】:

我有一个带有文件夹/mnt/kubernetes-volumes/ 的文件服务器。在这个文件夹中,我的应用程序的每个用户都有子目录:

  • /mnt/kubernetes-volumes/customerA
  • /mnt/kubernetes-volumes/customerB
  • /mnt/kubernetes-volumes/customerC

在每个客户文件夹中,我有两个用于我的数据存储应用程序的文件夹:

  • /mnt/kubernetes-volumes/customerA/elastic
  • /mnt/kubernetes-volumes/customerA/postgres

我有一个指定持久卷和声明的 Kubernetes 配置文件。这些挂载到elasticpostgres 文件夹。

# Create a volume on the NFS disk that the postgres db can use. 
apiVersion: v1
kind: PersistentVolume
metadata:
  namespace: customerA
  name: postgres-pv
  labels:
    name: postgres-volume 
spec:
  storageClassName: manual 
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 1.2.3.4 # ip addres of nfs server
    path: "/mnt/kubernetes-volumes/customerA/postgres" 

---

# And another persistent volume for Elastic

目前,我的/etc/exports 文件如下所示:

/mnt/kubernetes-volumes/customerA/postgres 10.0.0.1(rw,sync,no_subtree_check,no_root_squash)
/mnt/kubernetes-volumes/customerA/elastic 10.0.0.1(rw,sync,no_subtree_check,no_root_squash)
/mnt/kubernetes-volumes/customerA/postgres 10.0.0.2(rw,sync,no_subtree_check,no_root_squash)
/mnt/kubernetes-volumes/customerA/elastic 10.0.0.2(rw,sync,no_subtree_check,no_root_squash)


/mnt/kubernetes-volumes/customerB/postgres 10.0.0.1(rw,sync,no_subtree_check,no_root_squash)
/mnt/kubernetes-volumes/customerB/elastic 10.0.0.1(rw,sync,no_subtree_check,no_root_squash)
/mnt/kubernetes-volumes/customerB/postgres 10.0.0.2(rw,sync,no_subtree_check,no_root_squash)
/mnt/kubernetes-volumes/customerB/elastic 10.0.0.2(rw,sync,no_subtree_check,no_root_squash)

对于每个客户,我为 Kubernetes 集群中的每个节点分别显式导出 postgreselastic 文件夹。这按预期工作。但是,我现在必须为每个新客户手动将行添加到 etc/exports 文件中。是否可以只有这样的两行:

/mnt/kubernetes-volumes/ 10.0.0.1(rw,sync,no_subtree_check,no_root_squash)
/mnt/kubernetes-volumes/ 10.0.0.2(rw,sync,no_subtree_check,no_root_squash)

并自动让 Kubernetes 在kubernetes-volumes 目录中创建正确的子目录(customer、postgres 和 elastic)并挂载它们?

【问题讨论】:

    标签: kubernetes nfs


    【解决方案1】:

    Kubernetes 自身无法对节点执行系统命令,需要使用一些外部工具,例如 bash 脚本。

    您只能在容器中使用路径 /mnt/kubernetes-volumes/ 并使用环境变量传递客户名称,但这会使所有其他 pod 都可以访问所有数据,这不是一个好主意。

    此外,您可以尝试使用HELM templates 来创建您的persistentVolumes,并使用您的客户名称作为变量。

    【讨论】:

    • 谢谢,shell 脚本很简单,所以我会坚持那个解决方案。
    猜你喜欢
    • 2019-08-02
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 2020-07-24
    • 2010-09-07
    • 1970-01-01
    • 2021-04-25
    • 2018-09-24
    相关资源
    最近更新 更多