簡體   English   中英

如何使用 docker 在 Angular 應用程序中傳遞環境變量

[英]How to pass environment variables in angular application with docker

我正在將我的 Angular 6 應用程序作為 Kubernetes pod/容器運行。 在這個 Angular 應用程序中,我嘗試訪問一個 JSON 配置對象,以加載我的環境變量。

我已經使用 volume 來讀取配置映射和卷掛載來創建一個配置 json 文件。

如果我在 yaml 文件中包含卷創建代碼,我的服務將引發503錯誤。

在日志中看到時,Pod 處於運行模式。

我的部署 yaml 文件的代碼片段:

  apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "client-onboard.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "client-onboard.name" . }}
    helm.sh/chart: {{ include "client-onboard.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "client-onboard.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "client-onboard.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          volumeMounts:
          - name: env-vars
            mountPath: usr/share/nginx/html/config/env-vars.json
      volumes:
        - name: env-vars
          configMap:
            name: {{ .Chart.Name }}-configmap
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}

我為此使用了 nginx 服務器。

要創建文件,您需要使用子路徑。子路徑的值必須與 ConfigMap 的“鍵”匹配。 在下面的例子中,key 和 subPath 是 config.yaml。

apiVersion: v1
kind: ConfigMap
metadata:
  name: sherlock-config
  namespace: default
data:
  config.yaml: |
    namespaces:
      - default
    labels:
      - "app"
      - "owner"
---
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config/config.yaml
        subPath: config.yaml
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: sherlock-config
  restartPolicy: Never

kubectl 描述 cm Sherlock-config

Name:         sherlock-config
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"config.yaml":"namespaces:\n  - default\nlabels:\n  - \"app\"\n  - \"owner\"\n"},"kind":"ConfigMap","metadata":...

Data
====
config.yaml: <------ This is the key
----
namespaces:
  - default
labels:
  - "app"
  - "owner"

Events:  <none>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM