为什么 - image 没有缩进,而是与 containers 对齐?
为什么 - image 没有缩进,而是与 containers 对齐?
apiVersion: v1
kind: Pod
metadata:
name: busy-pod
labels:
owner: chrono
env: prod
region: north
tier: back
selfdefined: important
spec:
containers:
- image: docker.nastool.de/library/busybox:latest
name: busy
imagePullPolicy: IfNotPresent
env:
- name: os
value: "ubuntu"
- name: debug
value: "on"
command:
- /bin/echo
args:
- "$(os), $(debug)"
The example below shows a combination of sequences and mappings using two spaces of indentation. Note how the sequence assigned to the mapping is indented.
labels:
- name: "bug"
color: "ee0701"
- name: "enhancement"
color: "0e8a16"
repository:
allow_merge_commit: true
allow_rebase_merge: false
default_branch: "main"
The example below shows a combination of sequences and mappings using two spaces of indentation. Note how the sequence assigned to the mapping does not use indentation.
labels:
- name: "bug"
color: "ee0701"
- name: "enhancement"
color: "0e8a16"
repository:
allow_merge_commit: true
allow_rebase_merge: false
default_branch: "main"
According to the YAML specification, the above examples are equivalent.
也就是说使用两个空格的缩进和不使用缩进是等价的。
spec:
containers:
- image: docker.nastool.de/library/busybox:latest
和
spec:
containers:
- image: docker.nastool.de/library/busybox:latest
注意这里不仅要将 - image 缩进两个空格,属于同一 block 的 name、imagePullPolicy、env 等同样要缩进两个空格。
apiVersion: v1
kind: Pod
metadata:
name: busy-pod
labels:
owner: chrono
env: prod
region: north
tier: back
selfdefined: important
spec:
containers:
- image: docker.nastool.de/library/busybox:latest
name: busy
imagePullPolicy: IfNotPresent
env:
- name: os
value: "ubuntu"
- name: debug
value: "on"
command:
- /bin/echo
args:
- "$(os), $(debug)"
使用 - image 有两空格缩进的 busy-pod.yml 文件来启动 pod,从下面的输出中可知,启动成功。
$ kubectl apply -f busy-pod.yml
pod/busy-pod created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
busy-pod 0/1 CrashLoopBackOff 2 (22s ago) 39s
$ kubectl logs busy-pod
ubuntu, on
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现