本文介紹 Pod 中容器健康檢查相關的內容、配置方法以及實驗測試,實驗環境爲 Kubernetes 1.11,搭建方法參考kubeadm安裝kubernetes V1.11.1 集羣
0. 什麼是 Container Probes
我們先來看一下Kubernetes的架構圖,每個Node節點上都有 kubelet
,Container Probe 也就是容器的健康檢查是由 kubelet
定期執行的。
Kubelet通過調用Pod中容器的Handler來執行檢查的動作,Handler有三種類型。
- ExecAction,在容器中執行特定的命令,命令退出返回0表示成功
- TCPSocketAction,根據容器IP地址及特定的端口進行TCP檢查,端口開放表示成功
- HTTPGetAction,根據容器IP、端口及訪問路徑發起一次HTTP請求,如果返回碼在200到400之間表示成功 每種檢查動作都可能有三種返回狀態。
- Success,表示通過了健康檢查
- Failure,表示沒有通過健康檢查
- Unknown,表示檢查動作失敗
在創建Pod時,可以通過liveness
和readiness
兩種方式來探測Pod內容器的運行情況。liveness
可以用來檢查容器內應用的存活的情況來,如果檢查失敗會殺掉容器進程,是否重啓容器則取決於Pod的重啓策略。readiness
檢查容器內的應用是否能夠正常對外提供服務,如果探測失敗,則Endpoint Controller會將這個Pod的IP從服務中刪除。
1. 應用場景
我們都知道Kubernetes會維持Pod的狀態及個數,因此如果你只是希望保持Pod內容器失敗後能夠重啓,那麼其實沒有必要添加健康檢查,只需要合理配置Pod的重啓策略即可。更適合健康檢查的場景是在我們根據檢查結果需要主動殺掉容器並重啓的場景,還有一些容器在正式提供服務之前需要加載一些數據,那麼可以採用readiness
來檢查這些動作是否完成。
2. liveness 檢查實例
2.1 Container Exec
apiVersion: v1 kind: Pod metadata: labels: test: liveness name: liveness-exec spec: containers: - name: liveness image: docker.io/alpine args: - /bin/sh - -c - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600 livenessProbe: exec: command: - cat - /tmp/healthy initialDelaySeconds: 5 periodSeconds: 5
本例創建了一個容器,通過檢查一個文件是否存在來判斷容器運行是否正常。容器運行30秒後,將文件刪除,這樣容器的liveness檢查失敗從而會將容器重啓。
2.2 HTTP Health Check
apiVersion: v1 kind: Pod metadata: labels: test: liveness app: httpd name: liveness-http spec: containers: - name: liveness image: docker.io/httpd ports: - containerPort: 80 livenessProbe: httpGet: path: /index.html port: 80 httpHeaders: - name: X-Custom-Header value: Awesome initialDelaySeconds: 5 periodSeconds: 5
本例通過創建一個Apache服務器,通過訪問 index 來判斷服務是否存活。通過手工刪除這個文件的方式,可以導致檢查失敗,從而重啓容器。
[root@devops-101 ~]# kubectl exec -it liveness-http /bin/sh # # ls bin build cgi-bin conf error htdocs icons include logs modules # ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 11:39 ? 00:00:00 httpd -DFOREGROUND daemon 6 1 0 11:39 ? 00:00:00 httpd -DFOREGROUND daemon 7 1 0 11:39 ? 00:00:00 httpd -DFOREGROUND daemon 8 1 0 11:39 ? 00:00:00 httpd -DFOREGROUND root 90 0 0 11:39 ? 00:00:00 /bin/sh root 94 90 0 11:39 ? 00:00:00 ps -ef # # cd /usr/local/apache2 # ls bin build cgi-bin conf error htdocs icons include logs modules # cd htdocs # ls index.html # rm index.html # command terminated with exit code 137 [root@devops-101 ~]# kubectl describe pod liveness-http Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 1m default-scheduler Successfully assigned default/liveness-http to devops-102 Warning Unhealthy 8s (x3 over 18s) kubelet, devops-102 Liveness probe failed: HTTP probe failed with statuscode: 404 Normal Pulling 7s (x2 over 1m) kubelet, devops-102 pulling image "docker.io/httpd" Normal Killing 7s kubelet, devops-102 Killing container with id docker://liveness:Container failed liveness probe.. Container will be killed and recreated. Normal Pulled 1s (x2 over 1m) kubelet, devops-102 Successfully pulled image "docker.io/httpd" Normal Created 1s (x2 over 1m) kubelet, devops-102 Created container Normal Started 1s (x2 over 1m) kubelet, devops-102 Started container
2.3 TCP Socket
這種方式通過TCP連接來判斷是否存活,Pod編排示例。
apiVersion: v1 kind: Pod metadata: labels: test: liveness app: node name: liveness-tcp spec: containers: - name: goproxy image: docker.io/googlecontainer/goproxy:0.1 ports: - containerPort: 8080 readinessProbe: tcpSocket: port: 8080 initialDelaySeconds: 5 periodSeconds: 10 livenessProbe: tcpSocket: port: 8080 initialDelaySeconds: 15 periodSeconds: 20
3. readiness 檢查實例
另一種 readiness
配置方式和liveness
類似,只要修改livenessProbe
改爲readinessProbe
即可。
4. 配置參數
我們可以通過kubectl explain
命令來查看具體的配置屬性,在這裏還是簡單列一下主要的屬性。
- initialDelaySeconds:檢查開始執行的時間,以容器啓動完成爲起點計算
- periodSeconds:檢查執行的週期,默認爲10秒,最小爲1秒
- timeoutSeconds:檢查超時的時間,默認爲1秒,最小爲1秒
- successThreshold:從上次檢查失敗後重新認定檢查成功的檢查次數閾值(必須是連續成功),默認爲1
- failureThreshold:從上次檢查成功後認定檢查失敗的檢查次數閾值(必須是連續失敗),默認爲1
- httpGet的屬性
- host:主機名或IP
- scheme:鏈接類型,HTTP或HTTPS,默認爲HTTP
- path:請求路徑
- httpHeaders:自定義請求頭
- port:請求端口