我正在做一个关于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 命令
我在为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 文档推荐的
避免类型转换错误的最简单方法是对字符串进行显式处理,对其他所有内容进行隐式处理。或者,简而言之,引用所有字符串。
与此
当您处理字符串数据时,引用字符串总是比将它们保留为空词更安全:
整体文件看起来不错。我猜有一些缩进问题。
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)
以下higuita
的回答您可以 lint yaml 并检查错误,而无需使用npx在您的机器中安装模块。对于我不打算经常使用的命令,我更喜欢这种方法。NPX 下载包,执行命令并在完成后删除包。
npx yaml-lint yamllint file_name
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14978 次 |
最近记录: |