简体   繁体   English

Kubernetes YML,使用 for 循环

[英]kubernetes YML, use for loop

Is there a mechanism in a template (yml) file in k8s for generating multiple items? k8s中的模板(yml)文件中是否有用于生成多个项目的机制? Like a for loop.就像一个 for 循环。

For example , I have a template used my multiple projects.例如,我有一个模板用于我的多个项目。 But some need 1 database others maybe 2, 3 or more.但有些人需要 1 个数据库,而有些人可能需要 2 个、3 个或更多。 I don't want to define all variables by hands, and edit my file if a new projects comes in and needs a new variable.我不想手动定义所有变量,如果有新项目进入并需要新变量,则编辑我的文件。

Example of what I do today:我今天做的例子:

  template:
      (...)
      containers:
        (...)
        env:
        - name: DATABASE_NAME
          value: "{{prj.database.name}}"
        - name: DATABASE_NAME_2
          value: "{{prj.database.name.second}}"
        - name: DATABASE_USER
          valueFrom:
            secretKeyRef:
              name: "{{ k8s.deploy.app.secret.name }}"
              key: database_user
        - name: DATABASE_USER_2
          valueFrom:
            secretKeyRef:
              name: "{{ k8s.deploy.app.secret.name }}"
              key: database_user_2

As you can see I have to copy paste code for each user + password + database name.如您所见,我必须为每个用户 + 密码 + 数据库名称复制粘贴代码。 In this file and also in secrets.在这个文件和秘密中。 I use XLDeploy to insert data in my YML files.我使用 XLDeploy 在我的 YML 文件中插入数据。

What i'm looking for:我在找什么:

  template:
      (...)
      containers:
        (...)
        env:
        ---- for i in {{prj.database.number}} ----
        - name: DATABASE_NAME_{i}
          value: "{{prj.database.name.{i]}}"
        - name: DATABASE_USER_{i}
          valueFrom:
            secretKeyRef:
              name: "{{ k8s.deploy.app.secret.name }}"
              key: database_user_{i}
        ---- end for loop ----

So I could insert in XLDeploy my number of databases to use for each projects.所以我可以在 XLDeploy 中插入我用于每个项目的数据库数量。 And fill the values with XLD too.并用 XLD 填充值。

Is that possible, or I need to use a script langague to generate a YML file from a "YML template" ?这可能吗,或者我需要使用脚本语言从“YML 模板”生成 YML 文件?

You could use helm for this if you have other yamls related to this file.如果您有其他与此文件相关的 yaml,则可以为此使用helm Helm allows for syntax like this: Helm 允许这样的语法:

{{- range .Values.testvalues }}
---
apiVersion: v1
kind: Service
metadata:
  annotations: {}
  labels: {}
  name: {{ . }}
spec:
  ports:
  - name: tcp-80-8080
    port: 80
    protocol: TCP
    targetPort: 8080
  selector: 
    app: rss-{{ . }}
  type: ClusterIP
{{- end }}

So with a list for testvalues in the values.yaml like this:因此,在testvalues中有一个values.yaml列表,如下所示:

testvalues:
- a
- b
- c
- d

Helm would generate four service declarations -- one for each item. Helm 将生成四个服务声明——每个项目一个。 The first looking like this:第一个看起来像这样:

---
apiVersion: v1
kind: Service
metadata:
  annotations: {}
  labels: {}
  name: a
spec:
  ports:
  - name: tcp-80-8080
    port: 80
    protocol: TCP
    targetPort: 8080
  selector: 
    app: rss-a
  type: ClusterIP

The other option if it's just a single file, is to use Jinja and something like Python to build the file using the jinja2 module如果它只是一个文件,另一种选择是使用 Jinja 和 Python 之类的东西来使用jinja2模块构建文件

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

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