将YAML转换为JSON时出错,未找到预期的密钥kubernetes

时间:2019-02-01 12:19:37

标签: kubernetes yaml

我正在做一个关于谷歌云中kubernetes的实验室。
我已经创建了YAML文件,但是当我尝试部署它时,shell会显示此错误:

  

将YAML转换为JSON时出错:yaml:第34行:未找到预期的密钥

YAML文件:

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

5 个答案:

答案 0 :(得分:4)

yamllint软件包对于调试和发现此类错误非常有用,只需执行yamllint filename,它将列出它发现的可能问题

答案 1 :(得分:4)

按照higuita的回答,您可以使用Yaml和Yaml进行检查,而无需使用npx在计算机中安装模块。对于我不经常使用的命令,我更喜欢这种方法。 NPX下载软件包,执行命令并在完成后删除软件包。

npx yaml-lint yamllint file_name

答案 2 :(得分:1)

整个文件看起来不错。 我猜有一些缩进问题。

YAML文件

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

答案 3 :(得分:0)

尽管上面的yaml文件看起来不错,但缩进是问题所在(查看文件时几乎看不到)。另一个可能导致此错误的问题是文件出现问题,例如缺少字段名称或collon。

答案 4 :(得分:0)

我遇到了同样的问题,但我解决了复制 RAW Github 文件的链接并将其设置在 kubectl 上的问题

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