1. Overview
1.概述
In this tutorial, we’re going to see how Spring Boot 2.3 integrates with Kubernetes probes to create an even more pleasant cloud-native experience.
在本教程中,我们将看到Spring Boot 2.3如何与Kubernetes探针集成,以创造更加令人愉悦的云原生体验。
First, we’ll start with a little bit of a background on Kubernetes probes. Then we’ll switch gears and see how Spring Boot 2.3 supports those probes.
首先,我们先介绍一下Kubernetes探针的背景。然后我们再换个角度,看看Spring Boot 2.3如何支持这些探针。
2. Kubernetes Probes
2.Kubernetes探针
When using Kubernetes as our orchestration platform, the kubelet in each node is responsible for keeping the pods in that node healthy.
当使用Kubernetes作为我们的协调平台时,每个节点中的kubelet负责保持该节点中pod的健康。
For instance, sometimes our apps may need a little bit of time before being able to accept requests. The kubelet can make sure that the application receives requests only when it’s ready. Also, if the main process of a pod crashes for any reason, the kubelet will restart the container.
例如,有时我们的应用程序可能需要一点时间才能接受请求。kubelet可以确保应用程序只有在准备好的时候才能接收请求。另外,如果一个pod的主进程由于任何原因崩溃了,kubelet将重新启动容器。
In order to fulfill these responsibilities, Kubernetes has two probes: liveness probes and readiness probes.
为了履行这些职责,Kubernetes有两个探针:有效性探针和准备性探针。
The kubelet will use the readiness probe to determine when the application is ready to accept requests. More specifically, a pod is ready when all of its containers are ready.
kubelet将使用准备度探针来确定应用程序何时准备好接受请求。更具体地说,当一个pod的所有容器都准备好时,它就准备好了。
Similarly, the kubelet can check if a pod is still alive through liveness probes. Basically, the liveness probe helps the kubelet know when it should restart a container.
同样地,kubelet可以通过有效性探针检查一个pod是否仍然活着。基本上,有效性探针可以帮助kubelet知道何时应该重新启动一个容器。
Now that we are familiar with the concepts, let’s see how the Spring Boot integration works.
现在我们已经熟悉了这些概念,让我们看看Spring Boot集成是如何工作的。
3. Liveness and Readiness in Actuator
3.执行器的有效性和准备性
As of Spring Boot 2.3, LivenessStateHealthIndicator and ReadinessStateHealthIndicator classes will expose the liveness and readiness state of the application. When we deploy our application to Kubernetes, Spring Boot will automatically register these health indicators.
从Spring Boot 2.3开始,LivenessStateHealthIndicator和ReadinessStateHealthIndicator类将暴露应用程序的liveness和ready状态。当我们将应用程序部署到Kubernetes时,Spring Boot将自动注册这些健康指标。
As a result, we can use /actuator/health/liveness and /actuator/health/readiness endpoints as our liveness and readiness probes, respectively.
因此,我们可以使用/actuator/health/liveness 和/actuator/health/readiness 端点分别作为我们的liveness和readiness探针。
For instance, we can add these to our pod definition to configure the liveness probe as an HTTP GET request:
例如,我们可以将这些添加到我们的pod定义中,以将liveness探针配置为一个HTTP GET请求。
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
We’ll usually let Spring Boot decide when to stand up these probes for us. But, if we want to, we can enable them manually in our application.properties.
我们通常会让Spring Boot决定何时为我们建立这些探针。但是,如果我们愿意,我们可以在application.properties.中手动启用它们。
If we’re working with Spring Boot 2.3.0 or 2.3.1, we can enable the mentioned probes through a configuration property:
如果我们使用的是Spring Boot 2.3.0或2.3.1,我们可以通过配置属性启用上述探针。
management.health.probes.enabled=true
However, since Spring Boot 2.3.2, this property is deprecated due to configuration confusion.
然而,自Spring Boot 2.3.2以来,由于配置混乱,这个属性被废弃了。
If we work with Spring Boot 2.3.2, we can use the new properties to enable liveness and readiness probes:
如果我们使用Spring Boot 2.3.2,我们可以使用新的属性来启用有效性和准备性探测。
management.endpoint.health.probes.enabled=true
management.health.livenessState.enabled=true
management.health.readinessState.enabled=true
3.1. Readiness and Liveness State Transitions
3.1.准备就绪和活泼状态的转换
Spring Boot uses two enums to encapsulate different readiness and liveness states. For readiness state, there is an enum called ReadinessState with the following values:
Spring Boot使用两个枚举来封装不同的准备状态和活泼状态。对于准备状态,有一个名为ReadinessState的枚举,其值如下。
- The ACCEPTING_TRAFFIC state represents that the application is ready to accept traffic
- The REFUSING_TRAFFIC state means that the application is not willing to accept any requests yet
Similarly, the LivenessState enum represents the liveness state of the app with two values:
同样,LivenessState枚举用两个值表示应用程序的有效性状态。
- The CORRECT value means the application is running and its internal state is correct
- On the other hand, the BROKEN value means the application is running with some fatal failures
Here’s how readiness and liveness state changes in terms of application lifecycle events in Spring:
下面是在Spring中应用生命周期事件方面的准备度和活泼性状态的变化。
- Registering listeners and initializers
- Preparing the Environment
- Preparing the ApplicationContext
- Loading bean definitions
- Changing the liveness state to CORRECT
- Calling the application and command-line runners
- Changing the readiness state to ACCEPTING_TRAFFIC
Once the application is up and running, we (and Spring itself) can change these states by publishing appropriate AvailabilityChangeEvents.
一旦应用程序启动并运行,我们(和Spring本身)可以通过发布适当的AvailabilityChangeEvents来改变这些状态。
4. Managing the Application Availability
4.管理应用程序的可用性
Application components can retrieve the current readiness and liveness state by injecting the ApplicationAvailability interface:
应用程序组件可以通过注入ApplicationAvailability接口来检索当前的准备状态和活泼状态。
@Autowired private ApplicationAvailability applicationAvailability;
Then we can use it as follows:
然后我们可以按以下方式使用它。
assertThat(applicationAvailability.getLivenessState())
.isEqualTo(LivenessState.CORRECT);
assertThat(applicationAvailability.getReadinessState())
.isEqualTo(ReadinessState.ACCEPTING_TRAFFIC);
assertThat(applicationAvailability.getState(ReadinessState.class))
.isEqualTo(ReadinessState.ACCEPTING_TRAFFIC);
4.1. Updating the Availability State
4.1.更新可用性状态
We can also update the application state by publishing an AvailabilityChangeEvent event:
我们也可以通过发布一个AvailabilityChangeEvent事件来更新应用程序的状态。
assertThat(applicationAvailability.getLivenessState())
.isEqualTo(LivenessState.CORRECT);
mockMvc.perform(get("/actuator/health/liveness"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("UP"));
AvailabilityChangeEvent.publish(context, LivenessState.BROKEN);
assertThat(applicationAvailability.getLivenessState())
.isEqualTo(LivenessState.BROKEN);
mockMvc.perform(get("/actuator/health/liveness"))
.andExpect(status().isServiceUnavailable())
.andExpect(jsonPath("$.status").value("DOWN"));
As shown above, before publishing any event, the /actuator/health/liveness endpoint returns a 200 OK response with the following JSON:
如上所示,在发布任何事件之前,/actuator/health/liveness 端点会返回一个200 OK的响应,其中包含以下JSON。
{
"status": "OK"
}
Then after breaking the liveness state, the same endpoint returns a 503 service unavailable response with the following JSON:
然后,在打破有效性状态后,同一个端点返回一个503服务不可用的响应,其JSON如下。
{
"status": "DOWN"
}
When we change to a readiness state of REFUSING_TRAFFIC, the status value will be OUT_OF_SERVICE:
当我们改变为REFUSING_TRAFFIC的准备状态时,status值将是OUT_OF_SERVICE:。
assertThat(applicationAvailability.getReadinessState())
.isEqualTo(ReadinessState.ACCEPTING_TRAFFIC);
mockMvc.perform(get("/actuator/health/readiness"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("UP"));
AvailabilityChangeEvent.publish(context, ReadinessState.REFUSING_TRAFFIC);
assertThat(applicationAvailability.getReadinessState())
.isEqualTo(ReadinessState.REFUSING_TRAFFIC);
mockMvc.perform(get("/actuator/health/readiness"))
.andExpect(status().isServiceUnavailable())
.andExpect(jsonPath("$.status").value("OUT_OF_SERVICE"));
4.2. Listening to a Change
4.2.倾听变化
We can register event listeners to be notified when an application availability state changes:
我们可以注册事件监听器,以便在应用程序可用性状态改变时得到通知。
@Component
public class LivenessEventListener {
@EventListener
public void onEvent(AvailabilityChangeEvent<LivenessState> event) {
switch (event.getState()) {
case BROKEN:
// notify others
break;
case CORRECT:
// we're back
}
}
}
Here we’re listening to any change in application liveness state.
在这里,我们要监听应用程序有效性状态的任何变化。
5. Auto-Configurations
5.自动配置
Before wrapping up, let’s see how Spring Boot automatically configures these probes in Kubernetes deployments. The AvailabilityProbesAutoConfiguration class is responsible for registering the liveness and readiness probes conditionally.
在结束之前,让我们看看Spring Boot如何在Kubernetes部署中自动配置这些探针。AvailabilityProbesAutoConfiguration类负责有条件地注册liveness和ready probes。
As a matter of fact, there is a special condition there that registers the probes when one of the following is true:
事实上,那里有一个特殊的条件,当以下情况之一是真时,会注册探针:。
- Kubernetes is the deployment environment
- The management.health.probes.enabled property is set to true
When an application meets either of these conditions, the auto-configuration registers beans of LivenessStateHealthIndicator and ReadinessStateHealthIndicator.
当一个应用程序满足这些条件时,自动配置会注册LivenessStateHealthIndicator 和ReadinessStateHealthIndicator的Bean。
6. Conclusion
6.结语
In this article, we saw how we can use Spring Boot provides two health probes for Kubernetes integration.
在这篇文章中,我们看到了如何使用Spring Boot提供的两个健康探针进行Kubernetes集成。
As usual, all the examples are available over on GitHub.
像往常一样,所有的例子都可以在GitHub上找到。