gpt4 book ai didi

docker - 如何使用docker在角度应用程序中传递环境变量

转载 作者:行者123 更新时间:2023-12-02 11:46:44 26 4
gpt4 key购买 nike

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

我已经使用 volume 读取配置映射并使用 volume mount 创建一个配置 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 服务器。

最佳答案

要创建文件,您需要使用 subPath。subPath 的值必须与 ConfigMap 的“键”相匹配。在下面的示例中,键和子路径是 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 describe 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>

关于docker - 如何使用docker在角度应用程序中传递环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59734883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com