error converting YAML to JSON, did not find expected key kubernetes

KubernetesYaml

Kubernetes Problem Overview


I am doing a lab about kubernetes in google cloud.
I have create the YAML file, but when I am trying to deploy it a shell shows me this error:

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

YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
    spec:
      volumes:
      - name: nginx-config
        configMap:
          name: nginx-config
      - name: php-config
        configMap:
          name: php-config
      containers:
      - image: php-fpm:7.2
        name: php
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: persistent-storage
          mountPath: /var/www/data
        - name: php-config
          mountPath: /usr/local/etc/php-fpm.d/www.conf
          subPath: www.conf
      - image: nginx:latest
        name: nginx
        - containerPort: 80
        volumeMounts:
        - name: persistent-storage
          mountPath: /var/www/data
        - name: nginx-config
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
      volumes:
        - name: persistent-storage
          persistentVolumeClaim:
            claimName: nfs-pvc

Kubernetes Solutions


Solution 1 - Kubernetes

yamllint package is useful to debug and find this kind of errors, just do yamllint filename and it will list the possible problems it finds. Install via your distro package manager (usually recommended if available) or via the below npm install command (it will install globally)

npm install -g yaml-lint

Thanks to Kyle VG for the npm command

Solution 2 - Kubernetes

The overall file looks good. There are some issues with indentation.

YAML file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
    spec:
      volumes:
      - name: nginx-config
        configMap:
          name: nginx-config
      - name: php-config
        configMap:
          name: php-config
      containers:
      - image: php-fpm:7.2
        name: php
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: persistent-storage
            # looks like indentation issue here                 
            mountPath: /var/www/data 
        - name: php-config
            # looks like indentation issue here                 
            mountPath: /usr/local/etc/php-fpm.d/www.conf
            subPath: www.conf
      - image: nginx:latest
        name: nginx
        - containerPort: 80
        volumeMounts:
        - name: persistent-storage
            mountPath: /var/www/data
        - name: nginx-config
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
      volumes:
        - name: persistent-storage
          persistentVolumeClaim:
            claimName: nfs-pvc

Solution 3 - Kubernetes

Following higuita's answer you can lint your yaml and check for errors without installing a module in your machine using npx. I prefer this approach for commands that I do not intend to use often. NPX downloads the package, executes the command and remove the package when finishes.

npx yaml-lint yamllint file_name

Solution 4 - Kubernetes

I got that error while creating a yaml file for an Ingress using Helm. I had something like this as my Ingress specification

spec:
  tls:
  - hosts:
    - {{ .Values.ingress.host }}

and in the values.yaml

ingress:
  host: "[NAMESPACE]-example.com"

Turned out that the brackets where causing the error.

The issue could be fixed by putting quotes on the value using the quote function.

- {{ .Values.ingress.host | quote }}

This is also what the Helm doc recommends >The easiest way to avoid type conversion errors is to be explicit about strings, and implicit about everything else. Or, in short, quote all strings.

and here >When you are working with string data, you are always safer quoting the strings than leaving them as bare words:

Solution 5 - Kubernetes

Although the above yaml file looked fine, the indentation was the issue(which is hardly visible when looking at the file). Another issue that could cause this error, is having a problem with the file, such as a missing field name, or collon.

Solution 6 - Kubernetes

I had the same problem, but I solve copying the link of the RAW Github file and set it on kubectl

kubectl create -f https://raw.githubusercontent.com/user/project/master/file.yml

Solution 7 - Kubernetes

Ensure you don't have any invisible characters which are causing you issue.

This error:

error converting YAML to JSON: yaml: line 96: could not find expected '':''

Was because of an invisible break between the : and the C in this line:

- name: CERT_ALIAS

Solution 8 - Kubernetes

Use a yaml parser to find the source of the problem

There is many online ones, like https://yaml-online-parser.appspot.com/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser10944669View Question on Stackoverflow
Solution 1 - KuberneteshiguitaView Answer on Stackoverflow
Solution 2 - KubernetesAnkit DeshpandeView Answer on Stackoverflow
Solution 3 - KubernetesguizoView Answer on Stackoverflow
Solution 4 - Kubernetesbrass monkeyView Answer on Stackoverflow
Solution 5 - KubernetesDeok FilhoView Answer on Stackoverflow
Solution 6 - KubernetesAntonio ThomacelliView Answer on Stackoverflow
Solution 7 - KubernetesLodlaidenView Answer on Stackoverflow
Solution 8 - KubernetesjobwatView Answer on Stackoverflow