使用minikube在kubernetes中将文件从主机挂载/复制到Pod

时间:2020-05-16 03:57:59

标签: kubernetes kubectl minikube

我正在编写一个kubectl配置来启动图像并将文件复制到容器中。 我需要Config.yaml中的文件/,因此/Config.yaml必须是有效文件。 在启动之前,我需要Pod中的该文件,因此kubectl cp不起作用。 我的本地文件夹中有Config2.yaml,并且正在像这样启动pod:

kubectl apply -f pod.yml

以下是我的pod.yml文件。

apiVersion: v1
kind: Pod
metadata:
   name: python
spec:
   containers:
   - name: python
     image: mypython
     volumeMounts:
     - name: config
       mountPath: /Config.yaml
   volumes:
   - name: config
     hostPath:
       path: Config2.yaml
       type: File

如果我尝试这样使用,它也会失败:

      - name: config-yaml
        mountPath: /
        subPath: Config.yaml
        #readOnly: true

2 个答案:

答案 0 :(得分:4)

如果您只需要从创建之日起就将config.yaml中包含的信息显示在pod中,请改用configMap。

创建一个configMap,其中包含config.yaml中存储的所有数据,并将其装入pod的正确路径中。这不适用于读/写,但对只读数据则效果很好

答案 1 :(得分:0)

您可以在此处尝试postStart生命周期处理程序以在pod启动之前验证文件。

请参阅here

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    resources: {}
    volumeMounts:
    - mountPath: /config.yaml
      name: config
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "apt update && apt install yamllint -y && yamllint /config.yaml"]
  volumes:
  - name: config
    hostPath:
      path: /tmp/config.yaml
      type: File 
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

如果config.yaml无效。 Pod无法启动。