k3s-配置文件总结

文章目录
  1. 1. 参考资料
  2. 2. 简单说明
  3. 3. 基本语法
  4. 4. YAML支持的数据结构
  5. 5. K8S YAML字段
    1. 5.1. 必须存在的属性
    2. 5.2. 主要对象
  6. 6. 如何快速编写yaml文件
    1. 6.1. 使用kubectl create命令生成yaml文件
    2. 6.2. 使用kubectl get命令导出Pod的yaml文件
  7. 7. 各种实例资源yaml文件手册
    1. 7.1. glusterfsPV.yaml
    2. 7.2. PV.yaml
    3. 7.3. PVC.yaml
    4. 7.4. Deployment.yaml
    5. 7.5. Pod.yaml
    6. 7.6. Service.yaml

参考资料

https://developer.aliyun.com/article/928015

简单说明

是一个可读性高,用来表大数据列的格式。Yaml的意思其实是:仍是一种标记语言,但是为了强调这种语言以数据为中心。而不是以标记语言为重点。

基本语法

  • 缩进时不允许使用Tab键,只允许使用空格
  • 缩进时空格数目不重要,只要相同层级的元素左侧对齐即可
  • 使用—表示新的yaml文件开始

YAML支持的数据结构

  • 对象:键值对的集合,又称为映射(mapping)、哈希(hashes)/字典(dictionary)
  • 数组:一组按次序排列的值,又称为序列(sequence)/ 列表 (list)
  • 纯量(scalars):单个的,不可再分的值

K8S YAML字段

必须存在的属性

20230515113029

主要对象

20230515113121

如何快速编写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: --------------------------------#访问模型;对象列表
#ReadWriteOnce一人读写
#ReadOnlyMany 多人只读
#ReadWriteMany多人读写
- ReadWriteMany
persistentVolumeReclaimPolicy: Recycle ------#pvc解除绑定后,数据操作
#默认是Retain保留生成的数据、
#recycle回收
#delete,删除
=========================================================================================
#nfs: 类型
nfs: ---------------------------------------#nfs挂载类型
path: "/data/tomcat" ----------------------#nfs服务目录
server: 172.16.20.101 ---------------------#nfs服务地址
readOnly: false ---------------------------#关闭只读
=========================================================================================
#glusterfs:分布式类型 注意:使用glusterfs 需要提交部署endpoint服务
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: --------------------------------#访问模型;对象列表
#ReadWriteOnce一人读写
#ReadOnlyMany 多人只读
#ReadWriteMany多人读写
- 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的重启策略
#Always表示一旦不管以何种方式终止运行,
#kubelet都将重启,
#OnFailure表示只有Pod以非0退出码退出才重启,
#Nerver表示不再重启该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             #指定api版本,此值必须在kubectl apiversion中  
kind: Pod #指定创建资源的角色/类型
metadata: #资源的元数据/属性
name: django-pod #资源的名字,在同一个namespace中必须唯一
labels: #设定资源的标签,使这个标签在service网络中备案,以便被获知
k8s-app: django
version: v1
kubernetes.io/cluster-service: "true"
annotations: #设置自定义注解列表
- name: String #设置自定义注解名字
spec: #设置该资源的内容
restartPolicy: Always #表示自动重启,一直都会有这个容器运行
nodeSelector: #选择node节点14 zone: node1
containers:
- name: django-pod #容器的名字
image: django:v1.1 #容器使用的镜像地址
imagePullPolicy: Never #三个选择Always、Never、IfNotPresent,每次启动时检查和更新(从registery)images的策略,
# Always,每次都检查
# Never,每次都不检查(不管本地是否有)
# IfNotPresent,如果本地有就不检查,如果没有就拉取
command: ['sh'] #启动容器的运行命令,将覆盖容器中的Entrypoint,对应Dockefile中的ENTRYPOINT
args: ["$(str)"] #启动容器的命令参数,对应Dockerfile中CMD参数
env: #指定容器中的环境变量
- name: str #变量的名字
value: "/etc/run.sh" #变量的值
resources: #资源管理
requests: #容器运行时,最低资源需求,也就是说最少需要多少资源容器才能正常运行
cpu: 0.1 #CPU资源(核数),两种方式,浮点数或者是整数+m,0.1=100m,最少值为0.001核(1m)
memory: 32Mi #内存使用量
limits: #资源限制
cpu: 0.5
memory: 32Mi
ports:
- containerPort: 8080 #容器开发对外的端口
name: uwsgi #名称
protocol: TCP
livenessProbe: #pod内容器健康检查的设置
httpGet: #通过httpget检查健康,返回200-399之间,则认为容器正常
path: / #URI地址
port: 8080
#host: 127.0.0.1 #主机地址
scheme: HTTP
initialDelaySeconds: 180 #表明第一次检测在容器启动后多长时间后开始
timeoutSeconds: 5 #检测的超时时间
periodSeconds: 15 #检查间隔时间
#也可以用这种方法
#exec: 执行命令的方法进行监测,如果其退出码不为0,则认为容器正常
# command:
# - cat
# - /tmp/health
#也可以用这种方法
#tcpSocket: //通过tcpSocket检查健康
# port: number
lifecycle: #生命周期管理(钩子)
postStart: #容器运行之前运行的任务
exec:
command:
- 'sh'
- 'yum upgrade -y'
preStop: #容器关闭之前运行的任务
exec:
command: ['service httpd stop']
volumeMounts: #挂载设置
- name: volume #挂载设备的名字,与volumes[*].name 需要对应
mountPath: /data #挂载到容器的某个路径下
readOnly: True
volumes: #定义一组挂载设备
- name: volume #定义一个挂载设备的名字
#meptyDir: {}
hostPath:
path: /opt #挂载设备类型为hostPath,路径为宿主机下的/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地址 不指定则随机

=================================================================================================
#NodePort类型:集群外网络
type: NodePort ---------------------------#类型为NodePort
ports:
- name: mnwwwp
nodePort: 30001 ----------------------#当type = NodePort时,指定映射到物理机的端口号
port: 80 -----------------------------#服务监听的端口号
protocol: TCP ------------------------#端口协议,支持TCP和UDP,默认TCP
targetPort: 80 -----------------------#需要转发到后端Pod的端口号

==================================================================================================
#ClusterIP类型: 集群内网络
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