啟動(Startup Probe)、存活(Liveness Probe)和就緒探針(Readiness Probe)有其不同的用途和優(yōu)先級。
優(yōu)先級和用途
啟動探針(Startup Probe)用于Pod內(nèi)程序告訴kubernetes,其準備工作已經(jīng)做好。這些準備工作主要是指業(yè)務(wù)運行前的前置條件,比如資源文件下載完畢,內(nèi)置數(shù)據(jù)庫文件下載完畢等。這步完成后存活和就緒探針才會開始工作。
存活和就緒探針之間沒有關(guān)系,所以它們沒有優(yōu)先級區(qū)別,即在啟動探針確定Success后,它們兩個同時開始檢測。有任何一個失敗就會執(zhí)行其對應(yīng)的失敗處理動作。
存活探針用于表示程序是否活著。如果被認定不存活,會依據(jù)設(shè)置要么重啟容器或讓Pod調(diào)度失敗。
就緒探針表示程序是否可以提供服務(wù)。一般Pod內(nèi)程序是通過Service對外提供服務(wù),如果就緒探針失敗,Service會將該Pod摘除,這樣流量就不會打到這個不能工作的Pod上;如果就緒探針成功了,該Pod又會被加進Service。
似乎有存活和就緒探針就夠了,為什么還要啟動探針呢?因為一些準備工作我們并不知道其需要花多長時間,比如可能網(wǎng)絡(luò)帶寬問題導(dǎo)致資源文件下載很慢。這個時候設(shè)置存活或者就緒探針就可能不準確,或者導(dǎo)致其不靈敏。所以設(shè)置啟動探針可以提升其他探針的靈敏度。
啟動和存活探針
# startup_liveness.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: startup-liveness-deployment
spec:
selector:
matchLabels:
app: startup-liveness
template:
metadata:
labels:
app: startup-liveness
spec:
containers:
- name: startup-liveness-container
image: busybox
command: ["/bin/sh", "-c", "sleep 6; touch /tempdir/ready; sleep 3;touch /tempdir/keepalive; while true; do sleep 5; done"]
volumeMounts:
- name: probe-volume
mountPath: /tempdir
startupProbe:
exec:
command:
- cat
- /tempdir/ready
initialDelaySeconds: 3
failureThreshold: 6
periodSeconds: 1
successThreshold: 1
livenessProbe:
exec:
command:
- cat
- /tempdir/keepalive
failureThreshold: 6
periodSeconds: 1
successThreshold: 1
volumes:
- name: probe-volume
emptyDir:
medium: Memory
sizeLimit: 1Gi
這段清單中的邏輯如下圖
我們使用下面指令查看中間發(fā)生的事件
kubectl describe pod
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 15s default-scheduler Successfully assigned default/startup-liveness-deployment-66f76576ff-9pnmj to ubuntub
Normal Pulling 15s kubelet Pulling image "busybox"
Normal Pulled 13s kubelet Successfully pulled image "busybox" in 2.603715682s (2.603722383s including waiting)
Normal Created 13s kubelet Created container startup-liveness-container
Normal Started 13s kubelet Started container startup-liveness-container
Warning Unhealthy 7s (x4 over 10s) kubelet Startup probe failed: cat: can't open '/tempdir/ready': No such file or directory
Warning Unhealthy 4s (x2 over 5s) kubelet Liveness probe failed: cat: can't open '/tempdir/keepalive': No such file or directory
可以看到Startup Probe在第4次檢測時,/tempdir/ready文件還沒創(chuàng)建。但是第5次時,就檢測到了它,于是進入Liveness Probe檢測狀態(tài)。這從最后兩個行為經(jīng)歷的時間差10s-5s=5s可以印證Startup Probe執(zhí)行的次數(shù)大概在4~5次之間(因為檢測周期periodSeconds是1秒)。
Liveness Probe在第2次檢測時,/tempdir/keepalive還沒創(chuàng)建。到第三次檢測時,這個標志文件就創(chuàng)建了。于是整個Pod進入了Running狀態(tài)。
啟動和就緒探針
# startup_readiness.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: startup-readiness-deployment
spec:
selector:
matchLabels:
app: startup-readiness
template:
metadata:
labels:
app: startup-readiness
spec:
containers:
- name: startup-readiness-container
image: busybox
command: ["/bin/sh", "-c", "sleep 6; touch /tempdir/ready; sleep 3;touch /tempdir/readiness; while true; do sleep 5; done"]
volumeMounts:
- name: probe-volume
mountPath: /tempdir
startupProbe:
exec:
command:
- cat
- /tempdir/ready
initialDelaySeconds: 3
failureThreshold: 6
periodSeconds: 1
successThreshold: 1
readinessProbe:
exec:
command:
- cat
- /tempdir/readiness
failureThreshold: 6
periodSeconds: 1
successThreshold: 1
volumes:
- name: probe-volume
emptyDir:
medium: Memory
sizeLimit: 1Gi
和上一節(jié)流程類似
其執(zhí)行事件如下:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 13s default-scheduler Successfully assigned default/startup-readiness-deployment-64cbcc9659-k7m5v to ubuntuc
Normal Pulling 13s kubelet Pulling image "busybox"
Normal Pulled 11s kubelet Successfully pulled image "busybox" in 2.10831058s (2.10831728s including waiting)
Normal Created 11s kubelet Created container startup-readiness-container
Normal Started 11s kubelet Started container startup-readiness-container
Warning Unhealthy 5s (x4 over 8s) kubelet Startup probe failed: cat: can't open '/tempdir/ready': No such file or directory
Warning Unhealthy 2s (x3 over 4s) kubelet Readiness probe failed: cat: can't open '/tempdir/readiness': No such file or directory
這次readiness檢測到第4次時才認定狀態(tài)為success。
上述兩個實驗可以證明:啟動探針(Startup Probe)檢測狀態(tài)是success后,存活(Liveness Probe)和就緒探針(Readiness Probe)才開始檢測。
存活和就緒探針
# liveness_readiness.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: liveness-readiness-deployment
spec:
selector:
matchLabels:
app: liveness-readiness
template:
metadata:
labels:
app: liveness-readiness
spec:
containers:
- name: liveness-readiness-container
image: busybox
command: ["/bin/sh", "-c", "sleep 3; touch /tempdir/keepalive; sleep 3;touch /tempdir/readiness; while true; do sleep 5; done"]
volumeMounts:
- name: probe-volume
mountPath: /tempdir
livenessProbe:
exec:
command:
- cat
- /tempdir/keepalive
initialDelaySeconds: 3
failureThreshold: 6
periodSeconds: 1
successThreshold: 1
readinessProbe:
exec:
command:
- cat
- /tempdir/readiness
failureThreshold: 6
periodSeconds: 1
successThreshold: 1
volumes:
- name: probe-volume
emptyDir:
medium: Memory
sizeLimit: 1Gi
通過Pod的Event可以看到,Liveness和Readiness Probe的生命長度一致(如下圖都是6秒)。文章來源:http://www.zghlxwxcb.cn/news/detail-648111.html
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 10s default-scheduler Successfully assigned default/liveness-readiness-deployment-f6db88747-znxsm to ubuntub
Normal Pulling 10s kubelet Pulling image "busybox"
Normal Pulled 8s kubelet Successfully pulled image "busybox" in 2.092699902s (2.092706902s including waiting)
Normal Created 8s kubelet Created container liveness-readiness-container
Normal Started 8s kubelet Started container liveness-readiness-container
Warning Unhealthy 5s (x2 over 6s) kubelet Liveness probe failed: cat: can't open '/tempdir/keepalive': No such file or directory
Warning Unhealthy 4s (x4 over 6s) kubelet Readiness probe failed: cat: can't open '/tempdir/readiness': No such file or directory
探針流程
文章來源地址http://www.zghlxwxcb.cn/news/detail-648111.html
到了這里,關(guān)于研發(fā)工程師玩轉(zhuǎn)Kubernetes——啟動、存活和就緒探針的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!