参考资料
https://developer.aliyun.com/article/928015
简单说明
是一个可读性高,用来表大数据列的格式。Yaml的意思其实是:仍是一种标记语言,但是为了强调这种语言以数据为中心。而不是以标记语言为重点。
基本语法
- 缩进时不允许使用Tab键,只允许使用空格
- 缩进时空格数目不重要,只要相同层级的元素左侧对齐即可
- 使用—表示新的yaml文件开始
YAML支持的数据结构
- 对象:键值对的集合,又称为映射(mapping)、哈希(hashes)/字典(dictionary)
- 数组:一组按次序排列的值,又称为序列(sequence)/ 列表 (list)
- 纯量(scalars):单个的,不可再分的值
K8S YAML字段
必须存在的属性
主要对象
如何快速编写yaml文件
使用kubectl create命令生成yaml文件
使用kubectl get命令导出Pod的yaml文件
各种实例资源yaml文件手册
glusterfsPV.yaml
1.1 endpoint. yaml
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVersion: v1 kind: Endpoints metadata: ------------------------------------#元数据 name: glusterfs ----------------------------#ep名称 namespace: default -------------------------#命名空间 subsets: -------------------------------------#配置glusterfs连接信息 - addresses: ---------------------------------#添加glusterfs分布式地址 - ip: 10.0.0.14 - ip: 10.0.0.15 - ip: 10.0.0.16 ports: -------------------------------------#设定glusterfs服务端口 - port: 49152 protocol: TCP
|
1.2 glusterfs server. yaml
1 2 3 4 5 6 7 8 9 10 11 12
| apiVersion: v1 kind: Service metadata: name: glusterfs namespace: default spec: ports: - port: 49152 protocol: TCP targetPort: 49152 sessionAffinity: None ----------------------#是否支持session type: ClusterIP
|
PV.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| apiVersion: v1 kind: PersistentVolume metadata: -------------------------------------#元数据 name: tomcat-mysql --------------------------#pv名称 labels: -------------------------------------#标签信息 xxx: xxx spec: -----------------------------------------#定义pv模板 capacity: -----------------------------------#定义pv容量 storage: 10Gi accessModes: --------------------------------#访问模型;对象列表 - ReadWriteMany persistentVolumeReclaimPolicy: Recycle ------#pvc解除绑定后,数据操作 ========================================================================================= nfs: ---------------------------------------#nfs挂载类型 path: "/data/tomcat" ----------------------#nfs服务目录 server: 172.16.20.101 ---------------------#nfs服务地址 readOnly: false ---------------------------#关闭只读 ========================================================================================= glusterfs: -------------------------------#glusterfs挂载类型 endpoints: "glusterfs" --------------------#端点类型 请保持与glusterfs ep服务名称一致。 path: "qiangge" ---------------------------#挂载目录 glusterfs文件名称 readOnly: false ---------------------------#关闭只读
|
PVC.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| apiVersion: v1 kind: PersistentVolumeClaim metadata: -------------------------------------#元数据 name: html ----------------------------------#pvc名称 namespace: xxxx -----------------------------#命名空间 spec: -----------------------------------------#pvc模板 selector: -----------------------------------#标签选择器 matchLabels: -----------------------------#必须与pv标签信息一致才可关联 如果不指定则随机匹配pv xxx: xxx accessModes: --------------------------------#访问模型;对象列表 - ReadWriteMany resources: ----------------------------------#资源信息 requests: ---------------------------------#请求容量 storage: 99Gi storageClassName: xxxx -----------------#存储类名称 注意1.5.2版本不可用 volumeMode: Filesystem ----------------------#卷模式为文件系统 注意1.5.2版本不可用 volumeName: pvc-ff926bb2-3029-4a08-b123-31a2ad1b6a19 --#卷名称
|
Deployment.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| apiVersion: extensions/v1beta1 kind: Deployment metadata: ----------------------------------------#元数据 annotations: -------------------------------------#注释信息 deployment.kubernetes.io/revision: '1' k8s.kuboard.cn/ingress: 'false' k8s.kuboard.cn/service: NodePort k8s.kuboard.cn/workload: nextcloud labels:-------------------------------------------#标签信息 k8s.kuboard.cn/layer: '' k8s.kuboard.cn/name: nextcloud name: nextcloud-----------------------------------#名称 namespace: nextcloud------------------------------#命名空间 spec:-----------------------------------------------#定义容器模板,该模板可以包含多个容器 replicas: 3---------------------------------------#副本数量 selector:-----------------------------------------#标签选择器 matchLabels: k8s.kuboard.cn/layer: '' k8s.kuboard.cn/name: nextcloud strategy:-----------------------------------------#滚动升级策略 type: RollingUpdate-----------------------------#类型 rollingUpdate:----------------------------------#由于replicas为3,则整个升级,pod个数在2-4个之间 maxSurge: 25%---------------------------------#滚动升级时会先启动25%pod maxUnavailable: 25%---------------------------#滚动升级时允许的最大Unavailable的pod个数 template: metadata: ------------------------------------#元数据 labels:---------------------------------------#标签 k8s.kuboard.cn/layer: '' k8s.kuboard.cn/name: nextcloud spec: ------------------------------------------#定义容器模板,该模板可以包含多个容器 containers: ----------------------------------#容器信息 - name: nextcloud --------------------------#容器名称 image: '172.16.20.100/library/nextcloud:yan' imagePullPolicy: Always ------------------#镜像下载策略 ports: - name: http containerPort: 80 protocol: TCP env resources: -------------------------------#CPU内存限制 limits: --------------------------------#限制cpu内存 cpu: 200m memory: 200m requests: ------------------------------#请求cpu内存 cpu: 100m memory: 100m securityContext: -------------------------#安全设定 privileged: true -----------------------#开启享有特权 volumeMounts: ----------------------------#挂载volumes中定义的磁盘 - name: html ---------------------------#挂载容器1 mountPath: /var/www/html - name: session ------------------------#挂载容器1 mountPath: /var/lib/php/session volumes: ------------------------------------#在该pod上定义共享存储卷列表 - name: html -------------------------------#共享存储卷名称 (volumes类型有很多种) persistentVolumeClaim: -------------------#volumes类型为pvc claimName: html -----------------------#关联pvc名称 - name: session persistentVolumeClaim: claimName: session restartPolicy: Always ------------------------#Pod的重启策略 schedulerName: default-scheduler -------------#指定pod调度到节点
|
Pod.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| apiVersion: v1 kind: Pod metadata: name: django-pod labels: k8s-app: django version: v1 kubernetes.io/cluster-service: "true" annotations: - name: String spec: restartPolicy: Always nodeSelector: containers: - name: django-pod image: django:v1.1 imagePullPolicy: Never command: ['sh'] args: ["$(str)"] env: - name: str value: "/etc/run.sh" resources: requests: cpu: 0.1 memory: 32Mi limits: cpu: 0.5 memory: 32Mi ports: - containerPort: 8080 name: uwsgi protocol: TCP livenessProbe: httpGet: path: / port: 8080 scheme: HTTP initialDelaySeconds: 180 timeoutSeconds: 5 periodSeconds: 15 lifecycle: postStart: exec: command: - 'sh' - 'yum upgrade -y' preStop: exec: command: ['service httpd stop'] volumeMounts: - name: volume mountPath: /data readOnly: True volumes: - name: volume hostPath: path: /opt
|
Service.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| apiVersion: v1 kind: Service metadata: ---------------------------------#元数据 annotations: -----------------------------#注释信息 k8s.kuboard.cn/workload: nextcloud labels: ----------------------------------#标签信息 k8s.kuboard.cn/layer: '' k8s.kuboard.cn/name: nextcloud name: nextcloud --------------------------#名称 namespace: nextcloud ---------------------#命名空间 spec: --------------------------------------#定义Service模板 clusterIP: 10.0.181.206 ------------------#指定svcip地址 不指定则随机 ================================================================================================= type: NodePort ---------------------------#类型为NodePort ports: - name: mnwwwp nodePort: 30001 ----------------------#当type = NodePort时,指定映射到物理机的端口号 port: 80 -----------------------------#服务监听的端口号 protocol: TCP ------------------------#端口协议,支持TCP和UDP,默认TCP targetPort: 80 -----------------------#需要转发到后端Pod的端口号 ================================================================================================== type: ClusterIP --------------------------# ports: - name: mnwwwp port: 80 protocol: TCP targetPort: 80 - name: j5smwx port: 22 protocol: TCP targetPort: 22 selector: -------------------------------#label selector配置,将选择具有label标签的Pod作为管理 k8s.kuboard.cn/layer: '' k8s.kuboard.cn/name: nextcloud sessionAffinity: None --------------------#是否支持session
|