k8s学习(7):pod检测探针

概念

image-20200130162213788

探针方式

image-20200130162511586

实例

检测探针-就绪检测

readinessProbe-httpGet

一开始pod容器中并没有index1.html,监测失败,当在容器中添加index1.html,监测成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
namespace: default
spec:
containers:
- name: readiness-httpget-container
image: hub.test.com/library/myapp:v1
imagePullPolicy: IfNotPresent
readinessProbe:
httpGet:
port: 80
path: /index1.html
initialDelaySenconds: 1
periodSenconds: 3

initialDeplaySenconds:初始化监测延时时间,上述例子为1秒之后开始监测

periodSenconds:重试监测时间,上述例子为3秒监测一次

1
2
3
4
5
6
7
8
# 执行pod创建
kubectl create -f readiness-http-pod-yaml
# 查看pod状态
kubectl get pod
# 查看pod详细状态
kubectl describe pod
# 进入pod的容器中修改
kubectl exec readiness-httpget-pod -it -- /bin/sh

监测探针-存活监测

livenessProbe-exec

一开始在容器中创建tmp文件,60s后删除,监测失败

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: livness-exec-container
image: busybox
imagePullPolicy: IfNotPresent
command: ['/bin/sh', '-c', 'touch /tmp/live; sleep 10;rm -rf /tmp/live; sleep 3600']
livenessProbe:
exec:
command: ['test', '-e', '/tmp/live']
initialDeplaySeconds: 1
periodSeconds: 3

livenessProbe-httpGet

监测index.html文件是否能够get到

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:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: hub.test.com/library/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 10

livenessProbe-tcpSocket

检测pod8080端口是否能通信

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: v1
kind: Pod
metadata:
name: liveness-tcp-pod
spec:
containers:
- name: liveness-tcp-container
image: hub.test.com/library/myapp:v1
imagePullPolicy: IfNotPresent
livenessProbe:
tcpSocket:
port: 8080
initalDelayPSeconds: 5
timeoutSeconds: 1
periodSeconds: 3

也可以将就绪检测和存活检测写到一起