gashirar's blog

ウイスキーがすき/美味しいものがすき/k8sがすき

periodSecondsの値が初回のProbe実行タイミングに影響を与える件

TL;DR

はじめに

Kubernetesにおけるコンテナの死活チェックとしておなじみなのがLiveness Probe/Readiness Probe
(v1.18の時点でbetaだが、StartUp Probeもある。 )

Configure Liveness, Readiness and Startup Probes - Kubernetes

ProbeのinitialDelaySecondsというパラメータは

Number of seconds after the container has started before liveness probes are initiated.

とあるように初回Probe実行までの遅延時間を設定するパラメータで、起動に時間がかかるコンテナの初回Probe実行タイミングのコントロールで使ったりする。
のだが、同じinitialDelaySecondsにしていても初回実行までの時間が大きく異なる場合があったので調査した。

タイミングの検証

下記が疎通用のDeployment Manifest。v1.17.0環境で確認。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: probecheck
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      volumes:
      - name: html
        configMap:
          name: html
      containers:
      - name: httpd
        image:  httpd:2.4
        ports:
          - containerPort: 80
        volumeMounts:
        - name: html
          mountPath: /usr/local/apache2/htdocs/
        livenessProbe:
          httpGet:
            path: /liveness.html
            port: 80
          periodSeconds: 5
          initialDelaySeconds: 10
---
apiVersion: v1
data:
  liveness.html: |
    <!DOCTYPE html>
    <html lang="ja">
    <head>
      <meta charset="utf-8">
      <title>liveness</title>
    </head>
    <body>
        liveness
    </body>
    </html>
kind: ConfigMap
metadata:
  name: html

initialDelaySecondsは10としているため、10秒後くらいに初回Probeが実行されそう。

結果

上記Manifestをapplyしてkubectl logsで確認した結果が下記

[Mon Apr 20 06:42:00.083850 2020] [core:notice] [pid 1:tid 140532771124352] AH00094: Command line: 'httpd -D FOREGROUND'
10.244.0.1 - - [20/Apr/2020:06:42:11 +0000] "GET /liveness.html HTTP/1.1" 200 135
10.244.0.1 - - [20/Apr/2020:06:42:16 +0000] "GET /liveness.html HTTP/1.1" 200 135

Apacheの起動時間が06:42:00で初回Probeが06:42:11。大体10秒後に初回Probeが実行されていることがわかる。
次にperiodSecondsを30秒と設定して動かした結果がこちら。

[Mon Apr 20 06:47:06.578421 2020] [core:notice] [pid 1:tid 140531416900736] AH00094: Command line: 'httpd -D FOREGROUND'
10.244.0.1 - - [20/Apr/2020:06:47:36 +0000] "GET /liveness.html HTTP/1.1" 200 135
10.244.0.1 - - [20/Apr/2020:06:48:06 +0000] "GET /liveness.html HTTP/1.1" 200 135

Apacheの起動時間が06:47:06で初回Probeが06:47:36。initialDelaySecondsは10秒のままだが、初回Probeまでに30秒程度かかっている。

issue探し

なんだか奇妙な動きをしているのでKubernetesのissueをあさってみたところ該当するものを発見。

github.com

どうやらperiodSecondsをもとに係数をかけた時間だけSleepしている模様。ソースコードのコメントをみると

// If kubelet restarted the probes could be started in rapid succession.
// Let the worker wait for a random portion of tickerPeriod before probing.

のようにあるので意図して上記のような動きになっている

おわりに

自分としては現状の動きでアプリケーションへの影響はないので「ふーんそうなのかー」程度にみていたが、 Revisitされていたり今後変更はあるかもしれない。

[Revisit] Odd timing behavior with Readiness Probes with `initialDelaySeconds` and `periodSeconds` · Issue #80431 · kubernetes/kubernetes · GitHub