Kubernetes YAML 编写 讲解

简介: Kubernetes YAML 编写 讲解

Kubernetes YAML 编写 讲解

网络异常,图片无法展示
|

YAML 是一种非常简洁/强大/专门用来写配置文件的语言!

YAML 全称是 ”YAML Ain’t a Markup Language” 的递归缩写,该语言的设计参考了 JSON / XML 和 SDL 等语言,强调以数据为中心,简洁易读,编写简单。

YAML 语法特性

学过编程的人理解起来应该非常容易

语法特点

  • 大小写敏感
  • 通过缩进表示层级关系
  • 禁止使用 tab 缩进,只能使用空格键
  • 缩进的空格数目不重要,只要相同层级左对齐
  • 使用 #表示注释
# yamllanguages:    - Ruby    - Perl    - Pythonwebsites:    YAML: yaml.org    Ruby: ruby-lang.org    Python: python.org    Perl: use.perl.org
# Json{    languages: [        'Ruby',        'Perl',        'Python'    ],    websites: {        YAML: 'yaml.org',        Ruby: 'ruby-lang.org',        Python: 'python.org',        Perl: 'use.perl.org'    }}

数据结构

  • 对象: 键值对的字典
  • 数组: 一组按次序排列的列表
  • 纯量: 单个的且不可再分的值
# 纯量hello
# 数组- Cat- Dog- Goldfish
# 对象animal: pets

引号区别

  • 单引号(''): 特殊字符作为普通字符串处理
  • 双引号(""): 特殊字符作为本身想表示的意思
# 单引号name: 'Hi,\nTom'
# 双引号name: "Hi,\nTom"

内置类型列表

# YAML允许使用个感叹号(!)强制转换数据类型# 单叹号通常是自定义类型,双叹号是内置类型money: !!str123
date: !Booleantrue


YAML 中的纯量

纯量是最基本的且不可再分的值

字符串

# 不适用引号name: Tom
# 使用单引号name: 'Tom'
# 使用双引号name: "Tom"

布尔值

debug: truedebug: false

数字

12       # 十进制整数014      # 八进制整数0xC      #十六进制整数13.4     #浮点数1.2e+34  #指数.inf     #无穷大

Null

date: ~date: null

时间

# 使用iso-8601标准表示日期date: 2018-01-01t16:59:43.10-05:00

YAML 特殊类型


日常使用中基本不会用到的类型

文本块

# 注意“|”与文本之间须另起一行# 使用|标注的文本内容缩进表示的块,可以保留块中已有的回车换行value: |  hello  world!
# 输出结果# hello 换行 world!!


# +表示保留文字块末尾的换行# -表示删除字符串末尾的换行value: |hello
value: |-hello
value: |+hello
# 输出结果# hello\n hello hello\n\n


# 注意“>”与文本之间的空格# 使用>标注的文本内容缩进表示的块,将块中回车替换为空格最终连接成一行value: > helloworld!
# 输出结果# hello 空格 world!

锚点与引用

# 复制代码注意*引用部分不能追加内容# 使用&定义数据锚点,即要复制的数据# 使用*引用锚点数据,即数据的复制目的地name: &a yamlbook: *abooks:   - java   - *a   - python
# 输出结果book: yamlbooks:[java, yaml, python]

YAML 实例说明

光说不练假把式 => JS-Yaml 官网实例地址 https://nodeca.github.io/js-yaml


# Collection Types #############################################################################################################################################
# http://yaml.org/type/map.html -----------------------------------------------#
map:  # Unordered set of key: value pairs.  Block style: !!map    Clark: Evans    Ingy: döt Net    Oren: Ben-Kiki  Flow style: !!map { Clark: Evans, Ingy: döt Net, Oren: Ben-Kiki }
# http://yaml.org/type/omap.html ----------------------------------------------#
omap:  # Explicitly typed ordered map (dictionary).  Bestiary: !!omap    - aardvark: African pig-like ant eater. Ugly.    - anteater: South-American ant eater. Two species.    - anaconda: South-American constrictor snake. Scaly.    # Etc.  # Flow style  Numbers: !!omap [one: 1, two: 2, three: 3]
# http://yaml.org/type/pairs.html ---------------------------------------------#
pairs:  # Explicitly typed pairs.  Block tasks: !!pairs    - meeting: with team.    - meeting: with boss.    - break: lunch.    - meeting: with client.  Flow tasks: !!pairs [meeting: with team, meeting: with boss]
# http://yaml.org/type/set.html -----------------------------------------------#
set:  # Explicitly typed set.  baseball players: !!set    ? Mark McGwire    ? Sammy Sosa    ? Ken Griffey  # Flow style  baseball teams: !!set { Boston Red Sox, Detroit Tigers, New York Yankees }
# http://yaml.org/type/seq.html -----------------------------------------------#
seq:  # Ordered sequence of nodes  Block style: !!seq    - Mercury # Rotates - no light/dark sides.    - Venus # Deadliest. Aptly named.    - Earth # Mostly dirt.    - Mars # Seems empty.    - Jupiter # The king.    - Saturn # Pretty.    - Uranus # Where the sun hardly shines.    - Neptune # Boring. No rings.    - Pluto # You call this a planet?  Flow style: !!seq [      Mercury,      Venus,      Earth,      Mars, # Rocks      Jupiter,      Saturn,      Uranus,      Neptune, # Gas      Pluto,    ] # Overrated
# Scalar Types #################################################################################################################################################
# http://yaml.org/type/bool.html ----------------------------------------------#
bool:  - true  - True  - TRUE  - false  - False  - FALSE
# http://yaml.org/type/float.html ---------------------------------------------#
float:  canonical: 6.8523015e+5  exponentioal: 685.230_15e+03  fixed: 685_230.15  sexagesimal: 190:20:30.15  negative infinity: -.inf  not a number: .NaN
# http://yaml.org/type/int.html -----------------------------------------------#
int:  canonical: 685230  decimal: +685_230  octal: 02472256  hexadecimal: 0x_0A_74_AE  binary: 0b1010_0111_0100_1010_1110  sexagesimal: 190:20:30
# http://yaml.org/type/merge.html ---------------------------------------------#
merge:  - &CENTER { x: 1, y: 2 }  - &LEFT { x: 0, y: 2 }  - &BIG { r: 10 }  - &SMALL { r: 1 }
  # All the following maps are equal:
  - # Explicit keys    x: 1    y: 2    r: 10    label: nothing
  - # Merge one map    <<: *CENTER    r: 10    label: center
  - # Merge multiple maps    <<: [*CENTER, *BIG]    label: center/big
  - # Override    <<: [*BIG, *LEFT, *SMALL]    x: 1    label: big/left/small
# http://yaml.org/type/null.html ----------------------------------------------#
null:  # This mapping has four keys,  # one has a value.  empty:  canonical: ~  english: null  ~: null key  # This sequence has five  # entries, two have values.  sparse:    - ~    - 2nd entry    -    - 4th entry    - Null
# http://yaml.org/type/str.html -----------------------------------------------#
string: abcd
# http://yaml.org/type/timestamp.html -----------------------------------------#
timestamp:  canonical: 2001-12-15T02:59:43.1Z  valid iso8601: 2001-12-14t21:59:43.10-05:00  space separated: 2001-12-14 21:59:43.10 -5  no time zone (Z): 2001-12-15 2:59:43.10  date (00:00:00Z): 2002-12-14
# JavaScript Specific Types ####################################################################################################################################
# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp
regexp:  simple: !!js/regexp foobar  modifiers: !!js/regexp /foobar/mi
# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined
undefined: !!js/undefined ~
# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function
function: !!js/function >  function foobar() {    return 'Wow! JS-YAML Rocks!';  }
# Custom types #################################################################################################################################################
# JS-YAML allows you to specify a custom YAML types for your structures.# This is a simple example of custom constructor defined in `js/demo.js` for# custom `!sexy` type:## var SexyYamlType = new jsyaml.Type('!sexy', {#   kind: 'sequence',#   construct: function (data) {#     return data.map(function (string) { return 'sexy ' + string; });#   }# });## var SEXY_SCHEMA = jsyaml.Schema.create([ SexyYamlType ]);## result = jsyaml.load(yourData, { schema: SEXY_SCHEMA });
foobar: !sexy  - bunny  - chocolate



相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
相关文章
|
6月前
|
Kubernetes Cloud Native 应用服务中间件
【云原生】使用k8s创建nginx服务—通过yaml文件svc类型暴露
【云原生】使用k8s创建nginx服务—通过yaml文件svc类型暴露
130 0
|
4月前
|
Kubernetes Cloud Native Docker
云原生|kubernetes|网络插件flannel二进制部署和calico的yaml清单部署总结版
云原生|kubernetes|网络插件flannel二进制部署和calico的yaml清单部署总结版
133 0
|
6月前
|
Kubernetes 应用服务中间件 nginx
kubernetes集群命令行工具kubectl介绍&yaml文件介绍-20230208
kubernetes集群命令行工具kubectl介绍&yaml文件介绍-20230208
|
7天前
|
JSON Kubernetes Go
IDEA使用Kubernetes插件编写YAML
IDEA使用Kubernetes插件编写YAML
21 0
IDEA使用Kubernetes插件编写YAML
|
6月前
|
Kubernetes Java 容器
k8s部署springboot项目yaml
k8s部署springboot项目yaml
120 0
|
6月前
|
Kubernetes NoSQL 关系型数据库
通过编写k8s的资源清单yaml文件部署gitlab服务
通过编写k8s的资源清单yaml文件部署gitlab服务
|
4月前
|
JSON Kubernetes API
k8s学习-k8s资源对象与yaml结构
k8s学习-k8s资源对象与yaml结构
53 0
|
4月前
|
存储 Kubernetes Perl
k8s(10)声明式对象配置--yaml文件
k8s(10)声明式对象配置--yaml文件
54 0
|
8月前
|
Kubernetes 应用服务中间件 调度
Kubernetes 中几种常见的调度方式以及对应的 YAML 示例
在 Kubernetes 中,使用 YAML 文件来定义和配置 Pod。以下是几种常见的调度方式以及对应的 YAML 示例: 1. 基于资源需求与节点容量的调度: ```yaml apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: my-container image: nginx resources: requests: cpu: "0.5" memory: "512Mi" limits: cpu
|
11月前
|
运维 Kubernetes Cloud Native