error converting YAML to JSON: yaml: line 10: did not find expected key

29,077

The simple pod example YAML for Kubernetes shows that the 'metadata' and 'spec' elements required are at the top level of the definition. The kubectl command is most likely failing because it cannot find the 'spec' element, which defines the specification of the pod.

You seem to be testing the image pull configuration, and you have specified that you simply want to run echo SUCCESS inside the container. Considering both these conditions, it would be preferable to pull down bash image instead of the mysql image.

The following alternate YAML should work for your needs:

---
apiVersion: v1
kind: Pod
metadata:
  name: testing-for-image-pull
spec:
  containers:
  - name: bash
    image: bash
    imagePullPolicy: Always
    command: ["echo"]
    args: ["SUCCESS"]

The following changes have been made from the original YAML file: 1) The kind element has been corrected to the value Pod. 2) The name of the pod has been changed to fit Kubernetes requirements (lowercase DNS-like name). 3) The image and name elements have been modified to use the bash image. 4) The command definition has been changed to use the command and args keys instead.

Note that YAML uses spaces instead of tabs for indentation, and the suggested syntax for YAML is to use two spaces per level of indentation instead of the traditional four spaces.

For more example YAML files, refer to the Kubernetes website repository on GitHub.

Share:
29,077
PersianGulf
Author by

PersianGulf

My God is Allah and my religious book is Quran.Up to now my life was based on my God's will and whatever I gained or lost is due to my God's want. My aspiration is to put my life in favour of my lord, Allah since I believe this would be a desirable life. I am a fan and supporter of Free Software and i do all my computer works based on Freesoftware. My other interests are Linux/GNU family and BSD. My main speciality is network administration but I also have decent knowledge in C/C++ and Python progmming. In general I love programming languages. My favorite sport is mountain climbing because I love outdoors and generaly I'm a nature lover. I truely love to increase my knowledge because I'm eager to know as much as possible. Not only I have this desire for myself, but also for every human being and I will do my best to help anyone who wishes to be on this path. (You can find everythinf about me at http://pahlevanzadeh.net)

Updated on September 18, 2022

Comments

  • PersianGulf
    PersianGulf almost 2 years

    I have the following yaml file:

    ---
    apiVersion: v1
    kind: pod
    metadata:
        name: Tesing_for_Image_pull -----------> 1
        spec:
            containers:
            - name: mysql ------------------------> 2
              image: mysql ----------> 3
              imagePullPolicy: Always ------------->4
              command: ["echo", "SUCCESS"]  -------------------> 5
    

    After running kubectl create -f my_yaml.yaml I get the following error:

    error: error converting YAML to JSON: yaml: line 10: did not find expected key
    

    UPDATE: With yamllint I get the following error:

    root@debian:~# yamllint my_yaml.yaml
    my_yaml.yaml
      8:9       error    wrong indentation: expected 12 but found 8  (indentation)
      11:41     error    syntax error: expected <block end>, but found '<scalar>'
    

    Where is my problem and how can I solve it?

    • jesse_b
      jesse_b over 4 years
      What is with the - before name in line 8? And what is with all the ------------->'s?
  • PersianGulf
    PersianGulf over 4 years
    With your yaml file I got the error error: error validating "Tesing_for_Image_pull.yaml": error validating data: couldn't find type: v1.pod; if you choose to ignore these errors, turn validation off with --validate=false
  • Haxiel
    Haxiel over 4 years
    @PersianGulf I'm sorry, that was a typo I had missed. It should be kind: Pod, with a capital P. I'll fix my answer accordingly. I think this is a good time to point out that Kubernetes is generally terrible at producing meaningful error messages. The solutions are usually documented somewhere, though - case in point.
  • PersianGulf
    PersianGulf over 4 years
    I tested with capital 'P' and lower P, Both have error.
  • Haxiel
    Haxiel over 4 years
    @PersianGulf Sorry for the late reply, but I've done some testing and have included an alternate YAML in my answer. This one's been tested on a Minikube instance, so it should work correctly for you.
  • PersianGulf
    PersianGulf over 4 years
    With the above answer, solved my problem. But I have problem with devops.stackexchange.com/questions/10110/… , You make me happy if you answer it.