简体   繁体   English

在 Kubernetes 中,如何检测我的 pod 是否准备就绪?

[英]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.

相关问题 Spring Cloud Kube.netes 应用程序未检测到它何时在 pod 中运行 - Spring Cloud Kubernetes application does not detect when it is running in a pod 如何在Kubernetes Java应用程序中获取当前Pod - How to Get Current Pod in Kubernetes Java Application 如何在具有运行中的spring-boot应用程序的kubernetes容器/容器中运行cron? - How do I run a cron inside a kubernetes pod/container which has a running spring-boot application? 如何为 Kubernetes 服务启用 SSL? - How can I enable SSL for Kubernetes services? 无法访问 Kubernetes pod - Cannot access Kubernetes pod 如何在每个 pod 中获取所有 Kube.netes pod IP? - How to get all Kubernetes pod IP in each pods? 到达 Eureka Server pod Kubernetes - Reaching Eureka Server pod Kubernetes 如何配置 Redis Kubernetes 部署以使从机 redis 在主服务器宕机时接管 pod? - How to configure Redis Kubernetes deployment to make slave redis pod takeover when master is down? 如何使用 Spring Boot 将来自 Google Secret Manager 的秘密作为环境变量注入 Kubernetes Pod? - How to inject secret from Google Secret Manager into Kubernetes Pod as environment variable with Spring Boot? 由于日志中的 Rabbit MQ 连接问题,如何重新启动 kubernetes pod - How to restart kubernetes pod when issue because of Rabbit MQ connectivity in logs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM