简体   繁体   English

如何在Helm子图中设置与环境相关的values.yaml?

[英]How to set environment related values.yaml in Helm subcharts?

I am currently deploying my applications in a Kubernetes cluster using Helm. 我目前正在使用Helm将应用程序部署在Kubernetes集群中。 Now I also need to be able to modify some parameter in the values.yaml file for different environments. 现在,我还需要能够针对不同的环境修改values.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, eg helm install --values values-local.yaml . 对于只有一个级别的简单图表,可以通过设置不同的values-local.yaml和values-prod.yaml并将其添加到helm install标志(例如helm install --values values-local.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. 但是,如果我有第二层子图,也需要在多个环境之间进行区分,则无法设置自定义values.yaml。

Assuming following structure: 假设以下结构:

| 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

This will not work since Helm is expecting a values.yaml in subcharts. 由于Helm在子图中需要一个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. 我现在的解决方法是在subchart / values.yaml中具有if-else-construct并将其设置为父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 ): 这是我的操作方法(用于参考覆盖值 ):

  1. In your child charts (foochart) define the number of replicas as a variable: 在子图表(foochart)中,将副本数定义为变量:
    • foochart/values.yaml foochart / values.yaml

...
replicas: 1
...
  • foochart/templates/deployment.yaml foochart / templates / deployment.yaml

...
spec:
  replicas: {{ .Values.replicas }}
...
  1. Then, in your main chart's values files: 然后,在主图表的值文件中:

    • values-local.yaml values-local.yaml

foochart:
  replicas: 1
  • values-prod.yaml 值prod.yaml

foochart:
  replicas: 2

Just an idea, needs to be fleshed out a bit more... 只是一个想法,需要充实一点...

At KubeCon I saw a talk where they introduced a Kubernetes Operator called Lostromos . 在KubeCon上,我看到了一个演讲 ,他们介绍了一个叫做Lostromos的Kubernetes运算符。 The idea was to simplify deployments to support multiple different environments and make maintaining these kinds of things easier. 这个想法是简化部署以支持多个不同的环境,并使此类事情的维护更加容易。 It uses Custom Resource definitions . 它使用自定义资源定义 I wonder if you can leverage Lostromos in this case. 我想知道在这种情况下是否可以利用Lostromos。 Your sub-charts would have a single values.yaml but use lostromos and a CRD to feed in the properties you need . 您的子图表只有一个values.yaml但使用lostromos和CRD来输入所需的属性 So you would deploy the CRD instead and the CRD would trigger Lostromos to deploy your Helm chart. 因此,您将改为部署CRD,而CRD会触发Lostromos部署您的Helm图表。

Just something to get the ideas going but seemed like it might be worth exploring. 只是一些可以使想法付诸实践的东西,但似乎值得探讨。

I'm currently getting my chart from stable/jenkins and am trying to set my values.yaml file. 我目前正在从稳定/詹金斯获得图表,并试图设置我的values.yaml文件。 I have made the appropriate changes and try to run 'helm install -n --values= stable/jenkins but it continues to install the default values instead of the modified yaml file i created. 我进行了适当的更改,然后尝试运行'helm install -n --values = stable / jenkins,但是它将继续安装默认值,而不是我创建的修改后的yaml文件。 To be more specific, I commented out the plug-in requirements in the yaml file since it has been causing my pod status to stay on 'Init:0/1' on Kubernetes. 更具体地说,我注释了yaml文件中的插件要求,因为它导致我的pod状态保持在Kubernetes上的“ Init:0/1”上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM