4. Pod生命周期管理

4.1 Liveness command

通过命令行实现Liveness检查

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
# 编写Yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name: liveness
image: radial/busyboxplus
imagePullPolicy: IfNotPresent
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 60; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5

# 验证方法
kubectl exec -it liveness-exec -- ls /tmp
kubectl get pod

4.2 Liveness HTTP request

通过HTTP Request实现Liveness健康检查

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
# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: seedoflife/liveness
imagePullPolicy: IfNotPresent
args:
- /server
livenessProbe:
httpGet:
path: /healthz
port: 8080
httpHeaders:
- name: X-Custom-Header
value: Awesome
initialDelaySeconds: 3
periodSeconds: 3

# 验证方法
curl -v <Pod IP>:<Port>
kubectl get pod

4.3 ReadinessProbe + Service

通过tcpSocket实现ReadInessProbe + Service的健康检查

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
# 编写Yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: service-health
spec:
replicas: 1
selector:
matchLabels:
app: service-health
template:
metadata:
labels:
app: service-health
spec:
containers:
- name: service-health
image: python:2.7
imagePullPolicy: IfNotPresent
command: ["/bin/bash","-c","echo $(hostname) > index.html && sleep 30 && python -m SimpleHTTPServer 8080"]
ports:
- containerPort: 8080
readinessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 10
periodSeconds: 10

# 创建Service
kubectl expose deployment service-health --port=8080

# 验证方法
kubectl get service
kubectl scale deployment service-health --replicas=4
curl <ClusterIP>:<Port>