How to map one single file into kubernetes pod using hostPath? How to map one single file into kubernetes pod using hostPath? kubernetes kubernetes

How to map one single file into kubernetes pod using hostPath?


Try using the subPath key on your volumeMounts like this:

apiVersion: v1kind: Podmetadata:  name: singlefilespec:  containers:  - image: ubuntu    name: singlefiletest    command:      - /bin/bash      - -c      - ls -la /singlefile/ && cat /singlefile/hosts    volumeMounts:    - mountPath: /singlefile/hosts      name: etc      subPath: hosts  volumes:  - name: etc    hostPath:      path: /etc

Example:

$ kubectl apply -f singlefile.yamlpod "singlefile" created$ kubectl logs singlefiletotal 24drwxr-xr-x. 2 root root 4096 Aug  3 12:50 .drwxr-xr-x. 1 root root 4096 Aug  3 12:50 ..-rw-r--r--. 1 root root 1213 Apr 26 21:25 hosts# /etc/hosts: Local Host Database## This file describes a number of aliases-to-address mappings for the for # local hosts that share this file....


Actually it is caused by kvm which is used by minikube.

path: /home/ubuntu/workspace/web.conf

If I login to minikube, it is folder in vm.

$ ls -al /home/ubuntu/workspace # in minikube hosttotal 12drwxrwxr-x 2 ubuntu ubuntu 4096 Aug  3 12:11 .drwxrwxr-x 5 ubuntu ubuntu 4096 Aug  3 19:28 ..-rw-rw-r-- 1 ubuntu ubuntu 1184 Aug  3 12:11 web.conf$ minikube ssh$ ls -al /home/ubuntu/workspace # in minikube vmtotal 0drwxr-xr-x 3 root root 0 Aug  3 19:41 .drwxr-xr-x 4 root root 0 Aug  3 19:41 ..drwxr-xr-x 2 root root 0 Aug  3 19:41 web.conf

I don't know exactly why kvm host folder sharing behalf like this.

Therefore instead I use minikube mount command, see host_folder_mount.md, then it works as expected.