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

2019-07-19 15:20发布

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.

3条回答
Rolldiameter
2楼-- · 2019-07-19 15:47

Create ConfigMap from file.

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

You can check more examples on kubernetes docs

查看更多
等我变得足够好
3楼-- · 2019-07-19 15:56

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!!!.

查看更多
神经病院院长
4楼-- · 2019-07-19 16:11

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

查看更多
登录 后发表回答