以下代码段来自kubernetes官方文档(http://kubernetes.io/docs/user-guide/volumes/#gitrepo):
apiVersion: v1
kind: Pod
metadata:
name: server
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- mountPath: /mypath
name: git-volume
volumes:
- name: git-volume
gitRepo:
repository: "git@somewhere:me/my-git-repository.git"
revision: "22f1d8406d464b0c0874075539c1f2e96c253775"
以上将在/ mypath的容器中安装完整的git repo。有没有办法在/ mypath的容器内只挂载git repo内的特定子目录?
答案 0 :(得分:2)
是的,有办法做到这一点。请参阅以下示例。
我的git repo有子目录configs
。
所以这是我正在使用的pod文件:
apiVersion: v1
kind: Pod
metadata:
name: server
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- mountPath: /mypath/
name: git-volume
subPath: "hitcounter/configs"
volumes:
- name: git-volume
gitRepo:
repository: "https://github.com/surajssd/hitcounter"
revision: "9fd11822b822c94853b1c74ceb53adb8e1d2cfc8"
请注意subPath
中containers
中的字段volumeMounts
。在那里,您可以指定要在容器内的/mypath
处安装的卷中的子目录。
文档说:
$ kubectl explain pod.spec.containers.volumeMounts.subPath
FIELD: subPath <string>
DESCRIPTION:
Path within the volume from which the container's volume should be mounted.
Defaults to "" (volume's root).
或强>
像这样创建pod配置文件
apiVersion: v1
kind: Pod
metadata:
name: server
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- mountPath: /mypath/
name: git-volume
subPath: "configs"
volumes:
- name: git-volume
gitRepo:
repository: "https://github.com/surajssd/hitcounter"
revision: "9fd11822b822c94853b1c74ceb53adb8e1d2cfc8"
directory: "."
这里的区别是我指定了directory: "."
这确保了挂载点/mypath
将成为git repo并且subPath: "configs"
已更改,因为没有额外的目录hitcounter
$ kubectl explain pod.spec.volumes.gitRepo.directory
FIELD: directory <string>
DESCRIPTION:
Target directory name. Must not contain or start with '..'. If '.' is
supplied, the volume directory will be the git repository. Otherwise, if
specified, the volume will contain the git repository in the subdirectory
with the given name.
HTH
答案 1 :(得分:0)
不,你不能通过Kubernetes gitRepo卷插件来做到这一点。您只能手动执行或使用自己的卷插件。