6

I have 4 Kubernetes/Helm deployments (web, emailworker, jobworker, sync) which all need to share exactly the same spec.template.spec.containers[].env key. The env keys are quite large and I'd like to avoid copy/pasting it in each deployment, e.g.:

# ...
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
# ...

Is this possible to achieve with either yaml, Helm or Kubernetes?

4

2 回答 2

3

所以我找到了一个使用 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" . }}
于 2017-05-11T21:59:04.157 回答
2

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

无需弄乱任何模板。

于 2017-05-11T23:34:08.033 回答