yaml基本语法
Kubernetes 支持 YAML 和 JSON格式 管理资源对象
JSON 格式:主要用于 api 接口之间消息的传递
YAML 格式:用于配置和管理,YAML是一种简洁的非标记性语言,内容格式人性化,较易读。
YAML语法格式:
大小写敏感;
使用缩进表示层级关系;不支持Tab键制表符缩进,只使用空格缩进;
缩进的空格数目不重要,只要相同层级的元素左侧对齐即可,通常开头缩进两个空格;
字符后缩进一个空格,如冒号,逗号,短横杆(-) 等
"---" 表示YAML格式,一个文件的开始,用于分隔文件; 可以将创建多个资源写在同一个 yaml 文件中,用 --- 隔开,就不用写多个 yaml 文件了。
"#” 表示注释;
yaml 文件的学习方法:
多看别人(官方)写的,能读懂
能照着现场的文件改着用
遇到不懂的,善用 kubectl explain ...命令查.
下面是一份nginx服务的详解。一份标准的yaml文件
kind: Deployment #类型,是deployment控制器,kubectl explain Deployment
apiVersion: apps/v1 #API版本,# kubectl explain Deployment.apiVersion
metadata: #pod的元数据信息,kubectl explain Deployment.metadata
labels: #自定义pod的标签,# kubectl explain Deployment.metadata.labels
app: nginx-deployment-label #标签名称为app值为nginx-deployment-label,后面会用到此标签
name: nginx-deployment #pod的名称
namespace: nginx-demo #pod的namespace,默认是defaule
spec: #定义deployment中容器的详细信息,kubectl explain Deployment.spec
replicas: 1 #创建出的pod的副本数,即多少个pod,默认值为1
selector: #定义标签选择器
matchLabels: #定义匹配的标签,必须要设置
app: nginx-selector #匹配的目标标签,
template: #定义模板,必须定义,模板是起到描述要创建的pod的作用
metadata: #定义模板元数据
labels: #定义模板label,Deployment.spec.template.metadata.labels
app: nginx-selector #定义标签,等于Deployment.spec.selector.matchLabels
spec: #定义pod信息
containers:#定义pod中容器列表,可以多个至少一个,pod不能动态增减容器
- name: nginx-container #容器名称
image: harbor.host.com/base/nginx-web1:v1 #镜像地址
#command: ["/apps/tomcat/bin/run_tomcat.sh"] #容器启动执行的命令或脚本
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always #拉取镜像策略IfNotPresent、Always、Nerver
ports: #定义容器端口列表
- containerPort: 80 #定义一个端口
protocol: TCP #端口协议
name: http #端口名称
- containerPort: 443 #定义一个端口
protocol: TCP #端口协议
name: https #端口名称
env: #配置环境变量
- name: "password" #变量名称。必须要用引号引起来
value: "123456" #当前变量的值
- name: "age" #另一个变量名称
value: "18" #另一个变量的值
resources: #对资源的请求设置和限制设置
limits: #资源限制设置,上限
cpu: 500m #cpu的限制,单位为core数,可以写0.5或者500m等CPU压缩值
memory: 2Gi #内存限制,单位可以为Mib/Gib,将用于docker run --memory参数
requests: #资源请求的设置
cpu: 200m #cpu请求数,容器启动的初始可用数量,可以写0.5或者500m等CPU压缩值
memory: 512Mi #内存请求大小,容器启动的初始可用数量,用于调度pod时候使用
---
kind: Service #类型为service
apiVersion: v1 #service API版本, service.apiVersion
metadata: #定义service元数据,service.metadata
labels: #自定义标签,service.metadata.labels
app: linux66-nginx #定义service标签的内容
name: linux66-nginx-spec #定义service的名称,此名称会被DNS解析
namespace: nginx-demo #该service隶属于的namespaces名称,即把service创建到哪个namespace里面
spec: #定义service的详细信息,service.spec
type: NodePort #service的类型,定义服务的访问方式,默认为ClusterIP, service.spec.type
ports: #定义访问端口, service.spec.ports
- name: http #定义一个端口名称
port: 80 #service 80端口
protocol: TCP #协议类型
targetPort: 80 #目标pod的端口
nodePort: 30001 #node节点暴露的端口
- name: https #SSL 端口
port: 443 #service 443端口
protocol: TCP #端口协议
targetPort: 443 #目标pod端口
nodePort: 30043 #node节点暴露的SSL端口
selector: #service的标签选择器,定义要访问的目标pod
app: nginx-selector #将流量路到选择的pod上,须等于Deployment.spec.selector.matchLabels