-->

Can we use “data” as a yaml file instead of Json f

2019-07-19 15:54发布

问题:

lets take this example of a config map

apiVersion: v1
kind: ConfigMap
data:
  abc.yml: |-
<yml here>

Getting an error like failed to parse yaml to Json.

回答1:

Yes you can do that, but you should care about the syntax. You can also follow techniques for yaml from here.

If you use kubectl create configmap myconfig --from-file=abc.yml, then it is ok.

But if you write the whole yaml file for your configmap in myconfig.yaml and then run kubectl create -f myconfig.yaml, then you should care about syntax.

Say your abc.yml file is as followings:

a:
  b: b1
  c: c1
d: d1

Then write your myconfig.yaml file:

apiVersion: v1
kind: ConfigMap
data:
  abc.yml: |
    a:
      b: b1
      c: c1
    d: d1

Now just run kubectl create -f myconfig.yaml. That's it.

Happy Kubernetes!!!.



回答2:

Create ConfigMap from file.

kubectl create configmap myconfig --from-file=youfile.yml.

You can check more examples on kubernetes docs



回答3:

These could be the problems 1. most likely the issue could with the indentation. 2. remove '-' from abc.yml: |- and check

I followed the below steps and was able to load yaml file into configmap. it worked fine.

master $ cat c.yaml
apiVersion: v1
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru
kind: ConfigMap
metadata:
  name: example-redis-config
master $ kubectl create configmap  testcfg --from-file=./c.yaml
master $ kubectl get cm testcfg -oyaml
apiVersion: v1
data:
  c.yaml: |
    apiVersion: v1
    data:
      redis-config: |
        maxmemory 2mb
        maxmemory-policy allkeys-lru
    kind: ConfigMap
    metadata:
      name: example-redis-config
kind: ConfigMap
metadata:
  creationTimestamp: 2019-03-07T08:35:18Z
  name: testcfg
  namespace: default
  resourceVersion: "7520"
  selfLink: /api/v1/namespaces/default/configmaps/testcfg
  uid: f033536d-40b3-11e9-a67d-0242ac11005b