2017-08-03 27 views
0

我有一個由腳本生成的自己的nginx配置/home/ubuntu/workspace/web.conf。我更願意把它/etc/nginx/conf.d下除了default.conf如何使用hostPath將單個文件映射到kubernetes窗格中?

下面是nginx.yaml

 
apiVersion: v1 
kind: Pod 
metadata: 
    name: nginx 
spec: 
    volumes: 
     - name: webconf 
     hostPath: 
      path: /home/ubuntu/workspace/web.conf 
    containers: 
     - image: nginx 
     name: nginx 
     ports: 
      - containerPort: 18001 
      protocol: TCP        
     volumeMounts: 
      - mountPath: /etc/nginx/conf.d/web.conf 
      name: web 

雖然它被映射爲文件夾只

 
$ kubectl create -f nginx.yaml 
pod "nginx" created 
$ kubectl exec -it nginx -- bash 
[email protected]:/app# ls -al /etc/nginx/conf.d/ 
total 12 
drwxr-xr-x 1 root root 4096 Aug 3 12:27 . 
drwxr-xr-x 1 root root 4096 Aug 3 11:46 .. 
-rw-r--r-- 2 root root 1093 Jul 11 13:06 default.conf 
drwxr-xr-x 2 root root 0 Aug 3 11:46 web.conf 

它適用於泊塢窗容器-v hostfile:containerfile

如何在kubernetes中做到這一點?

BTW:我在Ubuntu 16.04 LTS使用minikube 0.21.0kvm

回答

0

其實這是造成由minikube使用的kvm

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

如果我登錄到minikube,它是vm中的文件夾。

$ ls -al /home/ubuntu/workspace # in minikube host 
total 12 
drwxrwxr-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 vm 
total 0 
drwxr-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 

我不知道爲什麼kvm主機文件夾共享代表這樣。

因此改爲使用minikube mount命令,請參閱host_folder_mount.md,然後按預期工作。

2

嘗試使用subPath關鍵在你volumeMounts這樣的:

apiVersion: v1 
kind: Pod 
metadata: 
    name: singlefile 
spec: 
    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 

例子:

$ kubectl apply -f singlefile.yaml 
pod "singlefile" created 
$ kubectl logs singlefile 
total 24 
drwxr-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. 
... 
+0

感謝您的快速響應,它實際上沒有'subPath' –

相關問題