yaml文件
k8s集群中对资源管理和资源对象编排部署都可以通过声明样式(YAML)文件来解决,也就是可以把需要对资源对象操作编辑到YAML格式文件中,我们把这种文件叫做资源文件清单,通过kubectl命令直接使用资源清单文件就可以实现对大量的资源对象进行编排部署了。
YAML文件书写格式
(1)YAML介绍
YAML:仍是一种标记语言。为了强调这种语言以数据作为中心,而不是以标记语言为重点。
YAML:是一个可读性高,用来表达数据序列的格式。
(2)YAML基本语法
- 使用空格作为缩进,通过缩进表示层级关系
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
- 不能使用Tab进行缩进,只能使用空格
- 一般开头缩进两个空格
- 字符后缩进一个空格,比如冒号,逗号等后面
- 使用—表示新的yaml文件的开始
- 使用#代表注释
YAML文件组成部分
- 控制器定义
- 被控制对象
常用字段含义
名称 | 解析 |
---|---|
apiVersion | api版本 |
kind | 资源类型 |
metadata | 资源元数据 |
spec | 资源规格 |
replicas | 副本梳理 |
selector | 标签选择器 |
template | Pod模版 |
metadata | Pod元数据 |
spec | Pod规格 |
containers | 容器配置 |
如何快速编写yaml文件
第一种 使用kubectl create 命令生成yaml文件
[root@k8smaster ~]# kubectl create deployment web --image=nginx -o yaml --dry-run > my1.yaml
W1213 08:52:50.911614 2655 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
第二种 使用kubectl get命令导出yaml文件,已经部署好的项目
[root@k8smaster ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 22h
[root@k8smaster ~]# kubectl get deploy -o=yaml --export > my2.yaml
Flag --export has been deprecated, This flag is deprecated and will be removed in future.
[root@k8smaster ~]# cat my2.yaml
apiVersion: v1
items:
- apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2021-12-12T02:45:42Z"
generation: 1
labels:
app: nginx
managedFields:
- apiVersion: apps/v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:labels:
.: {}
f:app: {}
f:spec:
f:progressDeadlineSeconds: {}
f:replicas: {}
f:revisionHistoryLimit: {}
f:selector:
f:matchLabels:
.: {}
f:app: {}
f:strategy:
f:rollingUpdate:
.: {}
f:maxSurge: {}
f:maxUnavailable: {}
f:type: {}
f:template:
f:metadata:
f:labels:
.: {}
f:app: {}
f:spec:
f:containers:
k:{"name":"nginx"}:
.: {}
f:image: {}
f:imagePullPolicy: {}
f:name: {}
f:resources: {}
f:terminationMessagePath: {}
f:terminationMessagePolicy: {}
f:dnsPolicy: {}
f:restartPolicy: {}
f:schedulerName: {}
f:securityContext: {}
f:terminationGracePeriodSeconds: {}
manager: kubectl
operation: Update
time: "2021-12-12T02:45:42Z"
- apiVersion: apps/v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
f:deployment.kubernetes.io/revision: {}
f:status:
f:availableReplicas: {}
f:conditions:
.: {}
k:{"type":"Available"}:
.: {}
f:lastTransitionTime: {}
f:lastUpdateTime: {}
f:message: {}
f:reason: {}
f:status: {}
f:type: {}
k:{"type":"Progressing"}:
.: {}
f:lastTransitionTime: {}
f:lastUpdateTime: {}
f:message: {}
f:reason: {}
f:status: {}
f:type: {}
f:observedGeneration: {}
f:readyReplicas: {}
f:replicas: {}
f:updatedReplicas: {}
manager: kube-controller-manager
operation: Update
time: "2021-12-12T02:46:04Z"
name: nginx
namespace: default
resourceVersion: "1250"
selfLink: /apis/apps/v1/namespaces/default/deployments/nginx
uid: ce1ffdda-c172-4e2a-8fc3-eb11aec3cf83
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 1
conditions:
- lastTransitionTime: "2021-12-12T02:46:04Z"
lastUpdateTime: "2021-12-12T02:46:04Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
- lastTransitionTime: "2021-12-12T02:45:42Z"
lastUpdateTime: "2021-12-12T02:46:04Z"
message: ReplicaSet "nginx-f89759699" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
observedGeneration: 1
readyReplicas: 1
replicas: 1
updatedReplicas: 1
kind: List
metadata:
resourceVersion: ""
selfLink: ""