在 Kubernetes 导出的 NFS 目录中创建并挂载子目录

Create and mount subdirectories in exported NFS directory in Kubernetes

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

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

我有一个指定持久卷和声明的 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不能自己在节点上执行系统命令,需要借助一些外部工具,比如bash脚本。

您只能在容器中使用路径 /mnt/kubernetes-volumes/,并使用环境变量传递客户名称,但这将使所有其他 pods 都可以访问所有数据,但事实并非如此好主意。

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