在线yaml与json编辑器:​​https://www.bejson.com/validators/yaml_editor/​

1.yaml格式特点

1. 大小写敏感
2. 使用缩进表示层级关系
3. 缩进时不允许使用Tal键,只允许使用空格
4. 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
5. 使用"#" 表示注释,从这个字符一直到行尾,都会被解析器忽略
6. 比json更适用于配置文件

2.yaml文件详解

kind: Deployment                          #类型,是deployment控制器,kubectl explain Deployment
apiVersion: extensions/v1beta1 #API版本,# kubectl explain Deployment.apiVersion

metadata: #pod的元数据信息,kubectl explain Deployment.metadata
labels: #自定义pod的标签,# kubectl explain Deployment.metadata.labels
app: linux36-nginx-deployment-label #标签名称为app,值为linux35-nginx,后面会用到此标签
name: linux36-nginx-deployment #pod的名称
namespace: linux36 #pod的namespace,默认是defaule

spec: #定义deployment中容器的详细信息,kubectl explain Deployment.spec
replicas: 1 #创建出的pod的副本数,即多少个pod,默认值为1
selector: #定义标签选择器
matchLabels: #定义匹配的标签,必须要设置
app: linux36-nginx-selector #匹配的目标标签

template: #定义模板,必须定义,模板是起到描述要创建的pod的作用
metadata: #定义模板元数据
labels: #定义模板label,Deployment.spec.template.metadata.labels
app: linux36-nginx-selector #定义标签,等于
spec: #定义pod信息
containers: #定义pod中容器列表,可以多个至少一个,pod不能动态增减容器
- name: linux36-nginx-container #容器名称
image: harbor.magedu.net/linux36/nginx-web1:2019-08-03_18_26_52 #镜像地址
#command: ["/apps/tomcat/bin/run_tomcat.sh"] #容器启动执行的命令或脚本
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always #拉取镜像策略
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: 2 #cpu的限制,单位为core数,将用于docker run --cpu-shares参数
memory: 2Gi #内存限制,单位可以为Mib/Gib,将用于docker run --memory参数
requests: #资源请求的设置
cpu: 500m #cpu请求数,容器启动的初始可用数量,可以写0.5或者500m等CPU压缩值
memory: 1Gi #内存请求大小,容器启动的初始可用数量,用于调度pod时候使用

volumeMounts:
- name: linux36-images
mountPath: /usr/local/nginx/html/webapp/images
readOnly: false
- name: linux36-static
mountPath: /usr/local/nginx/html/webapp/static
readOnly: false
volumes:
- name: linux36-images
nfs:
server: 192.168.7.108
path: /data/k8sdata/linux36/images
- name: linux36-static
nfs:
server: 192.168.7.108
path: /data/k8sdata/linux36/static

---
kind: Service #类型为service
apiVersion: v1 v1 #service API版本, service.apiVersion

metadata: #定义service元数据,service.metadata
labels: #自定义标签,service.metadata.labels
app: linux36-nginx-service-label #定义service标签的内容
name: linux36-nginx-service #定义service的名称,此名称会被DNS解析
namespace: linux36 #该service隶属于的namespaces名称,即把service创建到哪个namespace里面

spec: #定义service的详细信息,service.spec
type: NodePort #service的类型,定义服务的访问方式,默认为ClusterIP, service.spec.type
ports: #定义访问端口, service.spec.ports
- name: http
port: 80
protocol: TCP
targetPort: 80 #目标pod的端口
nodePort: 30002 #node节点暴露的端口
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 30443
selector: #service的标签选择器,定义要访问的目标pod
app: linux36-nginx-selector #将流量路到选择的pod上,须等于Deployment.spec.selector.matchLabels

3.创建业务namespace

cat linux36.yaml
------------------------------------
apiVersion: v1 #API版本
kind: Namespace #类型为namespac
metadata: #定义元数据
name: linux36 #namespace名称
------------------------------------

kubectl apply -f linux36.yaml
kubectl get namespaces

4.创建Nginx pod 并测试通过node port访问

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
labels:
app: linux36-nginx-deployment-label
name: linux36-nginx-deployment
namespace: linux36
spec:
replicas: 1
selector:
matchLabels:
app: linux36-nginx-selector
template:
metadata:
labels:
app: linux36-nginx-selector
spec:
containers:
- name: linux36-nginx-container
image: harbor.magedu.net/linux36/nginx-web1:2019-08-03_18_26_52
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
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: 2
memory: 2Gi
requests:
cpu: 500m
memory: 1Gi

volumeMounts:
- name: linux36-images
mountPath: /usr/local/nginx/html/webapp/images
readOnly: false
- name: linux36-static
mountPath: /usr/local/nginx/html/webapp/static
readOnly: false
volumes:
- name: linux36-images
nfs:
server: 192.168.7.108
path: /data/k8sdata/linux36/images
- name: linux36-static
nfs:
server: 192.168.7.108
path: /data/k8sdata/linux36/static
---
kind: Service
apiVersion: v1
metadata:
labels:
app: linux36-nginx-service-label
name: linux36-nginx-service
namespace: linux36
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 30002
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 30443
selector:
app: linux36-nginx-selector

4.1测试访问

root@k8s-master1:~# ss -ntl |grep 300
LISTEN 0 128 *:30002 *:*

http://192.168.47.113:30002/webapp/

yaml文件及语法基础_yaml文件及语法基础