Search code examples
kuberneteskubectlminikube

Mount / copy a file from host to Pod in kubernetes using minikube


I'm writing a kubectl configuration to start an image and copy a file to the container. I need the file Config.yaml in the / so /Config.yaml needs to be a valid file. I need that file in the Pod before it starts, so kubectl cp does not work. I have the Config2.yaml in my local folder, and I'm starting the pod like:

kubectl apply -f pod.yml

Here follows my pod.yml file.

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

If I try to use like this it also fails:

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

Solution

  • If you just need the information contained in the config.yaml to be present in the pod from the time it is created, use a configMap instead.

    Create a configMap that contains all the data stored in the config.yaml and mount that into the correct path in the pod. This would not work for read/write, but works wonderfully for read-only data