I’m moving a setup from Compose and Ansible to Kubernetes with Flux. Some apps need a directory of many files and subdirectories, and I’d like those files to stay in Git without hand-embedding each one in YAML. What approaches work well?
What’s a sane way to manage a directory of Kubernetes config files?
Answers
For files that belong in Git, Kustomize’s ConfigMap generator is a good starting point. Keep the source files in their existing directories and generate ConfigMaps from them, rather than pasting file contents into YAML. You can then mount the generated ConfigMaps where the application expects them. It also avoids much of the quoting and indentation trouble with JSON or nested configuration.
For a large tree, you may still need to split things up and define the mounts clearly. It is not exactly the same as mounting one arbitrary directory, but the file contents remain reviewable and declarative.
Reply to this answer
For a modest number of files, use separate ConfigMaps for sensible groups, such as one per subdirectory, and mount them at the relevant paths. A ConfigMap volume can present its keys as files, but it does not recreate an arbitrary nested directory tree by itself, so plan the mapping rather than expecting one object to behave like a host directory.
If the configuration barely changes and there are lots of files, putting it in a custom image can be simpler. That trades easy live edits for a versioned image update whenever the files change.
Reply to this answer
There is no single Kubernetes object that works exactly like a bind mount of a whole source directory. If the app can assemble its configuration at startup, an init container can copy or combine files into an emptyDir volume shared with the main container. That can help when files arrive from several sources, though it adds another container and more wiring.
A PVC is for persistent data, not automatically a Git-managed configuration source. For configuration you want reproducible from Git, keep the source under version control and deploy it through generated objects or an image.
Reply to this answer
Before building a pile of files, check whether the application has a higher-level configuration mechanism. Some systems can generate their internal files from a few settings, or provide a Kubernetes-specific resource for a common configuration type. That can be much easier to maintain than mirroring every file into a volume.
For Grafana dashboards, for example, dashboard discovery or a dashboard resource may fit better than treating every JSON file as a hand-managed mounted file. The right choice depends on the application, though.
Reply to this answer
Add your answer
Answers are reviewed before publication.
9M__Wicker:
The nested paths and avoiding manual YAML are the parts I’m stuck on. The files need to remain easy to edit in Git.