gpt4 book ai didi

spring-boot - 在 Kubernetes 中,如何检测到我的 pod 已准备就绪?

转载 作者:行者123 更新时间:2023-12-02 12:17:05 24 4
gpt4 key购买 nike

我有一个使用就绪探针的 Kubernetes pod,并与服务绑定(bind),这确保我在准备好之前不会收到流量。

我使用 Spring Actuator 作为这个就绪探测的健康端点。

但我想在 kubelet 认为 pod 准备就绪时触发一些操作。

最简单的方法是什么?

最佳答案

也许 实现您自己的健康检查 .当您第一次发现一切正常时,运行您的代码。

我使用静态变量 firstHealthCheckOK 进行检查。您的逻辑应该只运行一次。

我假设您正在运行 Spring-boot 2.x 并在 http://localhost:8080/actuator/health 上调用就绪探测。

下面的 health() 方法在 Kubernetes 调用 http://localhost:8080/actuator/health 时被调用

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class HealthCheck implements HealthIndicator {

static boolean firstHealthCheckOK = false;

@Override
public Health health() {
int errorCode = check(); // perform health check
if (errorCode != 0) {
return Health.down()
.withDetail("Error Code", errorCode).build();
}
if (firstHealthCheckOK == false){
firstHealthCheckOK = true;
doStartUpLogic();
}
return Health.up().build();
}

private int check() {
//some logic
return 0;
}

private void doStartUpLogic() {
//some startup logic
}
}

关于spring-boot - 在 Kubernetes 中,如何检测到我的 pod 已准备就绪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55237014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com