为什么我的环境变量没有在 ConfigMap 的 Kubernetes Pod 中设置?



我有以下配置映射规范:

apiVersion: v1
data:
MY_NON_SECRET: foo
MY_OTHER_NON_SECRET: bar
kind: ConfigMap
metadata:
name: web-configmap
namespace: default
$ kubectl describe configmap web-configmap
Name:         web-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>
Data
====
MY_NON_SECRET:
----
foo
MY_OTHER_NON_SECRET:
----
bar
Events:  <none>

以下吊舱规格:

apiVersion: v1
kind: Pod
metadata:
name: web-pod
spec:
containers:
- name: web
image: kahunacohen/hello-kube:latest
envFrom:
- configMapRef:
name: web-configmap
ports:
- containerPort: 3000
$ kubectl describe pod web-deployment-5bb9d846b6-8k2s9
Name:         web-deployment-5bb9d846b6-8k2s9
Namespace:    default
Priority:     0
Node:         minikube/192.168.49.2
Start Time:   Mon, 12 Jul 2021 12:22:24 +0300
Labels:       app=web-pod
pod-template-hash=5bb9d846b6
service=web-service
Annotations:  <none>
Status:       Running
IP:           172.17.0.5
IPs:
IP:           172.17.0.5
Controlled By:  ReplicaSet/web-deployment-5bb9d846b6
Containers:
web:
Container ID:   docker://8de5472c9605e5764276c345865ec52f9ec032e01ed58bc9a02de525af788acf
Image:          kahunacohen/hello-kube:latest
Image ID:       docker-pullable://kahunacohen/hello-kube@sha256:930dc2ca802bff72ee39604533342ef55e24a34b4a42b9074e885f18789ea736
Port:           3000/TCP
Host Port:      0/TCP
State:          Running
Started:      Mon, 12 Jul 2021 12:22:27 +0300
Ready:          True
Restart Count:  0
Environment:    <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-tcqwz (ro)
Conditions:
Type              Status
Initialized       True 
Ready             True 
ContainersReady   True 
PodScheduled      True 
Volumes:
kube-api-access-tcqwz:
Type:                    Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds:  3607
ConfigMapName:           kube-root-ca.crt
ConfigMapOptional:       <nil>
DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type    Reason     Age   From               Message
----    ------     ----  ----               -------
Normal  Scheduled  19m   default-scheduler  Successfully assigned default/web-deployment-5bb9d846b6-8k2s9 to minikube
Normal  Pulling    19m   kubelet            Pulling image "kahunacohen/hello-kube:latest"
Normal  Pulled     19m   kubelet            Successfully pulled image "kahunacohen/hello-kube:latest" in 2.3212119s
Normal  Created    19m   kubelet            Created container web
Normal  Started    19m   kubelet            Started container web

pod有一个容器正在运行expressjs,其中包含以下代码,该代码试图打印出配置映射中设置的env-vars:

const process = require("process");
const express = require("express");
const app = express();

app.get("/", (req, res) => {
res.send(`<h1>Kubernetes Expressjs Example 0.3</h2>
<h2>Non-Secret Configuration Example</h2>
<p>This uses ConfigMaps as env vars.</p>
<ul>
<li>MY_NON_SECRET: "${process.env.MY_NON_SECRET}"</li>
<li>MY_OTHER_NON_SECRET: "${process.env.MY_OTHER_NON_SECRET}"</li>
</ul>
`);
});

app.listen(3000, () => {
console.log("Listening on http://localhost:3000");
})

当我部署这些pod时,env-var是undefined

当我做$ kubectl exec {POD_NAME} -- env

我没有看到我的env-var。

我做错了什么?我试过杀死吊舱,等到它们重新启动,然后再次检查,但没有成功。

看起来您的pod是由web-deployment部署管理的。你不能直接修补这样的吊舱。

如果运行kubectl get pod <pod-name> -n <namespace> -oyaml,您将在metadata部分下看到一个名为ownerReferences的块。这告诉你谁是这个吊舱的所有者/管理者。

在部署的情况下,这里是所有权层次结构:

部署->副本集->吊舱

即部署创建replicaset,replicaset反过来创建pod。

因此,如果你想更改pod Spec中的任何内容,你应该在部署中进行更改,而不是直接在replicaset或pod中进行,因为它们会被覆盖。

通过运行并编辑环境字段来修补您的部署:

kubectl edit deployment.apps <deployment-name> -n <namespace>

或者用您的更改更新部署yaml并运行

kubectl apply -f <deployment-yaml-file>

最新更新