虽然用过、修改过现成的yaml文件,但这个东西一次没纯手打真的不行!以下是一个非常简单且漏洞百出的yaml文件|||
# WRONG example version 1
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx1
namespace:default
spec:
replicas: 3
selector:
matchLabels:
app: nginx2
template:
metadata:
labels:
app: nginx2
spec:
containers:
- name: nginx3
image: nginx
Round1 第一次执行报了这么个错
[root@k8s-master ~]# kubectl apply -f deploymentNginx.yaml
error: error parsing deploymentNginx.yaml: error converting YAML to JSON: yaml: line 7: could not find expected ':'
[root@k8s-master ~]#
那就看看第7行吧~ 第7行的冒号没什么问题,但是上面一行的冒号后少了个空格
冒号后如果有值,冒号后得有个空格
Round2 修改后执行执行依旧报错
[root@k8s-master ~]# kubectl apply -f deploymentNginx.yaml
error: error parsing deploymentNginx.yaml: error converting YAML to JSON: yaml: line 11: found character that cannot start any token
[root@k8s-master ~]#
用vim +11 deploymentNginx.yaml并没有看出什么问题,用cat -A看看有没有特殊字符
用Notepad++编辑后丢上Linux环境的。。复现了一下,虽然注意使用空格,但是回车的时候出现了一个Tab键
不能有Tab键!
尽量使用Unix格式吧,然后Notepad++的Tab键可以设置一下,设置-> 首选项-> 语言
Round3 在Linux环境编辑后执行又又又报错
[root@k8s-master ~]# kubectl apply -f deploymentNginx.yaml
error: error validating "deploymentNginx.yaml": error validating data: [ValidationError(Deployment.spec): missing required field "template" in io.k8s.api.apps.v1.DeploymentSpec, ValidationError(Deployment): unknown field "template" in io.k8s.api.apps.v1.Deployment]; if you choose to ignore these errors, turn validation off with --validate=false
[root@k8s-master ~]#
仔细看看,template这个块的缩进不对
缩进要正确!
再执行一次,终于对了
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx1
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx2
template:
metadata:
labels:
app: nginx2
spec:
containers:
- name: nginx3
image: nginx
nginx123之所以写成不一样是为了起来之后方便观察对应关系,注意nginx2的两个地方的标签要一致,使用 kubectl get deployment -o wide 查看