我很難讓 kubernetes livenessProbe exec 命令使用環境變數。我的目標是讓 liveness 探針監控 pod 上的記憶體使用情況,并執行 httpGet 運行狀況檢查。
“如果容器記憶體使用量超過資源限制的 90% 或 http 回應代碼/health失敗,則探測應該失敗。”
活性探針配置如下:
livenessProbe:
exec:
command:
- sh
- -c
- |-
"used=$(awk '{ print int($1/1.049e 6) }' /sys/fs/cgroup/memory/memory.usage_in_bytes);
thresh=$(awk '{ print int( $1 / 1.049e 6 * 0.9 ) }' /sys/fs/cgroup/memory/memory.limit_in_bytes);
health=$(curl -s -o /dev/null --write-out "%{http_code}" http://localhost:8080/health);
if [[ ${used} -gt ${thresh} || ${health} -ne 200 ]]; then exit 1; fi"
initialDelaySeconds: 240
periodSeconds: 60
failureThreshold: 3
timeoutSeconds: 10
如果我執行到 (ubuntu) pod 并運行這些命令,它們都可以正常作業并完成作業。
但是當部署為 livenessProbe 時,pod 不斷失敗并顯示以下警告:
Events: │
│ Type Reason Age From Message │
│ ---- ------ ---- ---- ------- │
│ Warning Unhealthy 14m (x60 over 159m) kubelet (combined from similar events): Liveness probe failed: sh: 4: used=1608; │
│ thresh=2249; │
│ health=200; │
│ if [[ -gt || -ne 200 ]]; then exit 1; fi: not found
看起來好像探測記憶體和卷曲健康檢查端點的初始命令都有效并填充了環境變數,但是這些變數替換隨后沒有填充到 if 陳述句中,因此探測永遠不會通過。
知道為什么嗎?或者如何配置才能正常作業?我知道這有點令人費解。提前致謝。
uj5u.com熱心網友回復:
看起來 shell 將您的整個命令視為要執行的檔案名。
我會洗掉外部引號
livenessProbe:
exec:
command:
- sh
- -c
- |-
used=$(awk '{ print int($1/1.049e 6) }' /sys/fs/cgroup/memory/memory.usage_in_bytes);
thresh=$(awk '{ print int( $1 / 1.049e 6 * 0.9 ) }' /sys/fs/cgroup/memory/memory.limit_in_bytes);
health=$(curl -s -o /dev/null --write-out "%{http_code}" http://localhost:8080/health);
if [[ ${used} -gt ${thresh} || ${health} -ne 200 ]]; then exit 1; fi
initialDelaySeconds: 240
periodSeconds: 60
failureThreshold: 3
timeoutSeconds: 10
你已經告訴 YAML 決議器它是一個多行字串
uj5u.com熱心網友回復:
我認為您問題的根源在于bash和sh(shell)之間的混淆。兩者都在容器中廣泛使用(但有時不存在 bash)但 bash 具有更多功能。在這里您使用[[哪個特定于 bash,sh 不知道它并可能導致不需要的行為。
如果它存在于容器中,首先在您的命令中替換sh為bash。如果不是,您將不得不使用 shell 語法來執行條件命令。
然后可以通過利用其他 Kubernetes 功能來完善您的活躍度探測:
為避免較大的初始延遲,請使用啟動探針。它將禁用其他探測器,直到它以一次成功回應并且應具有較高的 failureThreshold。它允許在容器啟動速度比預期更快的情況下提供靈活性,并在您添加其他探測器時集中延遲(這意味著沒有值重復)。
使用該
resources欄位。它允許您為特定部署或 pod指定記憶體和 CPU限制和請求(閱讀檔案)。因為活性探測失敗意味著您的 pod 將重新啟動,所以設定限制會做同樣的事情,但更干凈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/337299.html
標籤:Kubernetes 活性探针
