为什么 - 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)"

参考 Indenting YAML files

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


posted @   甲兵匪已  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示