Is there a concept of inheritance for Kubernetes deployments?
Asked Answered
G

2

15

Is there a way to create a tree of inheritance for Kubernetes deployments? I have a number of deployments which are similar but not identical. They share many ENV vars but not all. They all use the same image.

For example, I have a dev deployment which is configured almost identical to a production deployment but has env vars pointing to a different database backend. I have a celery deployment which is configured the same as the production deployment, however, it has a different run command.

Grover answered 9/10, 2018 at 15:15 Comment(0)
G
13

Helm is what many people are using for this. It let's you create templates for kubernetes descriptors and pass parameters in to generate descriptors from the templates.

There are other tools out there which can be used to generate variations on kubernetes deployment descriptors by injecting parameters into templates. Ansible is also popular. But Helm is closely connected with the Kubernetes CNCF and community and there's a good selection of official charts available.

EDIT: If the aim is to enable different deployments (e.g. for dev and prod) using a single docker image then that's probably best handled with a single chart. You can create different values files for each deployment and supply the chosen values file to helm install with the --values parameter. If there are parts of the chart that are only sometimes applicable then they can be wrapped in if conditions to turn them on/off.

On the subject of inheritance specifically, there's an example in the helm documention of how to take another chart as a parent/dependency and override its values and I created a chart earlier that you can see in github that includes several other charts and overrides parts of all of them via the values.yml. It also shares some config between the included charts with globals. If you're looking to use a parent to reduce duplication rather than join multiple apps then it is possible to create a base/wrapper chart but it may turn out to be better to just duplicate config.

EDIT (180119): The alternative of Kustomize may soon become available in kubectl

Gothicism answered 9/10, 2018 at 15:35 Comment(5)
Seconding, Helm's use of go templating is also very powerful. If you don't like go templating and are already familiar with Jsonnet, an alternative is ksonnet.ioAgni
It is not clear to me how best to do this with helm templates. I've been reading the docs today and it seems to me that mostly the templates are about getting values from values.yaml into the templates. But how do I get values to flow between files otherwise?Grover
Perhaps I commented exactly too soon. Just came across this section docs.helm.sh/developing_charts/#know-your-template-functions which describes the include function. I guess that that combined with the toYaml function should do it.Grover
Another way is you can include another chart within your chart chart as a subchart (github.com/helm/helm/blob/master/docs/chart_template_guide/…). So you can wrap another chart and extend it. It's more commonly used to embed a component within your own chart (I've tried to explore some uses in hackernoon.com/… )Gothicism
@Grover Seems the difficulties with using parents are under discussion for Helm3 - github.com/helm/helm/issues/3920 #47792471Gothicism
T
2

You may also want to check Kustomize. It provides some support to write your yaml manifests in hierarchical form, so that you don't have to repeat yourself.

Tripetalous answered 9/10, 2018 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.