Kubernetes Pod中容器的Liveness、Readiness和Startup探針

我最新最全的文章都在 南瓜慢說 www.pkslow.com ,歡迎大家來喝茶!

1 探針的作用

Kubernetes的容器生命週期管理中,有三種探針,首先要知道,這探針是屬於容器的,而不是Pod

  • 存活探針:Liveness
  • 就緒探針:Readiness
  • 啓動探針:Startup

Liveness探針可以知道什麼時候要重啓容器,如果發現容器不健康,就會殺死並重新創建新的容器。

Readniess探針可以知道要不要訪問容器,如果發現容器不健康,就不會把請求路由到該容器。

Startup探針可以知道應用程序容器什麼時候啓動了。 如果配置了這類探測器,就可以控制容器在啓動成功後再進行存活性和就緒檢查, 確保這些存活、就緒探測器不會影響應用程序的啓動。 這可以用於對慢啓動容器進行存活性檢測,避免它們在啓動運行之前就被殺掉。

2 配置示例

2.1 存活Liveness

2.1.1 命令方式

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

2.1.2 HTTP方式

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-http
spec:
  containers:
  - name: liveness
    image: k8s.gcr.io/liveness
    args:
    - /server
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
        httpHeaders:
        - name: Custom-Header
          value: Awesome
      initialDelaySeconds: 3
      periodSeconds: 3

2.1.3 TCP方式

apiVersion: v1
kind: Pod
metadata:
  name: goproxy
  labels:
    app: goproxy
spec:
  containers:
  - name: goproxy
    image: k8s.gcr.io/goproxy:0.1
    ports:
    - containerPort: 8080
    readinessProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 10
    livenessProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 15
      periodSeconds: 20

2.2 就緒Readiness

Liveness類似:

readinessProbe:
  exec:
    command:
    - cat
    - /tmp/healthy
  initialDelaySeconds: 5
  periodSeconds: 5

2.3 使用啓動探測器保護慢啓動容器

幸虧有啓動探測,應用程序將會有最多 5 分鐘(30 * 10 = 300s) 的時間來完成它的啓動。 一旦啓動探測成功一次,存活探測任務就會接管對容器的探測,對容器死鎖可以快速響應。 如果啓動探測一直沒有成功,容器會在 300 秒後被殺死,並且根據 restartPolicy 來設置 Pod 狀態。

ports:
- name: liveness-port
  containerPort: 8080
  hostPort: 8080

livenessProbe:
  httpGet:
    path: /healthz
    port: liveness-port
  failureThreshold: 1
  periodSeconds: 10

startupProbe:
  httpGet:
    path: /healthz
    port: liveness-port
  failureThreshold: 30
  periodSeconds: 10

3 Springboot應用的配置

Springboot 2.3新增了探針,具體路徑如下:

存活:/actuator/health/liveness

就緒:/actuator/health/readiness

需要通過添加actuator,並通過屬性配置打開對應功能:

management.endpoints.web.exposure.include="*"
management.health.livenessState.enabled=true
management.health.readinessState.enabled=true
management.endpoint.health.probes.enabled=true
management.endpoint.health.probes.show-details=always

歡迎關注微信公衆號<南瓜慢說>,將持續爲你更新...

多讀書,多分享;多寫作,多整理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章