【问题标题】:How to mount pod container path which is having data (I need this data)to my local host path in kubernetes如何将具有数据(我需要此数据)的 pod 容器路径挂载到 kubernetes 中的本地主机路径
【发布时间】:2020-05-07 15:57:18
【问题描述】:

我有 pod (kind:job),它在“/usr/src/app”下有一些代码构建文件,这些文件我需要在我的本地 k8s 主机中。

但是当我尝试按照以下 yamls 进行操作时,我无法在挂载的主机路径中看到实际存在于 pod 容器(“/usr/src/app”)中的任何数据。我认为安装是覆盖/隐藏该数据。请帮助我进入我的本地 k8s 主机。

我的文件是:-

apiVersion: batch/v1
kind: Job
metadata:
  name: wf
spec:
  template:
    spec:
      containers:
      - name: wf
        image: 12345678.dkr.ecr.ap-south-1.amazonaws.com/eks:ws
        volumeMounts:
          - name: wf-persistent-storage
            mountPath: /usr/src/app    # my data is in (/usr/src/app)
      volumes:
        - name: wf-persistent-storage
          # pointer to the configuration of HOW we want the mount to be implemented
          persistentVolumeClaim:
            claimName: wf-test-pvc
      restartPolicy: Never
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wf-test-pvc
spec:
  storageClassName: mylocalstorage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local
spec:
  storageClassName: mylocalstorage
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/root/mnt/"
    type: DirectoryOrCreate

【问题讨论】:

  • Job 实际上在做什么?我通常希望“app”目录的内容来自 Docker 映像,而不是挂载的卷,在这种情况下,您根本不需要这些卷和挂载。

标签: node.js docker kubernetes


【解决方案1】:

不要挂载你的 /usr/src/app。因为它将被 PVC 的内容覆盖。在您的情况下,pvc 最初是空的,因此所有文件都将被删除。

尝试使用以下代码,您将在 pvc 上挂载 /tmp 并使用命令将文件复制到 pvc。

apiVersion: batch/v1
kind: Job
metadata:
  name: wf
spec:
  template:
    spec:
      containers:
      - name: wf
        image: 12345678.dkr.ecr.ap-south-1.amazonaws.com/eks:ws
        command:
        - bash 
        - -c
        - cp -R /usr/src/app/* /tmp/ 
        volumeMounts:
          - name: wf-persistent-storage
            mountPath: /opt    # my data is in (/usr/src/app)
      volumes:
        - name: wf-persistent-storage
          # pointer to the configuration of HOW we want the mount to be implemented
          persistentVolumeClaim:
            claimName: wf-test-pvc
      restartPolicy: Never

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 2020-12-08
    • 2015-11-06
    相关资源
    最近更新 更多