简体   繁体   English

具有动态路径的 Kube.netes NFS 卷

[英]Kubernetes NFS volume with dynamic path

I am trying to mount my applications' logs directory to nfs dynamically including node_name.我正在尝试将我的应用程序的日志目录动态地挂载到 nfs,包括 node_name。

No success so far.到目前为止没有成功。

I tried as below:我尝试如下:

kind: Pod
apiVersion: v1
metadata:
  name: nfs-in-a-pod
spec:
  containers:
    - name: app
      image: alpine
      env:
      - name: POD_NAME
        valueFrom:
          fieldRef:
            apiVersion: v1
            fieldPath: metadata.name
      - name: NODE_NAME
        valueFrom:
          fieldRef:
            apiVersion: v1
            fieldPath: spec.nodeName
      volumeMounts:
        - name: nfs-volume
          mountPath: /var/nfs
          subPath: /$(NODE_NAME)
      command: ["/bin/sh"]
      args: ["-c", "sleep 500000"]
  volumes:
    - name: nfs-volume
      nfs:
        server: ip_adress_here
        path: /mnt/events

I think instead of subPath you should use subPathExpr, as mentioned in the documentation .我认为您应该使用 subPathExpr 而不是 subPath,如文档中所述。

Use the subPathExpr field to construct subPath directory names from Downward API environment variables.使用 subPathExpr 字段从 Downward API 环境变量构造 subPath 目录名称。 This feature requires the VolumeSubpathEnvExpansion feature gate to be enabled.此功能需要启用 VolumeSubpathEnvExpansion 功能门控。 It is enabled by default starting with Kube.netes 1.15.它从 Kube.netes 1.15 开始默认启用。 The subPath and subPathExpr properties are mutually exclusive. subPath 和 subPathExpr 属性是互斥的。


In this example, a Pod uses subPathExpr to create a directory pod1 within the hostPath volume /var/log/pods, using the pod name from the Downward API. The host directory /var/log/pods/pod1 is mounted at /logs in the container.在这个例子中,一个 Pod 使用 subPathExpr 在 hostPath 卷 /var/log/pods 中创建一个目录 pod1,使用来自 Downward 的 pod 名称 API。主机目录 /var/log/pods/pod1 挂载在 /logs 中容器。

apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
  - name: container1
    env:
    - name: POD_NAME
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.name
    image: busybox
    command: [ "sh", "-c", "while [ true ]; do echo 'Hello'; sleep 10; done | tee -a /logs/hello.txt" ]
    volumeMounts:
    - name: workdir1
      mountPath: /logs
      subPathExpr: $(POD_NAME)
  restartPolicy: Never
  volumes:
  - name: workdir1
    hostPath:
      path: /var/log/pods

Hope that´s it.希望就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM