探針是由kubelet對容器執(zhí)行的定期診斷。要執(zhí)行診斷,kubelet調(diào)用由容器實現(xiàn)的Handler。有三種類型的處理程序
ExecAction:在容器內(nèi)執(zhí)行指定命令。如果命令退出時返回碼為0則認(rèn)為診斷成功
TCPSockertAction:對指定端口上的容器的ip地址執(zhí)行TCP檢查。如果端口打開,則診斷被認(rèn)為是成功的
HTTPGetAction:對指定的端口和路勁上的容器的ip地址執(zhí)行HTTP Get請求。如果響應(yīng)的狀態(tài)碼大于等于200且小于400,則診斷被認(rèn)為成功
探測方式
livenessProbe:指示容器是否正在運行。如果存活探測失敗,則kubelet會殺死容器,并且容器將受到其重啟策略的影響。如果容器不提供存活探針,則默認(rèn)狀態(tài)為Success
readinessProbe:指示容器是否準(zhǔn)備好服務(wù)請求。如果就緒探測失敗,端點控制器將從與pod匹配的所有Service的端點中刪除該pod的ip地址。初始延遲之前的就緒狀態(tài)默認(rèn)為Failure。如果容器不提供就緒探針,則默認(rèn)為Success
檢測探針-就緒檢測
rsadinessProbe-httpget
示例
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
spec:
containers:
- name: readiness-httpget-container
image: nginx
imagePullPolicy: IfNotPresent //鏡像下載策略 如果本地有就不遠(yuǎn)程下載
readinessProbe: //就緒檢測
httpGet: //httpGet檢測方案
port: 80
path: /index1.html //檢查路勁
initialDelaySeconds: 1 //檢測延時 1秒之后開始檢測
periodSeconds: 3 //重試檢測時間
檢測探針-存活檢測
livenessProbe-exec
示例
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
spec
containes:
- name: liveness-exec-container
image: busybox
imagePullPolicy: IfNotPresent
command: ['/bin/sh','-c','touch /tmp/liven; sleep 60; rm -rf /tmp/liven; sleep 3600']
livenessProbe:
exec:
command: ['test','-e','/tmp/liven'] //檢測文件是否存在
initiaiDelaySeconds: 1 //檢測延時 1秒之后開始檢測
periodSeconds: 3 //重試檢測時間
檢測結(jié)果
[root@master yaml]# kubectl get po -w
NAME READY STATUS RESTARTS AGE
liveness-exec-pod 1/1 Running 0 33s
liveness-exec-pod 1/1 Running 1 101s
會反復(fù)重啟
livenessProbe-httpget
示例
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
spec:
containers:
- name: liveness-httpget-container
image: busybox
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1 //檢測延時 1秒之后開始檢測
periodSeconds: 3 //重試檢測時間
timeoutSeconds: 10 //每次訪問最大超時時間
檢測結(jié)果
正常情況
[root@master yaml]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
liveness-httpget-pod 1/1 Running 0 19s 10.244.2.57 node2 <none> <none>
[root@master yaml]# curl 10.244.2.57
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
進(jìn)入容器刪除index.html文件再次查看
[root@master yaml]# kubectl exec -it liveness-httpget-pod bash
root@liveness-httpget-pod:/# rm -rf /usr/share/nginx/html/index.html
root@liveness-httpget-pod:/# exit
exit
[root@master yaml]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
liveness-httpget-pod 1/1 Running 1 62s 10.244.2.57 node2 <none> <none>
看到pod已經(jīng)重啟
livenessProbe-tcp
示例
apiVersion: v1
kind: Pod
metadata:
name: probe-tcp
spec:
containers:
- name: nginx
image: nginx
livenessProbe:
initialDelaySeconds: 5
periodSeconds: 3
timeoutSeconds: 1
tcpSocket:
port: 88
檢測結(jié)果
[root@master yaml]# kubectl get po -w
NAME READY STATUS RESTARTS AGE
probe-tcp 1/1 Running 0 6s
probe-tcp 1/1 Running 1 24s
probe-tcp 1/1 Running 2 43s
會發(fā)現(xiàn)pod一直重啟 原因是設(shè)置的tcpSocket與容器的端口連接不上
啟動、退出動作
示例
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: lifecycle-demo-container
image: nginx
lifecycle:
postStart:
exec:
command: ['/bin/sh','-c','echo hello hello hello > /usr/share/message']
preStop:
exec:
command: ['/bin/sh','-c','echo bye bye bye > /usr/share/message']
Pod的狀態(tài)可能存在的值
1,掛起(pending):pod已被kubernetes系統(tǒng)接受,但有一個或者多個容器鏡像尚未創(chuàng)建,等待時間包括調(diào)度pod的時間和通過網(wǎng)絡(luò)下載鏡像的時間,需要花點時間
2,運行中(running):該pod已經(jīng)綁定到了一個節(jié)點上,pod中所有的容器都已被創(chuàng)建,至少一個容器正在運行,或者正處于啟動或重啟狀態(tài)
3,成功(succeeded):pod中的所有容器都被成功終止,并且不會再啟動
4,失敗(failed):pod中的所有容器已經(jīng)終止,并且至少有一個容器是因為失敗終止,也就是說容器以非0狀態(tài)退出或者被系統(tǒng)終止
5,未知(unknow):因為某些原因無法取得pod的狀態(tài),通常是因為pod所在主機(jī)通信失敗