yaml文件
YAML 是专门用来编写配置文件的语言,实质上是一种通用的数据串行化格式。
语法规则:
在 Kubernetes 中,基本只用到【字典/列表】两种结构类型。
# Map 字典,是 key:value 的键值对---apiVersion: v1kind: Podmetadata:name: c-sitelabels:app: web# 转换成 JSON 文件{"apiVersion": "v1","kind": "Pod","metadata": {"name": "c-site","labels": {"app": "web"}}}# Lists 列表,可以有任意数量的项在列表中,每个项的定义以破折号(-)开头的,与父元素之间可以缩进也可以不缩进。args- Cat- Dog- Fish# 对应的 JSON 格式{"args": [ 'Cat', 'Dog', 'Fish' ]}
Lists 的子项也可以是 Maps,Maps 的子项也可以是 Lists 。
#定义一个叫 containers 的 List 对象,每个子项都由 name、image、ports 组成,每个 ports 都有一个 key 为 containerPort 的 Map 组成
---
apiVersion: v1
kind: Pod
metadata:
name: ydzs-site
labels:
app: web
spec:
containers:
- name: front-end
image: nginx
ports:
- containerPort: 80
- name: flaskapp-demo
image: cnych/flaskapp
ports:
- containerPort: 5000
#转成 JSON 格式文件
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "ydzs-site",
"labels": {
"app": "web"
}
},
"spec": {
"containers": [{
"name": "front-end",
"image": "nginx",
"ports": [{
"containerPort": "80"
}]
}, {
"name": "flaskapp-demo",
"image": "cnych/flaskapp",
"ports": [{
"containerPort": "5000"
}]
}]
}
}
编写yaml
编写符合 Kubernetes API 对象的资源清单要清楚资源对象的功能和字段。可通过 kubectl 命令行工具来获取字段信息。
# kubectl explain deployment
KIND: Deployment
VERSION: apps/v1
DESCRIPTION:
Deployment enables declarative updates for Pods and ReplicaSets.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object>
Standard object metadata.
spec <Object>
Specification of the desired behavior of the Deployment.
status <Object>
Most recently observed status of the Deployment.
spec
