1. 简介

deployment的作用来维持pod的健壮性,当pod挂掉之后,deployment会生成新的pod,但如果pod是正常运行的,但pod里面出了问题,此时deployment是监测不到的。故此时需要探测(probe),用户定义出现什么样的状况才叫出问题,当probe监测到此问题,会认为pod出现了问题,执行重启大法来解决问题。

2. liveness prob

存活性探针,遇到问题会通过重启大法来解决问题,所谓重启就是删除pod里的容器,重新创建pod里的容器

2.1 支持的探测方式

1.command
2.httpGet
3.tcpSocket

2.2 command探测方式

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:
labels:
test: liveness
name: pod1
spec:
containers:
- name: liveness
image: busybox
imagePullPolicy: IfNotPresent
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 10
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5 #容器启动的5s内不监测
periodSeconds: 5 #每5s钟检测一次

2.3 httpGet探测方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: pod2
spec:
containers:
- name: liveness
image: nginx
livenessProbe:
failureThreshold: 3
httpGet:
path: /index.html
port: 80
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10

参数意义
initialDelaySeconds:容器启动后第一次执行他测是需要等待多少秒
periodSeconds:执行探测的频率,默认是10秒,最小1秒
timeoutSeconds:探测超时时间,默认1秒,最小1秒
seccessThreshold:探测失败后,最少连续探测成功多少此才被认定为成功,默认1,对于liveless来说必须是1
failureThreshold:当pod启动了并且探测到失败,kubernetes的重试次数,存活探测情况下的放弃就意味着重新启动容器,就绪探测情况下的放弃Pod会被打上未就绪的标签。默认值3,最小值是1

2.4 tcpsocket探测方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind: Pod
metadata:
labels:
test: pod3
name: liveness-exec
spec:
containers:
- name: liveness
image: nginx
livenessProbe:
failureThreshold: 3
tcpSocket:
port: 80
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1

3. readiness probe

就绪性探针,遇到问题不重启pod,svc不再把请求转发到此pod

3.1 支持的探测方式

1.command
2.httpGet
3.tcpSocket

3.2 command探测

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: v1
kind: Pod
metadata:
labels:
test: readness
name: readness-http
spec:
containers:
- name: readness
image: busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy;sleep 10
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5

3.3 httpGet探测方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-httpget
spec:
containers:
- name: liveness
image: nginx
readinessProbe:
failureThreshold: 3
httpGet:
path: /index.html
port: 80
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10

3.4 tcpsocket探测方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-tcp
spec:
containers:
- name: liveness
image: nginx
readinessProbe:
failureThreshold: 3
tcpSocket:
port: 80
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10