Pod定义清单
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
72
73
74
75
76
77
78
| apiVersion: v1 #必选,版本号,例如v1
kind: Pod #必选,资源类型,例如 Pod
metadata: #必选,元数据
name: string #必选,Pod名称
namespace: string #Pod所属的命名空间,默认为"default"
labels: #自定义标签列表
- name: string
spec: #必选,Pod中容器的详细定义
containers: #必选,Pod中容器列表
- name: string #必选,容器名称
image: string #必选,容器的镜像名称
imagePullPolicy: [ Always|Never|IfNotPresent ] #获取镜像的策略
command: [string] #容器的启动命令列表,如不指定,使用打包时使用的启动命令
args: [string] #容器的启动命令参数列表
workingDir: string #容器的工作目录
volumeMounts: #挂载到容器内部的存储卷配置
- name: string #引用pod定义的共享存储卷的名称,需用volumes[]部分定义的的卷名
mountPath: string #存储卷在容器内mount的绝对路径,应少于512字符
readOnly: boolean #是否为只读模式
ports: #需要暴露的端口库号列表
- name: string #端口的名称
containerPort: int #容器需要监听的端口号
hostPort: int #容器所在主机需要监听的端口号,默认与Container相同
protocol: string #端口协议,支持TCP和UDP,默认TCP
env: #容器运行前需设置的环境变量列表
- name: string #环境变量名称
value: string #环境变量的值
resources: #资源限制和请求的设置
limits: #资源限制的设置
cpu: string #Cpu的限制,单位为core数,将用于docker run --cpu-shares参数
memory: string #内存限制,单位可以为Mib/Gib,将用于docker run --memory参数
requests: #资源请求的设置
cpu: string #Cpu请求,容器启动的初始可用数量
memory: string #内存请求,容器启动的初始可用数量
lifecycle: #生命周期钩子
postStart: #容器启动后立即执行此钩子,如果执行失败,会根据重启策略进行重启
preStop: #容器终止前执行此钩子,无论结果如何,容器都会终止
livenessProbe: #对Pod内各容器健康检查的设置,当探测无响应几次后将自动重启该容器
exec: #对Pod容器内检查方式设置为exec方式
command: [string] #exec方式需要制定的命令或脚本
httpGet: #对Pod内个容器健康检查方法设置为HttpGet,需要制定Path、port
path: string
port: number
host: string
scheme: string
HttpHeaders:
- name: string
value: string
tcpSocket: #对Pod内个容器健康检查方式设置为tcpSocket方式
port: number
initialDelaySeconds: 0 #容器启动完成后首次探测的时间,单位为秒
timeoutSeconds: 0 #对容器健康检查探测等待响应的超时时间,单位秒,默认1秒
periodSeconds: 0 #对容器监控检查的定期探测时间设置,单位秒,默认10秒一次
successThreshold: 0
failureThreshold: 0
securityContext:
privileged: false
restartPolicy: [Always | Never | OnFailure] #Pod的重启策略
nodeName: <string> #设置NodeName表示将该Pod调度到指定到名称的node节点上
nodeSelector: obeject #设置NodeSelector表示将该Pod调度到包含这个label的node上
imagePullSecrets: #Pull镜像时使用的secret名称,以key:secretkey格式指定
- name: string
hostNetwork: false #是否使用主机网络模式,默认为false,如果设置为true,表示使用宿主机网络
volumes: #在该pod上定义共享存储卷列表
- name: string #共享存储卷名称 (volumes类型有很多种)
emptyDir: {} #类型为emtyDir的存储卷,与Pod同生命周期的一个临时目录。为空值
hostPath: string #类型为hostPath的存储卷,表示挂载Pod所在宿主机的目录
path: string #Pod所在宿主机的目录,将被用于同期中mount的目录
secret: #类型为secret的存储卷,挂载集群与定义的secret对象到容器内部
scretname: string
items:
- key: string
path: string
configMap: #类型为configMap的存储卷,挂载预定义的configMap对象到容器内部
name: string
items:
- key: string
path: string
|
容器探测
liveness probes:存活性探测,不在正常状态会重启
readliness probes:就绪性探测,不能接受请求不转发流量
httpGet方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| pod-liveness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-liveness-httpget
namespace: dev
labels:
user: kaikai
spec:
containers: #容器配置
- image: nginx:1.17.1
imagePullPolicy: IfNotPresent
name: nginx
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
livenessProbe: #存活性探针
httpGet: #http://127.0.0.1:80/
port: 80
scheme: HTTP
path: /
|
tcpSocker方式
1
2
3
4
| #访问容器端口,能够建立连接则正常
livenessProbe:
tcpSocker:
port: 8080
|
exec方式
1
2
3
4
5
6
| #exec在容器内执行一次命令,执行成功,认为程序正常
livenessProbe:
exec:
command:
- cat
- /data/aaa
|
探测补充
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
| [root@101 ~]# kubectl explain pod.spec.containers.livenessProbe
FIELDS:
exec <Object>
One and only one of the following should be specified. Exec specifies the
action to take.
failureThreshold <integer> ####连续探测多少次认定失败,默认三次,最少一次
Minimum consecutive failures for the probe to be considered failed after
having succeeded. Defaults to 3. Minimum value is 1.
httpGet <Object>
HTTPGet specifies the http request to perform.
initialDelaySeconds <integer> ####容器启动后等待多少秒执行第一次探测
Number of seconds after the container has started before liveness probes
are initiated. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
periodSeconds <integer> #####探测频率 默认十秒,最小值1秒
How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
value is 1.
successThreshold <integer> ####连续探测成功多少次被认定成功,默认一次
Minimum consecutive successes for the probe to be considered successful
after having failed. Defaults to 1. Must be 1 for liveness and startup.
Minimum value is 1.
tcpSocket <Object>
TCPSocket specifies an action involving a TCP port. TCP hooks not yet
supported
timeoutSeconds <integer> ###探测超时时间默认一秒最小一秒
Number of seconds after which the probe times out. Defaults to 1 second.
Minimum value is 1. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
|
重启策略
pod重启策略有三种
Always:自动重启,默认
Onfailure:容器终止运行且退出码不为0重启
Never:不重启容器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| pod-restartpolicy.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-restart-policy
namespace: dev
labels:
user: kaikai
spec:
containers: #容器配置
- image: nginx:1.17.1
imagePullPolicy: IfNotPresent
name: nginx
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
livenessProbe: #存活性探针
httpGet: #http://127.0.0.1:80/
port: 8080
scheme: HTTP
path: /
restartPolicy: Never #重启策略never 从不重启
|
Pod调度
pod调度由Scheduler组件采用相应算法计算出来,四大类调度方式
自动调度:
定向调度:NodeName,NodeSelector
亲和性调度:NodeAffinity,PodAffinity,PodAntiAffinity
污点(容忍)调度:Taints,Toleration
定向调度
利用在pod上声名的nodeName或nodeselector,将pod调度搭到期望的node节点,强制性的,node不存在也会调度,只不过调度不成功
nodeName
nodeName将pod调度到指定name节点,跳过Scheduler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| apiVersion: v1
kind: Pod
metadata:
name: pod-liveness-httpget
namespace: dev
labels:
user: kaikai
spec:
containers: #容器配置
- image: nginx:1.17.1
imagePullPolicy: IfNotPresent
name: nginx
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
livenessProbe: #存活性探针
httpGet: #http://127.0.0.1:80/
port: 80
scheme: HTTP
path: /
nodeName: "102" #指定node名字
|
podName
nodeSelector将Pod调度到添加了指定标签的Node节点上,通过kubernetes的label-selector机制,有Scheduler使用MatchNodeSelector调度策略进行label匹配,强制性
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
| pod-nodeselector.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-nodeselector
namespace: dev
labels:
user: kaikai
spec:
containers: #容器配置
- image: nginx:1.17.1
imagePullPolicy: IfNotPresent
name: nginx
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
livenessProbe: #存活性探针
httpGet: #http://127.0.0.1:80/
port: 80
scheme: HTTP
path: /
nodeSelector: #####通过标签选择node节点
nodeenv: pre
|
亲和性调度
nodeAffinity(node亲和性调度)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| pod.spec.affinity.nodeAffinity
requiredDuringSchedulingIgnoredDuringExecution ###Node节点必须满足指定的所有规则才可以,相当于硬限制
nodeSelectorTerms 节点选择列表
matchFields 按节点字段列出的节点选择器要求列表
matchExpressions 按节点标签列出的节点选择器要求列表(推荐)
key 键
values 值
operator 关系符 支持Exists, DoesNotExist, In, NotIn, Gt, Lt
preferredDuringSchedulingIgnoredDuringExecution 优先调度到满足指定的规则的Node,相当于软限制 (倾向)
preference 一个节点选择器项,与相应的权重相关联
matchFields 按节点字段列出的节点选择器要求列表
matchExpressions 按节点标签列出的节点选择器要求列表(推荐)
key 键
values 值
operator 关系符 支持In, NotIn, Exists, DoesNotExist, Gt, Lt
weight 倾向权重,在范围1-100。
|
podAffinity(node亲和性调度)
podAntiAffinity
污点调度