kubernetes:init容器中的安装量 [英] kubernetes: mounting volume from within init container
问题描述
我正在尝试使用init容器在主容器启动之前准备一些文件.我想在init容器中挂载hostPath
卷,以便为主容器共享准备一些文件.
I'm trying to make use of init container to prepare some files before the main container starts up. In the init container I'd like to mount a hostPath
volume so that I can share prepare some file for the main container.
我的集群使用的是1.6版之前的kubernetes,因此我使用的是meta.annotation
语法:
My cluster is using pre 1.6 version of kubernetes so I'm using the meta.annotation
syntax:
pod.beta.kubernetes.io/init-containers: '[
{
"name": "init-myservice",
"image": "busybox",
"command": ["sh", "-c", "mkdir /tmp/jack/ && touch cd /tmp/jack && touch a b c"],
"volumeMounts": [{
"mountPath": "/tmp/jack",
"name": "confdir"
}]
}
]'
但是它似乎不起作用. volumeMounts
的添加导致容器init-myserver
进入CrashLoop.没有它,吊舱将无法成功创建,但无法实现我想要的功能.
But it doesn't seem to work. The addition of volumeMounts
cause the container init-myserver
go into CrashLoop. Without it the pod gets created successfully but it doesn't achieve what I want.
是否无法在< 1.5中将体积安装在初始化容器中? 1.6+呢?
Is it not possible in <1.5 to mount volume in init container? What about 1.6+?
推荐答案
您无需执行hostPath
卷即可将init容器生成的数据与Pod容器共享.您可以使用emptyDir
获得相同的结果.使用emptyDir
的好处是您无需在主机上执行任何操作,即使您无法访问该群集上的节点,它也可以在任何类型的群集上工作.
You don't need to do hostPath
volume to share data generated by init-container with the containers of Pod. You can use emptyDir
to achieve same result. The benefit of using emptyDir
is that you don't need to do anything on host and this will work on any kind of cluster even if you don't have access to nodes on that cluster.
使用hostPath
的另一套问题是在主机上的该文件夹上设置适当的权限,如果使用的是启用了SELinux的发行版,则必须在该目录上设置正确的上下文.
Another set of problems with using hostPath
is setting proper permissions on that folder on host also if you are using any SELinux enabled distro you have to setup the right context on that directory.
apiVersion: v1
kind: Pod
metadata:
name: init
labels:
app: init
annotations:
pod.beta.kubernetes.io/init-containers: '[
{
"name": "download",
"image": "axeclbr/git",
"command": [
"git",
"clone",
"https://github.com/mdn/beginner-html-site-scripted",
"/var/lib/data"
],
"volumeMounts": [
{
"mountPath": "/var/lib/data",
"name": "git"
}
]
}
]'
spec:
containers:
- name: run
image: docker.io/centos/httpd
ports:
- containerPort: 80
volumeMounts:
- mountPath: /var/www/html
name: git
volumes:
- emptyDir: {}
name: git
签出上面的示例,其中init-container和pod中的容器共享同一个名为git
的卷.卷的类型为emptyDir
.我只希望init容器每次出现此pod时都提取数据,然后从pod的httpd
容器中提供数据.
Checkout the above example where the init-container and the container in pod are sharing same volume named git
. And the type of volume is emptyDir
. I just want the init-container to pull the data everytime this pod comes up and then it is served from the httpd
container of pod.
HTH.
这篇关于kubernetes:init容器中的安装量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!