探針-就緒探測、存活探測
探針是由kubelet對容器執行的定期診斷,要執行診斷,kubelet調用由容器實現的Handler,有三種類型的處理程序:
ExecActive:在容器內執行指定命令,若命令退出時返回碼為0,則認為診斷成功
TCPSockeAction:對指定端口上的容器的ip地址進行tcp檢查,如果端口打開,則診斷為成功
HTTPGetAction:對指定端口和路徑上的容器ip地址執行HTTP Get請求,如果相應的狀態碼>200且<400,則診斷是成功的
每次探測都將獲得以下三種結果之一:
成功:容器通過了診斷
失敗:容器未通過診斷
未知:診斷失敗,不會采取任何行動
兩種探測方式:
- livenessProbe(存活檢測):指示容器是否正在運行,如果存活檢測失敗,則kubelet會殺死容器,並且容器將受到其重啟策略的影響。如果容器不提供存活探針,則默認狀態為Success
- readinessProbe(就緒探測):指示容器是否准備好服務請求。如果就緒探測失敗,端點控制器將從與Pod匹配的所有的service的端點中刪除該Pod的IP地址,初始延遲之前的就緒狀態默認為Failure。如果容器不提供就緒探針,則默認狀態為Success
測試
## 就緒探測 ## 創建yaml文件 vim readinessProbe-httpget.yaml ... apiVersion: v1 kind: Pod metadata: name: readiness-httpget-pod namespace: default spec: containers: - name: readiness-httpget-container image: hub.vfancloud.com/test/myapp:v1 imagePullPolicy: IfNotPresent readinessProbe: httpGet: port: 80 path: /index1.html initialDelaySeconds: 1 #Pod開啟后,延遲1s再進行檢測 periodSeconds: 3 #檢測間隔時長 ... ## 查看Pod狀態,狀態為運行,卻沒ready [root@Centos8 k8sYaml]# kubectl get pod NAME READY STATUS RESTARTS AGE readiness-httpget-pod 0/1 Running 0 14s ## 查看描述,報錯404 [root@Centos8 k8sYaml]# kubectl describe pod readiness-httpget-pod Warning Unhealthy 0s (x2 over 2s) kubelet, testcentos7 Readiness probe failed: HTTP probe failed with statuscode: 404 ## 添加index1.html即可ready [root@Centos8 k8sYaml]# kubectl exec -it readiness-httpget-pod -- touch /usr/share/nginx/html/index1.html [root@Centos8 k8sYaml]# kubectl get pod NAME READY STATUS RESTARTS AGE readiness-httpget-pod 1/1 Running 0 5m5s
存活檢測
## 存活檢測,只要存活檢測失敗,就會重啟Pod ## 創建exec yaml文件 vim livenessProbe-exec.yaml ... apiVersion: v1 kind: Pod metadata: name: liveness-exec-pod namespace: default spec: containers: - name: liveness-exec-container image: busybox:v1 imagePullPolicy: IfNotPresent command: ['sh','-c','touch /tmp/live ;sleep 60; rm -f /tmp/live; sleep 3600'] livenessProbe: exec: command: ['test','-e','/tmp/live'] #檢測有無/tmp/live文件 initialDelaySeconds: 1 #Pod開啟后,延遲1s再進行檢測 periodSeconds: 3 #檢測間隔時長 ... ## 前60s Pod是正常運行狀態 [root@Centos8 k8sYaml]# kubectl get pod NAME READY STATUS RESTARTS AGE liveness-exec-pod 1/1 Running 0 5s readiness-httpget-pod 1/1 Running 0 39m ## 60s后,Pod已經出錯重啟一次 [root@Centos8 k8sYaml]# kubectl get pod NAME READY STATUS RESTARTS AGE liveness-exec-pod 1/1 Running 1 118s readiness-httpget-pod 1/1 Running 0 41m ## 創建httpGet yaml文件 vim livenessProbe-httpget.yaml ... apiVersion: v1 kind: Pod metadata: name: liveness-httpget-pod namespace: default spec: containers: - name: liveness-httpget-container image: hub.vfancloud.com/test/myapp:v1 imagePullPolicy: IfNotPresent ports: - name: http containerPort: 80 livenessProbe: httpGet: port: 80 path: /index.html #請求此文件內容 initialDelaySeconds: 1 periodSeconds: 3 timeoutSeconds: 10 #最大請求時長 ... ## 查看pod [root@Centos8 k8sYaml]# kubectl get pod NAME READY STATUS RESTARTS AGE liveness-httpget-pod 1/1 Running 0 3s ## 測試刪除index.html文件,Pod已經開始重啟 [root@Centos8 k8sYaml]# kubectl exec -it liveness-httpget-pod -- rm -f /usr/share/nginx/html/index.html [root@Centos8 k8sYaml]# kubectl get pod NAME READY STATUS RESTARTS AGE liveness-httpget-pod 1/1 Running 1 84s ## 創建tcp yaml文件 vim livenessProbe-tcp.yaml ... apiVersion: v1 kind: Pod metadata: name: liveness-httpget-pod namespace: default spec: containers: - name: nginx image: hub.vfancloud.com/test/myapp:v1 imagePullPolicy: IfNotPresent livenessProbe: tcpSocket: #檢測8080是否存在 port: 8080 initialDelaySeconds: 1 periodSeconds: 3 timeoutSeconds: 10 ... ## 因為鏡像內沒開啟8080,所以Pod會一直重啟 [root@Centos8 k8sYaml]# kubectl get pod NAME READY STATUS RESTARTS AGE liveness-httpget-pod 1/1 Running 1 18s