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

5/16/2020

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
-- Rodolfo
kubectl
kubernetes
minikube

2 Answers

5/16/2020

You can try postStart lifecycle handler here to validate the file before pod starts.

Please refer 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: {}

If config.yaml is invalid. Pod won't start.

-- hariK
Source: StackOverflow

5/16/2020

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

-- Patrick W
Source: StackOverflow