Docker Kubernetes 健康檢查

Docker Kubernetes 健康檢查html

  • 官方文檔:https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/

提供Probe探測機制,有如下兩種類型:nginx

  • livenessProbe:若是檢查失敗,將殺死容器,而後根據Pod的重啓策略來決定是否重啓(根據Pod的restartPolicy來操做)。
  • readinessProbe:若是檢查失敗,Kubernetes會把Pod從服務代理的分發後端剔除。

Probe支持如下三種檢查方法:docker

  • httpGet
  • 發送HTTP請求,返回200-400範圍狀態碼爲成功。
  • exec
  • 執行Shell命令返回狀態碼是0爲成功。
  • tcpSocket
  • 發起TCP Socket創建成功。判斷端口有沒有打開

環境:vim

  • 系統:Centos 7.4 x64
  • Docker版本:18.09.0
  • Kubernetes版本:v1.8
  • 管理節點:192.168.1.79
  • 工做節點:192.168.1.78
  • 工做節點:192.168.1.77

案例一

一、管理節點:建立yaml文件後端

vim check.yamlapi

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.10
    ports:
    - containerPort: 80
    livenessProbe:
      httpGet:
        path: /index.html
        port: 80
# api版本
apiVersion: v1
# 指定建立資源對象
kind: Pod
# 源數據、能夠寫name,命名空間,對象標籤
metadata:
# 服務名稱
  name: nginx-pod
# 標籤
  labels:
# 標籤名
    app: nginx 
# 容器資源信息
spec:
# 容器管理
  containers:
# 容器名稱
  - name: nginx
# 容器鏡像
    image: nginx:1.10
# 端口管理
    ports:
# 指定暴露端口
    - containerPort: 80
# 健康檢查模式(httpGet、exec、tcpSocket)
    livenessProbe:
# 選擇健康檢查類型
      httpGet:
# 選擇檢查文件
        path: /index.html
# 選擇檢查暴露端口
        port: 80
文件註釋

二、管理節點:建立Podapp

kubectl create -f check.yaml
命令:kubectl describe pods nginx-pod

# 探測端口爲80,探測文件名index.html,timeout超市時間爲一秒,period每10秒探測一次
    Liveness:       http-get http://:80/index.html delay=0s timeout=1s period=10s #success=1 #failure=3
查看健康檢查pod狀態

 案例二

語法格式tcp

# 語法格式
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness
    image: k8s.gcr.io/busybox
    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

 

一、經過官方實例測試健康檢查ide

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness
    image: busybox
    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
vim pod4.yaml

二、執行測試

kubectl create -f pod4.yaml

三、查看測試

kubectl get pods

通過一段時間檢查重啓

四、查看事件

 kubectl describe pod liveness-exec

....
Events:
  Type     Reason     Age                   From                    Message
  ----     ------     ----                  ----                    -------
  Normal   Scheduled  5m8s                  default-scheduler       Successfully assigned default/liveness-exec to 192.168.1.110
  Normal   Pulled     2m35s (x3 over 5m7s)  kubelet, 192.168.1.110  Successfully pulled image "busybox"
  Normal   Created    2m35s (x3 over 5m6s)  kubelet, 192.168.1.110  Created container
  Normal   Started    2m34s (x3 over 5m6s)  kubelet, 192.168.1.110  Started container
  Warning Unhealthy 112s (x9 over 4m32s) kubelet, 192.168.1.110  Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
  Normal   Pulling    81s (x4 over 5m7s)    kubelet, 192.168.1.110  pulling image "busybox"
  Normal   Killing    6s (x4 over 3m51s)    kubelet, 192.168.1.110  Killing container with id docker://liveness:Container failed liveness probe.. Container will be killed and recreated.
相關文章
相關標籤/搜索