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

时间:2017-05-11 19:06:58

标签: kubernetes kubernetes-helm

我有4个Kubernetes / Helm部署(网络,电子邮件工作,工作,同步),所有这些都需要共享完全相同的spec.template.spec.containers[].env密钥。 env键非常大,我想避免在每次部署中复制/粘贴它,例如:

# ...
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可以实现这个目标吗?

1 个答案:

答案 0 :(得分:2)

所以我找到了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" . }}