Kubernetes:如何避免在多个部署中重复环境?

Kubernetes: how to avoid duplicating env in multiple deployments?

我有 4 个 Kubernetes/Helm 部署(web、emailworker、jobworker、同步),它们都需要共享完全相同的 spec.template.spec.containers[].env 密钥。环境密钥非常大,我想在每次部署中避免 copy/pasting 它,例如:

# ...
env:
- name: NODE_ENV
  value: "{{ .Values.node_env }}"
- name: BASEURL
  value: "{{ .Values.base_url }}"
- name: REDIS_HOST
  valueFrom:
    secretKeyRef:
      name: secret-redis
      key: host
- name: KUE_PREFIX
  value: "{{ .Values.kue_prefix }}"
- name: DATABASE_NAME
  value: "{{ .Values.database_name }}"
- name: DATABASE_HOST
  valueFrom:
    secretKeyRef:
      name: secret-postgres
      key: host
- name: DATABASE_USER
  valueFrom:
    secretKeyRef:
      name: secret-postgres
      key: username
- name: DATABASE_PASSWORD
  valueFrom:
    secretKeyRef:
      name: secret-postgres
      key: password
- name: AWS_KEY
  valueFrom:
    secretKeyRef:
      name: secret-bucket
      key: key
- name: AWS_SECRET
  valueFrom:
    secretKeyRef:
      name: secret-bucket
      key: secret
- name: AWS_S3_BUCKET
  valueFrom:
    secretKeyRef:
      name: secret-bucket
      key: bucket
- name: AWS_S3_ENDPOINT
  value: "{{ .Values.s3_endpoint }}"
- name: INSTAGRAM_CLIENT_ID
  valueFrom:
    secretKeyRef:
      name: secret-instagram
      key: clientID
# ...

这可以通过 yaml、Helm 或 Kubernetes 实现吗?

所以我找到了一个使用 Helm 命名模板的解决方案:https://github.com/kubernetes/helm/blob/master/docs/chart_template_guide/named_templates.md

我创建了一个包含以下内容的文件 templates/_env.yaml

{{ define "env" }}
            - name: NODE_ENV
              value: "{{ .Values.node_env }}"
            - name: BASEURL
              value: "{{ .Values.base_url }}"
            - name: REDIS_HOST
              valueFrom:
                secretKeyRef:
                  name: secret-redis
                  key: host
            - name: KUE_PREFIX
              value: "{{ .Values.kue_prefix }}"
            - name: DATABASE_NAME
              value: "{{ .Values.database_name }}"
            - name: DATABASE_HOST
              valueFrom:
                secretKeyRef:
                  name: secret-postgres
                  key: host
            - name: DATABASE_USER
              valueFrom:
                secretKeyRef:
                  name: secret-postgres
                  key: username
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: secret-postgres
                  key: password
            - name: AWS_KEY
              valueFrom:
                secretKeyRef:
                  name: secret-bucket
                  key: key
            - name: AWS_SECRET
              valueFrom:
                secretKeyRef:
                  name: secret-bucket
                  key: secret
            - name: AWS_S3_BUCKET
              valueFrom:
                secretKeyRef:
                  name: secret-bucket
                  key: bucket
            - name: AWS_S3_ENDPOINT
              value: "{{ .Values.s3_endpoint }}"
            - name: INSTAGRAM_CLIENT_ID
              valueFrom:
                secretKeyRef:
                  name: secret-instagram
                  key: clientID
{{ end }}

下面是我在 templates/deployment.yaml 文件中使用它的方式:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: somedeployment
  # ...
spec:
  template:
    # ...
    metadata:
      name: somedeployment
    spec:
      # ...
      containers:
        - name: container-name
          image: someimage
          # ...
          env:
            {{- template "env" . }}

看看ConfigMap。这允许将配置收集到一个资源中并在多个部署中使用。

无需弄乱任何模板。