[英]In Kubernetes, how can I detect that my pod is ready?
I have a Kubernetes pod using a readiness probe, and tied with the service this ensures that I don't receive traffic until I'm ready.我有一个使用就绪探针的 Kubernetes pod,并与服务绑定,这确保我在准备好之前不会收到流量。
I'm using Spring Actuator as the health endpoint for this readiness probe.我使用 Spring Actuator 作为此就绪探测的运行状况端点。
But i'd like to trigger some actions whenever the pod is deemed ready by the kubelet.但是我想在 kubelet 认为 pod 准备就绪时触发一些操作。
What would be the simplest way to do this?什么是最简单的方法来做到这一点?
Perhaps implement your own HealthCheck .也许实施您自己的 HealthCheck 。 When you find that everything is ok for the first time, run your code.当您第一次发现一切正常时,运行您的代码。
I use a static variable firstHealthCheckOK is checked.我使用静态变量 firstHealthCheckOK 进行检查。 Your logic should run only once.您的逻辑应该只运行一次。
I am assuming you are running Spring-boot 2.x and are calling a readiness probe on http://localhost:8080/actuator/health我假设您正在运行 Spring-boot 2.x 并在http://localhost:8080/actuator/health上调用就绪探测器
The health() method below is called when Kubernetes calls 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
}
}
You can use a post-install hook provided by helm charts (in case you are using helm to deploy your application).您可以使用由 helm charts 提供的安装后挂钩(以防您使用 helm 部署您的应用程序)。 This will perform necessary actions/script/jobs after the pod becomes up and running.这将在 pod 启动并运行后执行必要的操作/脚本/作业。
As part of the pod lifecycle events, you may want to attach additional handlers such as podStart
and build your custom logic to manipulate the event happening as need be.作为 pod 生命周期事件的一部分,您可能希望附加其他处理程序(例如podStart
并构建您的自定义逻辑以根据需要操作发生的事件。
Alternatively, you can also run your code to read REST response from GET /api/v1/namespaces/{namespace}/pods/{name}/log
build any downstream logic to get pod status或者,您也可以运行您的代码以从GET /api/v1/namespaces/{namespace}/pods/{name}/log
读取 REST 响应构建任何下游逻辑以获取 pod 状态
Note, in controlled environments, it's good to not base any of your conditional logic on pods (individual pods) rather rely on deployments.请注意,在受控环境中,最好不要将任何条件逻辑建立在 Pod(单个 Pod)上,而是依赖于部署。 The REST endpoint which you should rather focus on is您应该关注的 REST 端点是
GET /apis/apps/v1beta2/namespaces/{namespace}/deployments/{name}/status
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.