本文共 2019 字,大约阅读时间需要 6 分钟。
YAML Syntax
YAML(Yet Another Markup Language)是一种常用的数据序列化语言,广泛应用于配置文件、项目管理和自动化脚本等领域。YAML 在 Kubernetes 和 Helm 等工具中具有重要地位。
YAML 主有两种核心类型:字典和序列。
字典类似于 JSON 或 YAML 格式,通过键值对存储数据:
map: one: 1 two: 2 three: 3
序列(列表)则通过逗号分隔来存储项:
sequence: - one - two - three
YAML 还支持丰富的格式符,用于实现更复杂的数据格式处理,以下是常用的符号:
#
注释,支持单行注释,可以通过连字符开始(-)或numerator:
。|
用于控制多行文本的空格, 無請 :優先以原始文本格式呈现。coffee: / Latte Cappuccino Espresso#output:Latte\nCappuccino\nEspresso coffee: |- Latte Cappuccino Espresso#output: Latte\nCappuccino\nEspresso (with no trailing newline)coffee: |+ Latte Cappuccino Espresso#output: Latte\nCappuccino\nEspresso\n\r\n
### YAML 锚定(Anchors)YAML 提供了对值进行引用和再利用的机制, referred 为锚定。这种机制在多步骤流程中非常实用。```yamlcoffee: "yes, please"favorite: &favoriteCoffee "Cappucino"coffees: - Latte - *favoriteCoffee - Espresso
上述 YAML 表示:
coffee: yes, pleasefavorite: Cappucinocoffees: - Latte - Cappucino - Espresso
注意:在 Helm 和 Kubernetes 中,由于频繁读取、修改并重新生成 YAML 文件,锚定会被丢失,因此在这些环境下不宜过度使用。
在 Kubernetes 中,资源清单文件(Resource Definition File)通常包含以下字段:
Parameter Name | Explanation |
---|---|
containers[].name | 容器名称 |
containers[].image | 容器镜像名称 |
containers[].imagePullPolicy | 镜像拉取策略 |
containers[].command[] | 容器执行命令 |
containers[].args[] | 容器启动参数 |
containers[].wolumeMounts[].name | 挂载点名称 |
containers[].wolumeMounts[].mountPath | 挂载路径 |
containers[].ports[].name | 端口名称 |
containers[].ports[].containerPort | 容器端口 |
containers[].ports[].hostPort | 主机端口 |
containers[].ports[].protocol | 端口协议 |
containers[].env[].name | 环境变量名称 |
containers[].env[].value | 环境变量值 |
containers[].resources.requests.cpu | 请求CPU资源 |
containers[].resources.requests.memory | 请求内存资源 |
以下是一个通过 YAML 创建 Pod 的示例配置文件:
apiVersion: v1kind: ReplicationControllermetadata: name: kikisspec: replicas: 3 selector: app: kiki template: metadata: labels: app: kiki spec: containers: - name: kiki image: centos imagePullPolicy: IfNotPresent command: - "/bin/sh" - "-c" - "sleep 3600"
转载地址:http://qxnkk.baihongyu.com/