键值对 – YAML 文件中的基本条目类型是键值对。键值对的格式是键和冒号,之后是空格,然后是值。
数组/列表 – 列表会在列表名称下列出一些项目。列表的元素以 - 开头。可以有 n 个列表,但是,数组中各个元素的缩进非常重要。
缩进标识层级关系
不支持制表符缩进,使用空格缩进
通常开头缩进两个空格
字符后缩进一个空格,如冒号,逗号等
“—”表示YAML格式,一个文件的开始
“#”表示注释
YAML文件开头需要编写标签信息,对应不同资源信息
[root@k8s_master ~]# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1 ##应用资源
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
例如创建一个nginx资源
编写yaml文件
[root@k8s_master ~]# vim nginx-deploy.yaml
apiVersion: apps/v1 ##版本号 ,pod资源
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 ##选择业务进行发布
执行创建
###测试yaml文件命令的正确性
[root@k8s_master ~]# kubectl create -f nginx-deploy.yaml --dry-run
deployment.apps/nginx-deployment created (dry run)
service/nginx-service created (dry run)
##执行创建
[root@k8s_master ~]# kubectl create -f nginx-deploy.yaml
deployment.apps/nginx-deployment created
service/nginx-service created
访问pod资源
##卡看容器对外端口
[root@k8s_master ~]# kubectl get all
利用nginx镜像创建资源,生成yaml格式文件
[root@k8s_master ~]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml
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: {}
利用nginx镜像创建资源,生成json格式文件
[root@k8s_master ~]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o json
{
"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": {}
}
将现有的资源生成模板导出到指定文件中(–export -o)
##查看现有资源
[root@k8s_master ~]# kubectl get pods,deploy
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-d55b94fd-5jvtq 1/1 Running 0 16m
pod/nginx-deployment-d55b94fd-pfb7n 1/1 Running 0 16m
pod/nginx-deployment-d55b94fd-vtm7t 1/1 Running 0 16m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx-deployment 3 3 3 3 16m
##导出资源成yaml文件到新的文件中
[root@k8s_master ~]# kubectl get deployment/nginx-deployment --export -o yaml > nginx_abc.yaml
##验证导出的yaml文件
[root@k8s_master ~]# cat nginx_abc.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: null
generation: 1
labels:
app: nginx
name: nginx-deployment
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx-deployment
spec:
progressDeadlineSeconds: 600
replicas: 3
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:1.15.4
imagePullPolicy: IfNotPresent
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: {}
查看字段帮助信息
使用 kubectl explain命令可以查看yaml文件的字段中可以使用那些信息字段
例如:查看pod资源中spec下的containers下的ports可以使用哪些信息字段
[root@k8s_master ~]# kubectl explain pod.spec.containers.ports
KIND: Pod
VERSION: v1
RESOURCE: ports <[]Object>
DESCRIPTION:
List of ports to expose from the container. Exposing a port here gives the
system additional information about the network connections a container
uses, but is primarily informational. Not specifying a port here DOES NOT
prevent that port from being exposed. Any port which is listening on the
default "0.0.0.0" address inside a container will be accessible from the
network. Cannot be updated.
ContainerPort represents a network port in a single container.
FIELDS:
containerPort <integer> -required-
Number of port to expose on the pod's IP address. This must be a valid port
number, 0 < x < 65536.
hostIP <string>
What host IP to bind the external port to.
hostPort <integer>
Number of port to expose on the host. If specified, this must be a valid
port number, 0 < x < 65536. If HostNetwork is specified, this must match
ContainerPort. Most containers do not need this.
name <string>
If specified, this must be an IANA_SVC_NAME and unique within the pod. Each
named port in a pod must have a unique name. Name for the port that can be
referred to by services.
protocol <string>
Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP".