【问题标题】:k8s spring boot pod failing readiness and liveness probek8s spring boot pod 准备就绪和活跃度探测失败
【发布时间】:2022-02-02 12:58:04
【问题描述】:

我已经配置了一个 spring-boot pod 并配置了 livenessreadiness 探针。 当我启动 pod 时,describe 命令显示以下输出。

Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  92s                default-scheduler  Successfully assigned pradeep-ns/order-microservice-rs-8tqrv to pool-h4jq5h014-ukl3l
  Normal   Pulled     43s (x2 over 91s)  kubelet            Container image "classpathio/order-microservice:latest" already present on machine
  Normal   Created    43s (x2 over 91s)  kubelet            Created container order-microservice
  Normal   Started    43s (x2 over 91s)  kubelet            Started container order-microservice
  Warning  Unhealthy  12s (x6 over 72s)  kubelet            Liveness probe failed: Get "http://10.244.0.206:8222/actuator/health/liveness": dial tcp 10.244.0.206:8222: connect: connection refused
  Normal   Killing    12s (x2 over 52s)  kubelet            Container order-microservice failed liveness probe, will be restarted
  Warning  Unhealthy  2s (x8 over 72s)   kubelet            Readiness probe failed: Get "http://10.244.0.206:8222/actuator/health/readiness": dial tcp 10.244.0.206:8222: connect: connection refused

pod 定义如下

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: order-microservice-rs
  labels:
    app: order-microservice
spec:
  replicas: 1
  selector:
    matchLabels:
      app: order-microservice
  template:
    metadata:
      name: order-microservice
      labels:
        app: order-microservice
    spec:
      containers:
        - name: order-microservice
          image: classpathio/order-microservice:latest
          imagePullPolicy: IfNotPresent
          env:
            - name: SPRING_PROFILES_ACTIVE
              value: dev
            - name: SPRING_DATASOURCE_USERNAME
              valueFrom:
                secretKeyRef:
                  key: username
                  name: db-credentials
            - name: SPRING_DATASOURCE_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: password
                  name: db-credentials
          volumeMounts:
            - name: app-config
              mountPath: /app/config
            - name: app-logs
              mountPath: /var/log
          livenessProbe:
            httpGet:
              port: 8222
              path: /actuator/health/liveness
            initialDelaySeconds: 10
            periodSeconds: 10
          readinessProbe:
            httpGet:
              port: 8222
              path: /actuator/health/readiness
            initialDelaySeconds: 10
            periodSeconds: 10
          resources:
            requests:
              memory: "550Mi"
              cpu: "500m"
            limits:
              memory: "550Mi"
              cpu: "750m"
      volumes:
        - name: app-config
          configMap:
            name: order-microservice-config
        - name: app-logs
          emptyDir: {}
      restartPolicy: Always

如果我在 replica-set 清单中禁用 livenessreadiness 探测并将 exec 放入 pod,则在调用 http://localhost:8222/actuator/health/livenesshttp://localhost:8222/actuator/health/readiness 端点时会得到有效响应。 为什么在使用 Kubernetes 调用 readinessliveness 端点时,我的 pod 会重新启动并失败。我哪里错了?

更新 如果我删除 resource 部分,则 pod 正在运行,但添加 resource 参数后,probes 将失败。

【问题讨论】:

  • pod重启时,spring boot应用启动完成了吗? @zilcuanu

标签: spring-boot kubernetes livenessprobe


【解决方案1】:

当您将容器/spring 应用程序限制为 0.5 核(500 毫核)时,启动时间可能比给定的活性探测阈值更长。

您可以增加它们,或者使用具有更宽松设置的 startupProbe(例如 failureThreshold 10)。在这种情况下,您可以减少 liveness 探测的时间,并在检测到容器启动成功后获得更快的反馈。

【讨论】:

  • 如何知道spring boot应用的cores和mem设置?到达号码的准则是什么
  • 这取决于具体的应用。我建议在启动和常规操作期间观察它并得出适当的值。
【解决方案2】:

您的 pod 配置仅提供 0.5 核 CPU,您的检查时间太短。根据您的服务器 CPU 性能,spring boot 启动可能需要 10 秒以上的时间。这是我对 spring boot pod 的配置,可能会给你一个点。

"livenessProbe": {
              "httpGet": {
                "path": "/actuator/liveness",
                "port": 11032,
                "scheme": "HTTP"
              },
              "initialDelaySeconds": 90,
              "timeoutSeconds": 30,
              "periodSeconds": 30,
              "successThreshold": 1,
              "failureThreshold": 3
            },
            "readinessProbe": {
              "httpGet": {
                "path": "/actuator/health",
                "port": 11032,
                "scheme": "HTTP"
              },
              "initialDelaySeconds": 60,
              "timeoutSeconds": 30,
              "periodSeconds": 30,
              "successThreshold": 1,
              "failureThreshold": 3
            },

而且我没有限制CPU和内存资源,如果限制了CPU,会花费更多时间。希望这对你有帮助。

【讨论】:

    【解决方案3】:

    当您尝试针对您的localhost 的请求并且它有效时,它不能保证它会在其他网络接口上有效。 Kubelet 是一个节点代理,因此请求将发送到您的 eth0 或同等地址,而不是您的 localhost

    您可以通过从另一个 pod 向您的 pod 的 IP 地址或备份它的服务发出请求来检查它。

    您可能正在使您的应用程序在localhost 上提供服务,而您必须使其在0.0.0.0eth0 上提供服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多