Kubernetes:从 init 容器内挂载卷

9

我想利用init容器在主容器启动之前准备一些文件。在init容器中,我想挂载一个hostPath卷,以便为主容器共享一些文件。

我的集群正在使用早于1.6版本的kubernetes,因此我正在使用meta.annotation语法:

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。如果没有它,Pod可以成功创建,但它并没有达到我的预期。
在1.5以下版本中无法在init容器中挂载卷吗? 1.6+呢?
1个回答

9
您不需要使用hostPath卷来共享由init-container生成的数据与Pod的容器。您可以使用emptyDir来实现相同的结果。使用emptyDir的好处是您不需要在主机上进行任何操作,即使您无法访问该集群上的节点,它也将在任何类型的集群上工作。
使用hostPath的另一组问题是在主机上设置正确的文件夹权限,如果您使用任何启用SELinux的发行版,则必须设置该目录的正确上下文。
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容器和pod中的容器共享名为git的同一卷。该卷的类型是emptyDir。我只想让init容器在每次启动此pod时拉取数据,然后从pod的httpd容器提供服务。
希望能对您有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接