k8s实践liveness与readiness 2种探针使用

不操千曲而后晓声,观千剑而后识器。这篇文章主要讲述k8s实践liveness与readiness 2种探针使用相关的知识,希望能为你提供帮助。
0.简介本文主要介绍v1.20版本k8s使用2种探针实例。


1.liveness

存活探针,检测服务是否运行正常

1.1 exec方式
采用以下过exec方式例子,启动dp
cat liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec
labels:
app: liveness
spec:
containers:
- name: liveness
image: busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 600
livenessProbe:
initialDelaySeconds: 10
periodSeconds: 3
failureThreshold: 3
successThreshold: 1
timeoutSeconds: 1
exec:
command:
- cat
- /tmp/healthy

启动
kubectl appley -f  liveness-exec.yaml
结论:删除pod里的/tmp/healthy,可以看到检测很快,但killing状态持续30秒左右


NormalScheduled2m2sdefault-schedulerSuccessfully assigned default/liveness-exec to k8s-node01
NormalPulled116skubeletSuccessfully pulled image "busybox" in 5.486523665s
NormalCreated116skubeletCreated container liveness
NormalStarted116skubeletStarted container liveness
WarningUnhealthy32s (x3 over 38s)kubeletLiveness probe failed: cat: can\'t open \'/tmp/healthy\': No such file or directory
NormalKilling32skubeletContainer liveness failed liveness probe, will be restarted

1.2  http方式
cat liveness-http.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-http
labels:
test: liveness
spec:
containers:
- name: liveness
image: mydlqclub/springboot-helloworld:0.0.1
livenessProbe:
initialDelaySeconds: 20
periodSeconds: 3
failureThreshold: 3
timeoutSeconds: 2
httpGet:
scheme: HTTP
port: 8081
path: /actuator/health

运行后查看
[root@iz0jl52ythz64abom43swwz ~]#kubectlget pod -o wide|grep liveness-http
liveness-http1/1Running116h172.16.1.34k8s-node01< none> < none>
[root@iz0jl52ythz64abom43swwz ~]# curl 172.16.1.34^C
[root@iz0jl52ythz64abom43swwz ~]# curl 172.16.1.34:8081/actuator/health
{"status":"UP"}
[root@iz0jl52ythz64abom43swwz ~]# curl 172.16.1.34:8081/actuator/health
{"status":"UP"}
[root@iz0jl52ythz64abom43swwz ~]#

结论:直接kill容器里服务后,也是30秒后自动重建
1.3 tcp方式
cat liveness-tcp2.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-tcp
labels:
app: liveness
spec:
containers:
- name: liveness
image: cloudnativelabs/whats-my-ip
livenessProbe:
initialDelaySeconds: 15
periodSeconds: 3
failureThreshold: 3
timeoutSeconds: 1

tcpSocket:
port: 8080

结论:killall容器里服务后,也是30s左右
1.4 总结
使用exec、http、tcp方式,均在30s左右重建与恢复。
2、readiness
就绪探针,验证容器服务启动是否正常

2.1 exec方式
cat readliness-exec.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: readliness-exec
name: readliness-exec
spec:
replicas: 1
selector:
matchLabels:
run: readliness-exec
template:
metadata:
labels:
run: readliness-exec
spec:
containers:
- image: cloudnativelabs/whats-my-ip
name: readliness-exec
ports:
- containerPort: 8080
readinessProbe:
initialDelaySeconds: 60
periodSeconds: 3
failureThreshold: 3
successThreshold: 1
timeoutSeconds: 1
exec:
command:
- cat
- /tmp/healthy
terminationGracePeriodSeconds: 30

运行后可以看懂ready是0/1,此时容器虽然在running,但实际为未就绪状态。原因是我没有创建  /tmp/healthy文件,所以检测失败,符合预期
[root@iz0jl52ythz64abom43swwz ~]# kubectlget pod
NAMEREADYSTATUSRESTARTSAGE
readliness-exec-59cfc454d-dgxx80/1Running03m21s

可以看到监控检测是失败的
NormalScheduled3m24sdefault-schedulerSuccessfully assigned default/readliness-exec-59cfc454d-dgxx8 to k8s-node01
NormalPulling3m25skubeletPulling image "cloudnativelabs/whats-my-ip"
NormalPulled3m24skubeletSuccessfully pulled image "cloudnativelabs/whats-my-ip" in 851.613807ms
NormalCreated3m24skubeletCreated container readliness-exec
NormalStarted3m24skubeletStarted container readliness-exec
WarningUnhealthy82s (x21 over 2m22s)kubeletReadiness probe failed: cat: can\'t open \'/tmp/healthy\': No such file or directory

在容器里创建/tmp/healthy文件,可以看到状态已经为1/1
[root@iz0jl52ythz64abom43swwz ~]# kubectlget pod
NAMEREADYSTATUSRESTARTSAGE
readliness-exec-59cfc454d-dgxx81/1Running03m31s

在删除此文件后,又变为了0/1
WarningUnhealthy6s (x47 over 8m24s)kubeletReadiness probe failed: cat: can\'t open \'/tmp/healthy\': No such file or directory
[root@iz0jl52ythz64abom43swwz ~]# kubectlget pod
NAMEREADYSTATUSRESTARTSAGE
readliness-exec-59cfc454d-dgxx80/1Running09m28s

添加svc后观察
kubectl expose deployment readliness-exec--name=readliness-exec --port=80 --target-port=8080
?访问失败
[root@iz0jl52ythz64abom43swwz ~]# kubectl get svc
NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGE
kubernetesClusterIP10.96.0.1< none> 443/TCP12d
readliness-execClusterIP10.99.194.102< none> 80/TCP5m46s
whats-my-ipClusterIP10.103.25.20< none> 80/TCP11d
[root@iz0jl52ythz64abom43swwz ~]# curl 10.99.194.102
curl: (7) Failed connect to 10.99.194.102:80; Connection refused

在实例里添加监控检测文件后,访问正常
kubectlget pod
NAMEREADYSTATUSRESTARTSAGE
readliness-exec-59cfc454d-dgxx81/1Running036m
[root@iz0jl52ythz64abom43swwz ~]# curl 10.99.194.102
HOSTNAME:readliness-exec-59cfc454d-dgxx8 IP:172.16.1.42
[root@iz0jl52ythz64abom43swwz ~]# curl 10.99.194.102
HOSTNAME:readliness-exec-59cfc454d-dgxx8 IP:172.16.1.42

2.2 结论
【k8s实践liveness与readiness 2种探针使用】如果监听的文件不存在,那么容器READY状态会有异常,并且访问有问题。

    推荐阅读