前言:YAML(/ˈjæməl/,尾音类似 camel ) 是"YAML不是一种标记语言"的外语缩写 (见前方参考资料原文内容);但为了强调这种语言以数据做为中心,而不是以置标语言为重点,而用返璞词重新命名。它是一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言
它是类似于标准通用标记语言的子集XML的数据描述语言,语法比XML简单很多
YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822
Clark Evans在2001年5月在首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者
YAML是"YAML Ain’t a Markup Language"(YAML不是一种置标语言)的递归缩写
在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种置标语言)
YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表,标量等资料形态
它使用空白符号缩排和大量依赖外观的特色,特别适合用来表达或编辑数据结构、各种设定档、倾印除错内容、文件大纲(例如:许多电子邮件标题格式和YAML非常接近)
尽管它比较适合用来表达阶层式(hierarchical model)的数据结构,不过也有精致的语法可以表示关联性(relational model)的资料
由于YAML使用空白字符和分行来分隔资料,使得它他特别适合用grep、Python、Perl、Ruby操作
其让人最容易上手的特色是巧妙避开各种封闭符号,如:引号、各种括号等,这些符号在嵌套结构中会变得复杂而难以辨认
数据结构可以用类似大纲的缩排方式呈现,结构通过缩进来表示,连续的项目通过减号"-“来表示,map结构里面的key/value对用冒号”:"来分隔
注意:
语法格式:
缩进标识层级关系
不支持制表符缩进,使用空格缩进
通常开头缩进两个空格
字符后缩进一个空格,如冒号,逗号等
“---”表示YAML格式,一个文件的开始
“#”表示注释
脚本语言
序列化
配置文件
比如Ruby on Rails的配置就选用的YAML。对ROR而言,这很自然,也很省事
由于兼容性问题,不同语言间的数据流转建议不要用YAML
[root@master01 ~]# kubectl api-versions //查看应用版本名称
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
[root@master01 ~]# mkdir demo
[root@master01 ~]# cd demo/
[root@master01 demo]# vim nginx-deployment.yaml //创建一个nginx控制器yaml文件
apiVersion: apps/v1 //应用名称,版本为v1
kind: Deployment //类型为控制器
metadata: //指定源信息
name: nginx-deployment //源信息的名称
labels: //指定标签
app: nginx //标签为nginx,关联到pod资源
spec: //规格
replicas: 3 //副本集为3
selector: //选择器
matchLabels: //标签
app: nginx //也是指向nginx
template: //分界线,上面指定的控制器的资源管理,下面指定的pod的资源管理控制
metadata:
labels:
app: nginx //标签
spec:
containers: //容器
- name: nginx //名称
image: nginx:1.15.4 //镜像
ports:
- containerPort: 80 //容器端口
[root@master01 demo]# kubectl create -f nginx-deployment.yaml //-f指定yaml文件
deployment.apps/nginx-deployment created
[root@master01 demo]# kubectl get pods //查看容器
NAME READY STATUS RESTARTS AGE
nginx-deployment-d55b94fd-82s87 1/1 Running 0 88s
nginx-deployment-d55b94fd-9sfnz 1/1 Running 0 88s
nginx-deployment-d55b94fd-sv569 1/1 Running 0 88s
[root@master01 demo]# kubectl get deploy //查看控制器资源
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 9d
nginx-deployment 3 3 3 3 95s
[root@master01 demo]# vim nginx-service.yaml //创建一个nginx服务资源yaml文件用于发布
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
type: NodePort //可以发布的类型
ports:
- port: 80 //容器暴露的端口
targetPort: 80 //负载均衡器的端口
selector:
app: nginx
[root@master01 demo]# kubectl create -f nginx-service.yaml
service/nginx-service created
[root@master01 demo]# kubectl get svc //可以看到对外的端口为30667
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 23d
nginx-service NodePort 10.0.0.206 80:30667/TCP 9s
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.4
ports:
- containerPort: 80
--- //用---表示分隔
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: nginx
//自动测试命令的正确性,并不执行创建
[root@master01 demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-deployment created (dry run) //dry run代表用来测试
//用-o yaml生成格式
[root@master01 demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
apiVersion: apps/v1beta1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: nginx-deployment
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
run: nginx-deployment
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: nginx-deployment
spec:
containers:
- image: nginx
name: nginx-deployment
ports:
- containerPort: 80
resources: {}
status: {}
//用-o json生成格式
[root@master01 demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o json
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
{
"kind": "Deployment",
"apiVersion": "apps/v1beta1",
"metadata": {
"name": "nginx-deployment",
"creationTimestamp": null,
"labels": {
"run": "nginx-deployment"
}
},
"spec": {
"replicas": 3,
"selector": {
"matchLabels": {
"run": "nginx-deployment"
}
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"run": "nginx-deployment"
}
},
"spec": {
"containers": [
{
"name": "nginx-deployment",
"image": "nginx",
"ports": [
{
"containerPort": 80
}
],
"resources": {}
}
]
}
},
"strategy": {}
},
"status": {}
}
[root@master01 demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml>mydeployment.yaml
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
[root@master01 demo]# ls
mydeployment.yaml//格式生成成功 nginx-deployment.yaml nginx-service.yaml
//将现有的资源生成模板导出
[root@master01 demo]# kubectl get deploy/nginx --export -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: null
generation: 1
labels:
run: nginx
name: nginx
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 2
selector:
matchLabels:
run: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
run: nginx
spec:
containers:
- image: nginx:latest
imagePullPolicy: Always
name: nginx
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status: {}
//保存到文件中
[root@master01 demo]# kubectl get deploy/nginx --export -o yaml>my-deploy.yaml
[root@master01 demo]# ls
mydeployment.yaml my-deploy.yaml//生成成功 nginx-deployment.yaml nginx-service.yaml
//查看字段帮助信息
[root@localhost demo]# kubectl explain pods.spec.containers