【发布时间】:2019-08-09 17:52:59
【问题描述】:
我有一个使用就绪探测的 Kubernetes pod,并与服务绑定,这确保我在准备好之前不会收到流量。
我使用 Spring Actuator 作为这个就绪探测的健康端点。
但我想在 kubelet 认为 pod 准备就绪时触发一些操作。
最简单的方法是什么?
【问题讨论】:
-
也许实现您自己的 HealthCheck。当你第一次发现一切正常时,运行你的代码
我有一个使用就绪探测的 Kubernetes pod,并与服务绑定,这确保我在准备好之前不会收到流量。
我使用 Spring Actuator 作为这个就绪探测的健康端点。
但我想在 kubelet 认为 pod 准备就绪时触发一些操作。
最简单的方法是什么?
【问题讨论】:
您可以使用 helm 图表提供的安装后挂钩(如果您使用 helm 部署应用程序)。这将在 pod 启动并运行后执行必要的操作/脚本/作业。
【讨论】:
也许实现您自己的 HealthCheck。当您第一次发现一切正常时,运行您的代码。
我使用静态变量 firstHealthCheckOK 进行检查。您的逻辑应该只运行一次。
我假设您正在运行 Spring-boot 2.x 并在 http://localhost:8080/actuator/health 上调用就绪探测
Kubernetes调用http://localhost:8080/actuator/health时会调用下面的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
}
}
【讨论】:
作为 pod 生命周期事件的一部分,您可能希望附加额外的处理程序,例如 podStart,并构建您的自定义逻辑以根据需要操纵发生的事件。
或者,您也可以运行代码来读取 REST 响应
GET /api/v1/namespaces/{namespace}/pods/{name}/log
构建任何下游逻辑以获取 pod 状态
请注意,在受控环境中,最好不要将任何条件逻辑基于 pod(单个 pod),而是依赖于部署。您应该关注的 REST 端点是
GET /apis/apps/v1beta2/namespaces/{namespace}/deployments/{name}/status
【讨论】: