Why does Kubernetes report: "configmap references non-existent config key"?

Pas*_*cal 0 kubernetes kubernetes-pod configmap

In Kubernetes I create a configmap using:

kubectl create configmap dd-agent-config --from-file=./kubernetes/datadog/configmap.yaml

configmap.yaml:

kind: ConfigMap
apiVersion: v1
metadata:
  name: dd-agent-config
  namespace: default
data:
  etcd: |-
    ad_identifiers:
      - etcd
    init_config:
    instances:
      - prometheus_url: "http://%%host%%:2379/metrics"
        ssl_cert: "/etc/kubernetes/pki/etcd/peer.crt"
        ssl_private_key: "/etc/kubernetes/pki/etcd/peer.key"
        ssl_ca_cert: "/etc/kubernetes/pki/etcd/ca.crt"
Run Code Online (Sandbox Code Playgroud)

The description of the configmap loaded in Kubernetes:

$ kubectl describe configmap dd-agent-config               
Name:         dd-agent-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
configmap.yaml:
----
kind: ConfigMap
apiVersion: v1
metadata:
  name: dd-agent-config
  namespace: default
data:
  etcd: |-
    ad_identifiers:
      - etcd
    init_config:
    instances:
      - prometheus_url: "http://%%host%%:2379/metrics"
        ssl_cert: "/etc/kubernetes/pki/etcd/peer.crt"
        ssl_private_key: "/etc/kubernetes/pki/etcd/peer.key"
        ssl_ca_cert: "/etc/kubernetes/pki/etcd/ca.crt"
Events:  <none>
Run Code Online (Sandbox Code Playgroud)

My pod definition:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: datadog-agent
spec:
  selector:
    matchLabels:
      app: datadog-agent
  template:
    metadata:
      labels:
        app: datadog-agent
      name: datadog-agent
    spec:
      serviceAccountName: datadog-agent
      containers:
        - image: datadog/agent:7
          name: datadog-agent
          [...]
          volumeMounts:
            - name: confd-config
              mountPath: /conf.d/etcd.d
      volumes:
        - name: confd-config
          configMap:
            name: dd-agent-config
            items:
              - key: etcd
                path: auto_conf.yaml
Run Code Online (Sandbox Code Playgroud)

Somehow this pod is unable to see the configmap key 'etcd':

$ kubectl get events 
0s          Warning   FailedMount         pod/datadog-agent-t56lp                       MountVolume.SetUp failed for volume "confd-config" : configmap references non-existent config key: etcd
Run Code Online (Sandbox Code Playgroud)

and the pod stays in the state of "ContainerCreating"

$ kubectl get pods
NAMESPACE     NAME                                     READY   STATUS              RESTARTS   AGE
default       datadog-agent-t56lp                      0/1     ContainerCreating   0          64s
Run Code Online (Sandbox Code Playgroud)

Pas*_*cal 5

我发现我创建configmap的方式是错误的。

而不是像这样创建它:

kubectl create configmap dd-agent-config --from-file=./kubernetes/datadog/configmap.yaml

我应该这样创建它:

kubectl create -f ./kubernetes/datadog/configmap.yaml