Kubernetes / Helm:在Init和Main容器之间共享一个非属性文件

时间:2019-12-16 15:24:06

标签: kubernetes kubernetes-helm volume data-sharing configmap

在kubernetes(minikube)/ helm env中,我有一个ldif文件,我想在Init容器和普通容器之间的卷上共享它。我不想共享存储此文件的整个文件夹。

不幸的是,除非文件是属性文件(语法为“ key = value”),否则我无法理解这一点。

我使用configMap / subPath进行了测试,似乎如果不遵守键/值语法,则Init容器甚至无法启动,否则一切正常,文件也将出现在主容器中。

所以我想知道是否有可能完成这种共享。

BR

编辑:主容器启动命令是单个服务启动,除非唯一方法,否则它无法执行复制或移动由init容器共享的文件。

2 个答案:

答案 0 :(得分:1)

是的,有可能,而且您处在正确的轨道上。

这是如何执行此操作的示例。

---
kind: ConfigMap 
apiVersion: v1 
metadata:
  name: example-configmap 
data:
  my-file.ldif: |
     dn: cn=The Postmaster,dc=example,dc=com
     objectClass: organizationalRole
     cn: The Postmastermongodb
---
kind: Pod
apiVersion: v1
metadata:
  name: example-pod
spec:
  volumes: 
  - name: config-volume
    configMap:
      name: example-configmap
  initContainers:
  - name: init
    image: busybox
    volumeMounts:
    - name: config-volume
      mountPath: /path/in/the/init-container/my-file.ldif
      subPath: my-file.ldif
  containers:
  - name: main
    image: busybox
    volumeMounts:
    - name: config-volume
      mountPath: /path/in/the/container/my-file.ldif
      subPath: my-file.ldif

如果您发布了configmap,将会有所帮助。您可能会被绊倒,因为要使其正常工作,您需要文件的全部内容作为configmap中一个键的值。

答案 1 :(得分:0)

要在init容器和其他容器之间共享文件,可以将volume mount与emptyDir一起使用

  volumes:
  - name: cache-volume
    emptyDir: {}

初始化容器要共享的文件可以由容器进程复制到emptyDir的mountPath上,然后主容器从emptyDir卷的uthPath中选择它。

这样,即使在pod重新启动后,问题文件(例如,路径/ path1 / file中的文件)也被复制到/ path2 / file(initcontainer上的emptyDir的uthPath),然后在将emptyDir挂载到主容器上的情况下停留在该文件中在pod重新启动之前可用