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

Tut*_*kul 6 yaml kubernetes

我正在做一个关于Google Cloud中的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
Run Code Online (Sandbox Code Playgroud)

hig*_*ita 36

yamllint 包对于调试和查找此类错误很有用,只需执行yamllint filename它,它就会列出它发现的可能问题。通过您的发行版包管理器(如果可用,通常推荐)或通过以下 npm install 命令安装(它将全局安装)

npm install -g yaml-lint

感谢 Kyle VG 的 npm 命令

  • 对于 Ubuntu 用户`sudo apt-get update && sudo apt-get install yamllint -y`。 (4认同)
  • 对于 Mac 用户,请使用“brew install yamllint”进行安装。 (4认同)
  • 对于 Centos7 用户,`sudo yum check-update && sudo yum install yamllint` (2认同)

Laz*_*ass 9

我在为Ingress使用 Helm创建 yaml 文件时遇到该错误。我有这样的东西作为我的 Ingress 规范

spec:
  tls:
  - hosts:
    - {{ .Values.ingress.host }}
Run Code Online (Sandbox Code Playgroud)

并在 values.yaml 中

ingress:
  host: "[NAMESPACE]-example.com"
Run Code Online (Sandbox Code Playgroud)

原来是括号在哪里导致错误。

该问题可以通过使用该quote函数在值上加上引号来解决。

- {{ .Values.ingress.host | quote }}
Run Code Online (Sandbox Code Playgroud)

这也是Helm 文档推荐的

避免类型转换错误的最简单方法是对字符串进行显式处理,对其他所有内容进行隐式处理。或者,简而言之,引用所有字符串

当您处理字符串数据时,引用字符串总是比将它们保留为空词更安全:


Ank*_*nde 8

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

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
Run Code Online (Sandbox Code Playgroud)

  • 我当时想,在打孔卡时代,FORTRAN 已经遗留了缩进问题。如果人们能醒悟并使用 TABS 而不是空格就好了!:) (4认同)
  • YAML 中不允许使用制表符 - “为了保持可移植性,制表符不得在缩进中使用,因为不同的系统以不同方式对待制表符。”请参阅 [spec](https://yaml.org/spec/1.2/spec.html) (3认同)

gui*_*izo 8

以下higuita的回答您可以 lint yaml 并检查错误,而无需使用npx在您的机器中安装模块。对于我不打算经常使用的命令,我更喜欢这种方法。NPX 下载包,执行命令并在完成后删除包。

npx yaml-lint yamllint file_name
Run Code Online (Sandbox Code Playgroud)