当前位置: 首页 > 工具软件 > Key Chain > 使用案例 >

Kubernetes基础:ConfigMap:无法找到key的对应方法

锺离声
2023-12-01

这篇文章memo一下错误写法导致无法进行环境变量映射的问题和对应方法。

现象

创建ConfigMap

准备如下设定内容

[root@host131 config]# kubectl get cm
No resources found in default namespace.
[root@host131 config]# cat user.properties 
user.name=liumiaocn
user.id=1002
[root@host131 config]#

使用kubectl create 命令创建ConfigMap,执行日志如下所示:

[root@host131 config]# kubectl create configmap user-configmap --from-file=user.properties 
configmap/user-configmap created
[root@host131 config]#

确认ConfigMap

使用kubectl get configmap即可获取已经创建的ConfigMap信息

[root@host131 config]# kubectl get configmap
NAME             DATA   AGE
user-configmap   1      24s
[root@host131 config]# kubectl describe configmap user-configmap
Name:         user-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
user.properties:
----
user.name=liumiaocn
user.id=1002

Events:  <none>
[root@host131 config]#

创建pod

创建pod并将user-configmap中的内容和环境变量进行关联

[root@host131 config]# cat busybox-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: configmap-test-pod
spec:
  containers:
    - name: busybox-container
      image: busybox:latest
      command: ["sleep", "1000"]
      env:
        - name: ENV_VAR_USERNAME
          valueFrom:
            configMapKeyRef:
              name: user-configmap
              key: user.name
        - name: ENV_VAR__ID
          valueFrom:
            configMapKeyRef:
              name: user-configmap
              key: user.id
  restartPolicy: Never
[root@host131 config]#

创建pod,提示如下错误

[root@host131 config]# kubectl get pods
NAME                 READY   STATUS                       RESTARTS   AGE
configmap-test-pod   0/1     CreateContainerConfigError   0          8s
[root@host131 config]# 

使用kubectl describe命令确认到如下错误信息

[root@host131 config]# kubectl describe pods configmap-test-pod
Name:         configmap-test-pod
Namespace:    default
Priority:     0
Node:         192.168.163.131/192.168.163.131
Start Time:   Fri, 03 Jan 2020 05:18:58 -0500
Labels:       <none>
Annotations:  <none>
Status:       Pending
IP:           10.254.80.4
IPs:
  IP:  10.254.80.4
Containers:
  busybox-container:
    Container ID:  
    Image:         busybox:latest
    Image ID:      
    Port:          <none>
    Host Port:     <none>
    Command:
      sleep
      1000
    State:          Waiting
      Reason:       CreateContainerConfigError
    Ready:          False
    Restart Count:  0
    Environment:
      ENV_VAR_USERNAME:  <set to the key 'user.name' of config map 'user-configmap'>  Optional: false
      ENV_VAR__ID:       <set to the key 'user.id' of config map 'user-configmap'>    Optional: false
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-zdtxx (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-zdtxx:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-zdtxx
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                From                      Message
  ----     ------     ----               ----                      -------
  Normal   Scheduled  17s                default-scheduler         Successfully assigned default/configmap-test-pod to 192.168.163.131
  Normal   Pulling    12s (x2 over 16s)  kubelet, 192.168.163.131  Pulling image "busybox:latest"
  Normal   Pulled     10s (x2 over 13s)  kubelet, 192.168.163.131  Successfully pulled image "busybox:latest"
  Warning  Failed     10s (x2 over 13s)  kubelet, 192.168.163.131  Error: couldn't find key user.name in ConfigMap default/user-configmap
[root@host131 config]#

对应方法

使用标准方式的ConfigMap的yml文件,动作执行正常。yml文件如下所示

[root@host131 config]# cat user.yml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: user-configmap
  namespace: default
data:
  user.name: liumiao
  user.id: '1001'
[root@host131 config]#

创建ConfigMap并确认

[root@host131 config]# kubectl create -f user.yml 
configmap/user-configmap created
[root@host131 config]# 
[root@host131 config]# kubectl describe cm user-config
Name:         user-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
user.id:
----
1001
user.name:
----
liumiao
Events:  <none>

[root@host131 config]# 

创建pod

[root@host131 config]# kubectl create -f busybox-pod.yaml 
pod/configmap-test-pod created
[root@host131 config]# 
[root@host131 config]# kubectl get pods
NAME                 READY   STATUS    RESTARTS   AGE
configmap-test-pod   1/1     Running   0          6s
[root@host131 config]# 

进入pod之中确认引用的环境变量,发现也是正常的

[root@host131 config]# kubectl exec -it configmap-test-pod sh
/ # env |grep ENV
ENV_VAR__ID=1001
ENV_VAR_USERNAME=liumiao
/ #
 类似资料: