我最新最全的文章都在南瓜慢說 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
歡迎關注微信公眾號<南瓜慢說>,將持續為你更新...
多讀書,多分享;多寫作,多整理。