k8s学习笔记-03(Pod yaml文件编写)

打印 上一主题 下一主题

主题 495|帖子 495|积分 1485

原创文档编写不易,未经许可请勿转载。文档中有疑问的可以邮件联系我。 邮箱:yinwanit@163.com
描述

Pod在k8s中归属apiVersion版本为v1。在编写yaml文件中apiVersion应该设置为v1。kind才能设置成Pod。
编写Pod的yaml文件时可以参考 kubectl explain --api-version=v1  pod. 一级一级查看具体的配置项。
apiVersion和kind对应关系参考:https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/workload-resources/。
在通过yaml文件创建Pod时最好通过kubectl run 命令导出一个yaml文件。
本文对于Pod常用yaml配置进行演示,主要有勾子进程、健康性检查、初始化容器、Pod节点选择、node污点及容忍性。
模板文件导出命令: kubectl run test_name_pod --image=nginx  --image-pull-policy=IfNotPresent --restart=Always --namespace=kube-system --labels=aa=12  --dry-run=client -o yaml > temp_pod.yaml
一、Pod生命周期勾子

勾子程序用在在pod开启关闭时通知应用级联操作,用在容器开启关闭前对容器进行自定义的命令或http端口拉取访问。
勾子程序有两个hook,一个为PostStart、PreStop,如果PreStop或者PostStart失败的话, 容器会停止。

  • PostStart:在容器运行前执行操作,会阻塞Pod进入run的状态,如果PostStart hook未执行成功,Pod将无法进行running状态。
  • PreStop:应用在容器停止前,会阻塞容器删除,但是过了terminationGracePeriodSeconds时间后,容器会被强制删除。
配置参考:kubectl explain  pod.spec.containers.lifecycle
1.1 exec实例文件

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test1-pod
  7.   name: test1-pod
  8. spec:
  9.   containers:
  10.   - image: nginx
  11.     imagePullPolicy: IfNotPresent
  12.     name: test1-pod
  13.     resources: {}
  14.     lifecycle:
  15.       postStart:
  16.         exec:
  17.           command: ["/bin/bash", "-c", "touch /root/22"]
  18.       preStop:
  19.         exec:
  20.           command:
  21.           - cat
  22.           - /root/22
  23.   dnsPolicy: ClusterFirst
  24.   restartPolicy: Always
  25. status: {}
复制代码
View Code1.2 httpGet实例文件

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test2-pod
  7.   name: test2-pod
  8. spec:
  9.   containers:
  10.   - image: nginx
  11.     imagePullPolicy: IfNotPresent
  12.     name: test2-pod
  13.     resources: {}
  14.     lifecycle:
  15.       postStart:
  16.         httpGet:
  17.           host: 10.244.190.70
  18.           path: /
  19.           port: 80
  20.           scheme: HTTP
  21.   dnsPolicy: ClusterFirst
  22.   restartPolicy: Always
  23. status: {}
复制代码
View Code部分参数详解:

  • host: 需要请求的主机地址
  • path: web网页的路径
  • port:对端web服务的端口
  • scheme:连接主机的协议,默认HTTP
二、健康性检查

每个容器都会执行一个进程此进程由CMD和ENTRYPOINT指定,如果进程退出时返回码非零,则表示该容器发生故障,k8s会根据yaml文件中定义的restrtPolicy规则决定是否重启容器。
有时进程未退出则不会反会值,但是程序处于异常状态,此时需要额外的检测手段对容器进行检查,使用Liveness探测来确定容器中运行的程序能够提供正常的服务。
在容器启动完成过后但是容器中的应用不一定启动完成,对于对外服务的容器,需要使用Readiness决定容器什么时候可以加入到service中。
LIveness配置参考:kubectl explain  pod.spec.containers.livenessProbe
Readiness配置参考:kubectl explain  pod.spec.containers.readinessProbe
2.1 Liveness探测

Liveness探测让用户可以自定义判断容器是否健康,如果探测失败则重启容器。
2.1.1 exec实例文件

自定参数进行检测。
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test3-pod
  7.   name: test3-pod
  8. spec:
  9.   containers:
  10.   - image: nginx
  11.     imagePullPolicy: IfNotPresent
  12.     name: test3-pod
  13.     resources: {}
  14.     lifecycle:
  15.       postStart:
  16.         exec:
  17.           command:
  18.           - sleep
  19.           - "20"
  20.     livenessProbe:
  21.       exec:
  22.         command:
  23.         - cat
  24.         - /root/22
  25.       periodSeconds: 11
  26.       initialDelaySeconds: 10
  27.       failureThreshold: 5
  28.       successThreshold: 1
  29.       timeoutSeconds: 3
  30.   dnsPolicy: ClusterFirst
  31.   restartPolicy: Always
  32. status: {}
复制代码
View Code部分参数详解:

  • periodSeconds: 每隔多少秒检测一次,默认10秒,最小值1秒。
  • initialDelaySeconds: 容器启动过后多少秒开始检测,默认0秒。
  • failureThreshold: 检测失败多少次确定程序异常,默认3次,最小1次。
  • successThreshold: 检测成功多少次确定程序正常,必须为1,默认为1。
  • timeoutSeconds: 检测超时时间默认1秒。
2.1.2 httpGet实例文件

用作http网页服务检测
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test4-pod
  7.   name: test4-pod
  8. spec:
  9.   containers:
  10.   - image: nginx
  11.     imagePullPolicy: IfNotPresent
  12.     name: test4-pod
  13.     resources: {}
  14.     lifecycle:
  15.       postStart:
  16.         exec:
  17.           command:
  18.           - sleep
  19.           - "20"
  20.     livenessProbe:
  21.       httpGet:
  22.         host: 10.244.190.70
  23.         path: /
  24.         port: 80
  25.         scheme: HTTP
  26.       periodSeconds: 11
  27.       initialDelaySeconds: 10
  28.       failureThreshold: 5
  29.       successThreshold: 1
  30.       timeoutSeconds: 3
  31.   dnsPolicy: ClusterFirst
  32.   restartPolicy: Always
  33. status: {}
复制代码
View Code部分参数解释:

  • host: 需要请求的主机地址
  • path: web网页的路径
  • port:对端web服务的端口
  • scheme:连接主机的协议,默认HTTP
2.1.3  tcpSocket实例文件

用作tcp端口检测。
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test5-pod
  7.   name: test5-pod
  8. spec:
  9.   containers:
  10.   - image: nginx
  11.     imagePullPolicy: IfNotPresent
  12.     name: test5-pod
  13.     resources: {}
  14.     lifecycle:
  15.       postStart:
  16.         exec:
  17.           command:
  18.           - sleep
  19.           - "20"
  20.     livenessProbe:
  21.       tcpSocket:
  22.         host: 10.244.190.70
  23.         port: 80
  24.       periodSeconds: 11
  25.       initialDelaySeconds: 10
  26.       failureThreshold: 5
  27.       successThreshold: 1
  28.       timeoutSeconds: 3
  29.   dnsPolicy: ClusterFirst
  30.   restartPolicy: Always
  31. status: {}
复制代码
View Code部分参数解释:

  • host: 需要请求的主机地址
  • port:对端监听的TCP端口号
2.2 Readiness探测

Readiness可以检测容器什么时候满足对外提供服务的能力,即什么时候可以加入到service负载池中。Readiness探测语法和Liveness语法完全一致。 
2.2.1 exec实例文件

自定义参数检查。
  1. cat pod-readinessProve.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   creationTimestamp: null
  6.   labels:
  7.     run: test8-pod
  8.   name: test8-pod
  9. spec:
  10.   containers:
  11.   - image: nginx
  12.     imagePullPolicy: IfNotPresent
  13.     name: test8-pod
  14.     resources: {}
  15.     lifecycle:
  16.       postStart:
  17.         exec:
  18.           command:
  19.           - sleep
  20.           - "20"
  21.     readinessProbe:
  22.       exec:
  23.         command:
  24.         - cat
  25.         - /root/22
  26.       periodSeconds: 11
  27.       initialDelaySeconds: 10
  28.       failureThreshold: 5
  29.       successThreshold: 1
  30.       timeoutSeconds: 3
  31.   dnsPolicy: ClusterFirst
  32.   restartPolicy: Always
  33. status: {}
复制代码
View Code部分参数详解:

  • periodSeconds: 每隔多少秒检测一次,默认10秒,最小值1秒。
  • initialDelaySeconds: 容器启动过后多少秒开始检测,默认0秒。
  • failureThreshold: 检测失败多少次确定程序异常,默认3次,最小1次。
  • successThreshold: 检测成功多少次确定程序正常,必须为1,默认为1。
  • timeoutSeconds: 检测超时时间默认1秒。
2.2.2 httpGet实例文件

用作http网页服务检测。
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test6-pod
  7.   name: test6-pod
  8. spec:
  9.   containers:
  10.   - image: nginx
  11.     imagePullPolicy: IfNotPresent
  12.     name: test6-pod
  13.     resources: {}
  14.     lifecycle:
  15.       postStart:
  16.         exec:
  17.           command:
  18.           - sleep
  19.           - "20"
  20.     readinessProbe:
  21.       httpGet:
  22.         host: 10.244.190.70
  23.         path: /
  24.         port: 80
  25.         scheme: HTTP
  26.       periodSeconds: 11
  27.       initialDelaySeconds: 10
  28.       failureThreshold: 5
  29.       successThreshold: 1
  30.       timeoutSeconds: 3
  31.   dnsPolicy: ClusterFirst
  32.   restartPolicy: Always
  33. status: {}
复制代码
View Code部分参数解释:

  • host: 需要请求的主机地址
  • path: web网页的路径
  • port:对端web服务的端口
  • scheme:连接主机的协议,默认HTTP
2.2.3 tcpSocket实例文件

用作tcp端口检测。
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test7-pod
  7.   name: test7-pod
  8. spec:
  9.   containers:
  10.   - image: nginx
  11.     imagePullPolicy: IfNotPresent
  12.     name: test7-pod
  13.     resources: {}
  14.     lifecycle:
  15.       postStart:
  16.         exec:
  17.           command:
  18.           - sleep
  19.           - "20"
  20.     readinessProbe:
  21.       tcpSocket:
  22.         host: 10.244.190.70
  23.         port: 80
  24.       periodSeconds: 11
  25.       initialDelaySeconds: 10
  26.       failureThreshold: 5
  27.       successThreshold: 1
  28.       timeoutSeconds: 3
  29.   dnsPolicy: ClusterFirst
  30.   restartPolicy: Always
  31. status: {}
复制代码
View Code部分参数解释:

  • host: 需要请求的主机地址
  • port:对端监听的TCP端口号
二、初始化容器

在k8s中对于一些容器,在运行时必须满足一定的条件,但是又不能直接在容器中执行命令使环境到达满足容器运行的要求,因为在容器中执行命令可能会导致镜像里面的CMD和ENTRYPOINT指定命令发生改变从而导致容器运行不起来。
为了当前环境能够满足正式容器运行的条件,创建一个初始化容器对环境进行一个设置,待初始化容器对环境设置完成过后再运行主程序容器。
如果为一个 Pod 指定了多个 Init 容器,Init容器会按顺序一次运行一个。 每个 Init 容器必须运行成功才能够运行一个Init容器,所有Init容器运行完成才能运行Pod中的主程序容器。
Pod中所有容器的名称必须唯一包含Init容器和主程序容器,如果同一个Pod中有容器名称不唯一则会报错。
初始化容器一般用来对宿主机节点的配置进行修改。
配置参考:kubectl explain  pod.spec.initContainers
 2.1 使用Init预处理数据卷

创建两个卷datadir、datadir2。datadir使用指定宿主机中/data_pod为路径,datadir2随机在宿主机上生成一个文件夹作为存储数据路径。在初始化容器中挂在datadir卷,并在卷中创建一个文件,最后在主程序容器中挂在datadir卷。
emptyDir对于容器来说是持久卷,对于Pod来说不是持久卷,该方式创建的卷会跟随Pod删除而删除。
hostPath:对于Pod来说是持久卷,不会随着Pod删除而删除。
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test9-pod
  7.   name: test9-pod
  8. spec:
  9.   volumes:
  10.   - name: datadir
  11.     hostPath:
  12.       path: /data_pod
  13.   - name: datadir2
  14.     emptyDir: {}
  15.   initContainers:
  16.   - name: init-container-01
  17.     image: busybox
  18.     command:
  19.     - touch
  20.     - /pod_data/lvan.sh
  21.     volumeMounts:
  22.     - name: datadir
  23.       mountPath: "/pod_data"
  24.   containers:
  25.   - image: nginx
  26.     imagePullPolicy: IfNotPresent
  27.     env:
  28.     - name: aa
  29.       value: "456"
  30.     - name: bb
  31.       value: "test_env"
  32.     name: test9-pod
  33.     volumeMounts:
  34.     - mountPath: "/data01"
  35.       name: datadir
  36.     - mountPath: "/data/02"
  37.       name: datadir2
  38.   dnsPolicy: ClusterFirst
  39.   restartPolicy: Always
  40. status: {}
复制代码
View Code2.2 使用Init修改宿主机配置

使用初始化容器对宿主机进行配置更改时首先需要共享需要更改的宿主机的文件夹到初始化容器中,对于宿主机内核相关信息更改,还需在创建容器时使用特权模式创建。
设置了特权模式的容器,可以对主机进行很多操作,比如,要修改宿主机内核参数等。
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test10-pod
  7.   name: test10-pod
  8. spec:
  9.   initContainers:
  10.   - name: init-container-01
  11.     image: busybox
  12.     command:
  13.     - touch
  14.     - /root/lvan.sh
  15.     securityContext:
  16.       privileged: true
  17.   containers:
  18.   - image: nginx
  19.     imagePullPolicy: IfNotPresent
  20.     env:
  21.     - name: aa
  22.       value: "456"
  23.     - name: bb
  24.       value: "test_env"
  25.     name: test10-pod
  26.   dnsPolicy: ClusterFirst
  27.   restartPolicy: Always
  28. status: {}
复制代码
View Code部分参数详解:

  • privileged: 设置容器为特权模式,true
三、pod调度

 创建Pod时可以指定Pod运行的worknode节点,直接指定运行的worknode将不受worknode上的污点影响。
3.1 nodeName

直接指定Pod运行在哪个宿主机上。
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test11-pod
  7.   name: test11-pod
  8. spec:
  9.   nodeName: node01.lvan
  10.   containers:
  11.   - image: nginx
  12.     imagePullPolicy: IfNotPresent
  13.     env:
  14.     - name: aa
  15.       value: "456"
  16.     - name: bb
  17.       value: "test_env"
  18.     name: test11-pod
  19.   dnsPolicy: ClusterFirst
  20.   restartPolicy: Always
  21. status: {}
复制代码
View Code3.2 nodeSelector

nodeSelector通过标签来确定Pod运行在哪个宿主机中。
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test12-pod
  7.   name: test12-pod
  8. spec:
  9.   nodeSelector:
  10.     kubernetes.io/hostname: node02.lvan
  11.   containers:
  12.   - image: nginx
  13.     imagePullPolicy: IfNotPresent
  14.     env:
  15.     - name: aa
  16.       value: "456"
  17.     - name: bb
  18.       value: "test_env"
  19.     name: test12-pod
  20.   dnsPolicy: ClusterFirst
  21.   restartPolicy: Always
  22. status: {}
复制代码
View Code四、node污点设置

没有污点的worknode可以支持可容忍污点的Pod。如果worknode上污点,那么表示不允许pod调度到该节点,除非pod也被标记为可以容忍污点节点。
可以使用污点来区分不同节点的类型,如环境中worknode节点物理配置不一样,有的磁盘比较好、有的网络比较好。可以添加污点却分出每种不同类型的nodework。
使用kubectl taint命令可完成Node节点污点设置。
设置了污点的Node,会根据设置的策略对Pod进行调度,可以让新的Pod不调度到该节点,也可以把现有节点的上Pod驱逐出该Node。
污点由标签和作用两部分组成,格式为: key=value:effect,其中key=value即为污点标签,部分,effect为该污点的具体作用。污点有三个effect动作。
effect策略解释:

  • PreferNoSchedule:NoSchedule软策略,尽量不要调度到该节点上,如果环境中除了该节点外其他节点均不满足Pod运行,则会调度到该策略的workNode上。
  • NoSchedule:设置Pod不调度到该workNode上,Pod设置了可以容忍该污点除外。
  • NoExecute:设置workNode不能调度Pod,同时驱逐掉该workNode上已有的Pod。
4.1 节点污点查看

 使用命令查看node下有哪些污点。
  1. # kubectl describe  node node_name  | grep -iA1 Taints
复制代码
如果结果为表示没有设置污点。
4.2 节点污点设置

格式:key=value:effect
4.2.1 设置污点
  1. # kubectl taint nodes node_name  标签名=标签值:NoSchedule
  2. # #设置节点node01.lvan污点标签为storage=low-storage,尽量不调度Pod到该节点。
  3. # kubectl taint nodes node01.lvan storage=low-storage:PreferNoSchedule
复制代码
4.2.2 去除污点
  1. # kubectl  taint  node  node_name  标签名=标签值:PreferNoSchedule-<br># kubectl  taint  node  node_name  标签名=标签值-<br># kubectl  taint  node  node_name  标签名-
  2. # #去除node01.lvan节点上storage:PreferNoSchedule污点
  3. # kubectl  taint  node  node01.lvan  storage:PreferNoSchedule-<br><br>
复制代码
五、Pod容忍性

当节点设置了污点,同时污点的策略还是NoSchedule,Pod则不会调度到该节点上。如果需要在该workNode上运行Pod,则需要设置Pod能够对该节点的上的污点进行容忍。
yaml文件中污点设置参考:kubectl explain --api-version=v1 pod.spec.tolerations. 
operator参数解释:

  • Exists:节点上存在该标签的污点就行,不用在乎具体的值和动作。都可以容忍。
  • Equal:节点上存在的污点标签值要和yaml文件中定义一致,动作可以省略才可以容忍。
5.1 Exists

节点上只要有yaml文件中对应标签名的污点即表示该Pod可容忍对应的污点。
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test12-pod
  7.   name: test12-pod
  8. spec:
  9.   tolerations:
  10.   - key: storage
  11.     operator: Exists
  12.   containers:
  13.   - image: nginx
  14.     imagePullPolicy: IfNotPresent
  15.     env:
  16.     - name: aa
  17.       value: "456"
  18.     - name: bb
  19.       value: "test_env"
  20.     name: test12-pod
  21.   dnsPolicy: ClusterFirst
  22.   restartPolicy: Always
  23. status: {}
复制代码
View Code5.2 Equal

需要yaml中定义的容忍污点标签、值、动作和节点上的污点一致才可容忍节点上的污点。
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     run: test12-pod
  7.   name: test12-pod
  8. spec:
  9.   tolerations:
  10.   - key: storage
  11.     operator: Equal
  12.     value: low-storage
  13.     effect: NoSchedule
  14.   containers:
  15.   - image: nginx
  16.     imagePullPolicy: IfNotPresent
  17.     env:
  18.     - name: aa
  19.       value: "456"
  20.     - name: bb
  21.       value: "test_env"
  22.     name: test12-pod
  23.   dnsPolicy: ClusterFirst
  24.   restartPolicy: Always
  25. status: {}
复制代码
View Code六、亲和性

k8s中亲和性分为节点亲和性和Pod亲和性两种。

  • 节点亲和性:设置Pod和node的关系。
  • Pod亲和性:设置Pod和Pod之间的关系。
节点亲和性和Pod亲和性有软亲和性和硬亲和性两种。

  • 软亲和性:preferred打头的为软亲和性
  • 硬亲和性:required打头的为硬亲和性。
 配置参考kubectl explain  pod.spec.affinity.
6.1 键值运算关系

持续更新2023年8月7日。
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

梦见你的名字

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表