k8s的yaml文件编辑避“坑”经验
2019-08-03 本文已影响0人
wu_sphinx
今天在命令行端手改k8s的资源文件的时候,老是出现写yaml出错的状况,很是头疼,比如以下
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: ng
name: ng
spec:
volumes:
- name: cf
configMap:
name: cmvolume
containers:
- image: nginx
name: ng
resources: {}
volumeMounts:
- name: cf
mountPath: /etc/lala
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
这在yaml语法是固然是没有错的,问题在于volumes那一块,我理解volumes下挂的是列表,但从写法上却是错的,转成json就很容易看出来了
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"creationTimestamp": null,
"labels": {
"run": "ng"
},
"name": "ng"
},
"spec": {
"volumes": [
{
"name": "cf",
"configMap": null
}
],
"containers": [
{
"image": "nginx",
"name": "ng",
"resources": {},
"volumeMounts": [
{
"name": "cf",
"mountPath": "/etc/lala"
}
]
}
],
"dnsPolicy": "ClusterFirst",
"restartPolicy": "Never"
},
"status": {}
}
其实还是一个格式的问题,对象跟列表傻傻分不清,还是需要梳理一下yaml的格式
-
k:v字典形式
"kind": "Pod"
对应的json如下:
{
"kind": "Pod"
}
- 列表形式
- pod
- svc
- deploy
对应的json如下:
[
"pod",
"svc",
"deploy"
]
- 复合结构
metadata:
creationTimestamp: null
labels:
run: ng
app: nginx
annotate:
- a: 1
b: 1
- a: 2
b: 2
对应json结构应为:
{
"metadata": {
"creationTimestamp": null,
"labels": {
"run": "ng",
"app": "nginx"
},
"annotate": [
{
"a": 1,
"b": 1
},
{
"a": 2,
"b": 2
}
]
}
}
有几点是比较容易写错
- 冒号后面一定要有空格
- 列表一定是有
-标识的 - 同一级的数据,起始位置一定是同一列
有了这些,现结合kubectl explain能很大程度避免写错
比如我刚才写错的地方可以参考
➜ ~ kubectl explain pod.spec.volumes.configMap
KIND: Pod
VERSION: v1
RESOURCE: configMap <Object>
DESCRIPTION:
ConfigMap represents a configMap that should populate this volume
Adapts a ConfigMap into a volume. The contents of the target ConfigMap's
Data field will be presented in a volume as files using the keys in the
Data field as the file names, unless the items element is populated with
specific mappings of keys to paths. ConfigMap volumes support ownership
management and SELinux relabeling.
FIELDS:
defaultMode <integer>
熟读文档,多练习,避“坑”能增进理解。
ps:
kubectl explain真的是非常棒。
refer: