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



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

不幸的是,根据我的理解,除非文件是属性文件(语法"键=值"(,否则这是不可能的。

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

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

BR

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

是的,这是可能的,你走在正确的轨道上。

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

---
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

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

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

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

init 容器想要共享的文件可以通过 emptyDir 的mountPath 上的容器进程复制,然后主容器从 emptyDir 卷的 mouthPath 中选取它。

这样,即使在 pod 重新启动后,有问题的文件,比如路径/path1/file 也会复制到/path2/file(在 initcontainer 上 mouthPath of emptyDir(,然后在 emptyDir 挂载到主容器时保持在那里,直到 pod 重新启动

最新更新