kubernetes 容器健康及就緒檢測

1.健康檢測

exec探測

apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec
spec:
  containers:
  - name: liveness-demo
    image: busybox
    args:
    #容器初始化執行的命令
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 10; rm -rf /tmp/healthy; sleep 600
    #表示,容器一運行就建立一個文件,睡上10s,再刪除這個文件
    livenessProbe:
    #健康檢測
      exec:
      #ExecAction,在容器中執行特定的命令,命令退出返回0表示成功
      #TCPSocketAction,根據容器IP地址及特定的端口進行TCP檢查,端口開放表示成功
      #HTTPGetAction,根據容器IP、端口及訪問路徑發起一次HTTP請求,若是返回碼在200到400之間表示>成功
        command:
        - cat
        - /tmp/healthy
        #這裏用exec探測,不停探測"/tmp/healthy"是否存在,不在了就重啓

http 探測

apiVersion: v1
kind: Pod
metadata:
  name: liveness-http
spec:
  containers:
  - name: liveness-http
    image: nginx
    livenessProbe:
      httpGet:
        path: /index.html
                #探測路徑
        port: 80
        scheme: HTTP
      initialDelaySeconds: 20
      #表示容器啓動20s在,進行第一次探測 默認0s
      periodSeconds: 3
      #表示各3s探測一次 默認10s
      timeoutSeconds: 1
      #表示探測超時時間 默認1s
      successThreshold: 1
      #表示重失敗到正常狀態,要連續探測1次成功就標記爲正常狀態 默認1次
      failureThreshold: 5
      #表示重正常到失敗狀態,要連續探測5次失敗就標記爲失敗狀態 默認3次

[root@localhost ~]# kubectl exec liveness-http -it -- /bin/bash
#登陸進容器
root@liveness-http:/# rm /usr/share/nginx/html/index.html
#手動刪除index.html文件完成手動觸發html

kubectl describe pod liveness-exec |grep Liveness:
#運行命令查看探測狀態信息
kubernetes 容器健康及就緒檢測nginx

httpGet的屬性
host:主機名或IP
scheme:連接類型,HTTP或HTTPS,默認爲HTTP
path:請求路徑
httpHeaders:自定義請求頭
port:請求端口api

TCP探測

apiVersion: v1
kind: Pod
metadata:
  name: liveness-tcp
spec:
  containers:
  - name: liveness-tcp
    image: nginx
    livenessProbe:
      tcpSocket:
        port: 80

2.就緒狀態探測—exec

apiVersion: v1
kind: Pod
metadata:
  name: readiness-exec
spec:
  containers:
  - name: readiness-exec
    image: nginx
    args: ["/bin/sh", "-c", "while true; do touch /tmp/ready; sleep 10; rm -f /tmp/ready; sleep 10; done;"]
    #表示建立個文件暫停10s,刪除文件暫停10s,無限循環
    readinessProbe:
    #就緒狀態監測
      exec:
        command: ["cat", "/tmp/ready"]
      initialDelaySeconds: 5
      periodSeconds: 1
    #該容器每10s會在正常和非正常狀態來回跳轉

readiness配置方式和liveness相似,其餘探測只要修改livenessProbe改成readinessProbe便可bash

相關文章
相關標籤/搜索