我们最近在使用容器内部的环境变量时遇到了一个问题。
操作系统:Windows 10专业版
k8s集群:minikube
k8s版本:1.18.3
1。无效的方式,尽管它是我们的首选方式
这是使用“ envFrom”的deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: db
labels:
app.kubernetes.io/name: db
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: db
template:
metadata:
labels:
app.kubernetes.io/name: db
spec:
serviceAccountName: default
securityContext:
{}
containers:
- name: db
image: "postgres:9.4"
ports:
- name: http
containerPort: 5432
protocol: TCP
envFrom:
- configMapRef:
name: db-configmap
这是db.properties:
POSTGRES_HOST_AUTH_METHOD=trust
步骤1:
kubectl create configmap db-configmap ./db.properties
步骤2:
kebuctl apply -f ./deployment.yaml
第3步:
kubectl get pod
运行上面的命令,得到以下结果:
db-8d7f7bcb9-7l788 0/1 CrashLoopBackOff 1 9s
这表示未未插入环境变量POSTGRES_HOST_AUTH_METHOD。
2。有效的方式(我们无法使用这种方法)
这是使用'env'的deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: db
labels:
app.kubernetes.io/name: db
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: db
template:
metadata:
labels:
app.kubernetes.io/name: db
spec:
serviceAccountName: default
securityContext:
{}
containers:
- name: db
image: "postgres:9.4"
ports:
- name: http
containerPort: 5432
protocol: TCP
env:
- name: POSTGRES_HOST_AUTH_METHOD
value: trust
步骤1:
kubectl apply -f ./deployment.yaml
步骤2:
kubectl get pod
运行上面的命令,得到以下结果:
db-fc58f998d-nxgnn 1/1 Running 0 32s
以上指示环境已注入,以便数据库启动。
在第一种情况下我做了什么错事?
预先感谢您的帮助。
更新:
提供配置图:
kubectl describe configmap db-configmap
Name: db-configmap
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
db.properties:
----
POSTGRES_HOST_AUTH_METHOD=trust
答案 0 :(得分:2)
用于创建用例1的配置映射。请使用以下命令
mean
答案 1 :(得分:0)
您是否丢失了钥匙? (请参见下面的“键:”(无引号))而且我认为您需要提供env变量的名称...人们通常使用该键名,但不必这样做。我在下面重复了与环境变量NAME和配置映射的键名相同的值(“ POSTGRES_HOST_AUTH_METHOD”)。
#start env .. where we add environment variables
env:
# Define the environment variable
- name: POSTGRES_HOST_AUTH_METHOD
#value: "UseHardCodedValueToDebugSometimes"
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to environment variable (above "name:")
name: db-configmap
# Specify the key associated with the value
key: POSTGRES_HOST_AUTH_METHOD
我的示例(尝试使用您的值)。...来自此通用示例:
pods / pod-single-configmap-env-variable.yaml
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
PS
在您正确设置后,您可以使用“描述”在您的配置映射中添加外观。
kubectl describe configmap db-configmap --namespace=IfNotDefaultNameSpaceHere
答案 2 :(得分:0)
请按照您的描述进行操作。
deployment# exb db-7785cdd5d8-6cstw
root@db-7785cdd5d8-6cstw:/# env | grep -i TRUST
db.properties=POSTGRES_HOST_AUTH_METHOD=trust
环境集并不完全是POSTGRES_HOST_AUTH_METHOD,实际上是环境中的文件名。
通过创建配置图
kubectl create cm db-configmap --from-env-file db.properties
,它实际上会将env POSTGRES_HOST_AUTH_METHOD放入pod中。