如何在码头工人的角度应用程序中传递环境变量

问题描述 投票:0回答:1

我将我的Angular 6应用程序作为Kubernetes容器/容器运行。在此Angular应用程序中,我尝试访问JSON配置对象以加载环境变量。

我已经使用卷来读取配置映射并通过卷挂载来创建配置json文件。

如果在yaml文件中包含卷创建代码,则我的服务将引发503错误。

在日志中看到的窗格处于运行模式。

我的部署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服务器

docker kubernetes angular6 yaml volume
1个回答
0
投票

使用ConfigMap数据定义容器环境变量,而无需使用卷

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        # Define the environment variable
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: special-config
              # Specify the key associated with the value
              key: special.how
  restartPolicy: Never

用存储在ConfigMap中的数据填充卷

在Pod规范的卷部分下添加ConfigMap名称。这会将ConfigMap数据添加到指定为volumeMounts.mountPath的目录(在本例中为/ etc / config)。命令部分列出名称与ConfigMap中的键匹配的目录文件。

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
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never
© www.soinside.com 2019 - 2024. All rights reserved.