Helm函数可基于变量设置值? [英] Helm function to set value based on a variable?

查看:176
本文介绍了Helm函数可基于变量设置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Helm来设置我的3个AWS EKS集群-沙箱,登台和生产.

I'm learning Helm to setup my 3 AWS EKS clusters - sandbox, staging, and production.

如何设置模板,以便根据要在其上安装图表的群集来得出一些值?例如,在我的myapp/templates/deployment.yaml中,我可能想要

How can I set up my templates so some values are derived based on which cluster the chart is being installed at? For example, in my myapp/templates/deployment.yaml I may want

apiVersion: apps/v1
kind: Deployment
metadata:
  ...
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}

我可能希望replicas124,这取决于我是将图表分别安装在sandboxstaging还是production集群中?例如,我想对CPU和内存请求以及Pod的限制做同样的事情.

I may want replicas to be either 1, 2, or 4 depending if I'm installing the chart in my sandbox, staging, or production cluster respectively? I wanna do same trick for cpu and memory requests and limits for my pods for example.

我当时想在我的values.yaml文件中包含类似的内容

I was thinking of having something like this in my values.yaml file

environments:
  - sandbox
  - staging
  - production
perClusterValues:
  replicas:
    - 1
    - 2
    - 4
  cpu:
    requests:
      - 256m
      - 512m
      - 1024m
    limits:
      - 512m
      - 1024m
      - 2048m
  memory:
    requests:
      - 1024Mi
      - 1024Mi
      - 2048Mi
    limits:
      - 2048Mi
      - 2048Mi
      - 3072Mi

因此,如果我在sandbox环境中安装头盔图表,我希望能够做到

So if I install a helm chart in the sandbox environment, I want to be able to do

$ helm install myapp myapp --set environment=sandbox

apiVersion: apps/v1
kind: Deployment
metadata:
  ...
spec:
  {{- if not .Values.autoscaling.enabled }}
  # In pseudo-code, in my YAML files
  # Get the index value from .Values.environments list
  # based on pass-in environment parameter
  {{ $myIndex = indexOf .Values.environments .Value.environment }}
  replicas: {{ .Values.perClusterValues.replicas $myIndex }}
  {{- end }}

希望您能理解我的逻辑,但是正确的语法是什么?还是这是个好方法?

I hope you understand my logic, but what is the correct syntax? Or is this even a good approach?

推荐答案

您可以使用helm install -f选项在其中传递额外的YAML值文件,并且该优先级高于图表自己的values.yaml文件.因此,只需使用已有的模板结构,就可以提供备用值文件

You can use the helm install -f option to pass an extra YAML values file in, and this takes precedence over the chart's own values.yaml file. So using exactly the template structure you already have, you can provide alternate values files

# sandbox.yaml
autoscaling:
  enabled: false
replicaCount: 1

# production.yaml
autoscaling:
  enabled: true
replicaCount: 5

然后当您部署图表时,使用

And then when you go to deploy the chart, run it with

helm install myapp . -f production.yaml

(您也可以helm install --set replicaCount=3覆盖特定的值,但是--set语法有点怪异和不寻常;在每个环境中使用单独的YAML文件可能更容易.某些工具可能还可以利用JSON文件有效的YAML以写出其他部署时自定义项.)

(You can also helm install --set replicaCount=3 to override specific values, but the --set syntax is finicky and unusual; using a separate YAML file per environment is probably easier. Some tooling might be able to take advantage of JSON files also being valid YAML to write out additional deploy-time customizations.)

这篇关于Helm函数可基于变量设置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆