如何在Helm子图中设置与环境相关的values.yaml? [英] How to set environment related values.yaml in Helm subcharts?
问题描述
我目前正在使用Helm在Kubernetes集群中部署我的应用程序.现在,我还需要能够针对不同的环境修改values.yaml文件中的某些参数.
I am currently deploying my applications in a Kubernetes cluster using Helm. Now I also need to be able to modify some parameter in the values.yaml file for different environments.
对于仅具有一个级别的简单图表,通过具有不同的values-local.yaml和values-prod.yaml并将其添加到helm install
标志,例如,这很容易. helm install --values values-local.yaml
.
For simple charts with only one level this is easy by having different values-local.yaml and values-prod.yaml and add this to the helm install
flag, e.g. helm install --values values-local.yaml
.
但是,如果我有第二层子图,这又需要区分多个环境之间的值,则无法设置自定义values.yaml.
But if I have a second layer of subcharts, which also need to distinguish the values between multiple environments, I cannot set a custom values.yaml.
假定以下结构:
| chart
| Chart.yaml
| values-local.yaml
| values-prod.yaml
| charts
| foo-app
| Chart.yaml
| values-local.yaml
| values-prod.yaml
| templates
| deployments.yaml
| services.yaml
这将无法正常工作,因为Helm希望子图中出现values.yaml
.
This will not work since Helm is expecting a values.yaml
in subcharts.
我现在的解决方法是在subchart/values.yaml中具有if-else-construct并将其设置为父values.yaml中的全局变量.
My workaround right now is to have an if-else-construct in the subchart/values.yaml and set this in as a global variable in the parent values.yaml.
*foo-app/values.yaml*
{{ - if .Values.global.env.local }}
foo-app:
replicas: 1
{{ else if .Values.global.env.dev}}
foo-app:
replicas: 2
{{ end }}
parent/values-local.yaml
global:
env:
local: true
parent/values-prod.yaml
global:
env:
prod: true
但是我希望周围有更好的方法,所以我不需要依赖这些自定义标志.
But I hope there is a better approach around so I do not need to rely on these custom flags.
希望您能在此方面为我提供帮助.
I hope you can help me out on this.
推荐答案
Here is how I would do it (for reference overriding values):
- 在子图表(foochart)中,将副本数定义为变量:
- foochart/values.yaml
- In your child charts (foochart) define the number of replicas as a variable:
- foochart/values.yaml
...
replicas: 1
...
- foochart/templates/deployment.yaml
...
spec:
replicas: {{ .Values.replicas }}
...
-
然后,在主图表的值文件中:
Then, in your main chart's values files:
- values-local.yaml
foochart:
replicas: 1
- values-prod.yaml
foochart:
replicas: 2
这篇关于如何在Helm子图中设置与环境相关的values.yaml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!