Kubernetes Ecosystem Awareness
本指南中前面所描述的所有功能同样有效,而无论您的应用程序在 Kubernetes 中是否正在运行。这对于开发和故障排除非常有帮助。从开发角度来看,这使您可以启动 Spring Boot 应用程序并调试属于该项目的一部分模块之一。您不必在 Kubernetes 中部署它,因为项目的代码依赖于 Fabric8 Kubernetes Java client,而这是一个流畅的 DSL,可以使用 http
协议与 Kubernetes 服务器的 REST API 通信。
All features described earlier in this guide work equally well, regardless of whether your application is running inside
Kubernetes. This is really helpful for development and troubleshooting.
From a development point of view, this lets you start your Spring Boot application and debug one
of the modules that is part of this project. You need not deploy it in Kubernetes,
as the code of the project relies on the
Fabric8 Kubernetes Java client, which is a fluent DSL that can
communicate by using http
protocol to the REST API of the Kubernetes Server.
Kubernetes 感知基于 Spring Boot API,具体而言,基于 ConditionalOnCloudPlatform。该属性将自动检测您的应用程序当前是否部署在 kubernetes 中。可以完全通过 spring.main.cloud-platform
覆盖该设置。
Kubernetes awareness is based on Spring Boot API, specifically on ConditionalOnCloudPlatform.
That property will auto-detect if your application is currently deployed in kubernetes or not. It is possible to override
that setting via spring.main.cloud-platform
.
例如,如果你需要测试某些功能,但不想部署到集群,那么设置 spring.main.cloud-platform=KUBERNETES
就可以了。这将使 spring-cloud-kubernetes
表现得像是在一个真实的集群中部署一样。
For example, if you need to test some features, but do not want to deploy to a cluster, it is enough to set the:
spring.main.cloud-platform=KUBERNETES
. This will make spring-cloud-kubernetes
act as-if it is deployed in a real cluster.
如果你在类路径设置了`spring-cloud-starter-bootstrap`或正在设置`spring.cloud.bootstrap.enabled=true`,那么你需要设置`spring.main.cloud-platform`,它应该在`bootstrap.{properties|yml}`中设置(或特定于配置文件的设置)。还要注意,这些属性:`spring.cloud.kubernetes.config.enabled`和`spring.cloud.kubernetes.secrets.enabled`只有在类路径中设置了`spring-cloud-starter-bootstrap`或正在设置`spring.cloud.bootstrap.enabled=true`的时候才会生效。 |
If you have |
Breaking Changes In 3.0.x
在 3.0.x
之前的 Spring Cloud Kubernetes 版本中,Kubernetes 感知是使用 spring.cloud.kubernetes.enabled
属性实现的。此属性已删除,不再受支持。我们改为使用 Spring Boot API: ConditionalOnCloudPlatform。如果需要明确启用或禁用此感知,请使用 spring.main.cloud-platform=NONE/KUBERNETES
。
In versions of Spring Cloud Kubernetes prior to 3.0.x
, Kubernetes awareness was implemented using spring.cloud.kubernetes.enabled
property. This
property was removed and is un-supported. Instead, we use Spring Boot API: ConditionalOnCloudPlatform.
If it is needed to explicitly enable or disable this awareness, use spring.main.cloud-platform=NONE/KUBERNETES
.
-
Another breaking change is the additional
list
verb needed for loading configmaps/secrets. For example:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cluster-role
rules:
- apiGroups: ["", "extensions", "apps", "discovery.k8s.io"]
resources: ["configmaps", "pods", "services", "endpoints", "secrets", "endpointslices"]
verbs: ["get", "list", "watch"]
Kubernetes Profile Autoconfiguration
当应用程序在 Kubernetes 中作为 pod 运行时,会自动激活名为 kubernetes
的 Spring 配置。这允许自定义配置、定义在 Spring Boot 应用程序在 Kubernetes 平台内部署时应用的 bean(例如,不同的开发和生产配置)。
When the application runs as a pod inside Kubernetes, a Spring profile named kubernetes
automatically gets activated.
This lets you customize the configuration, to define beans that are applied when the Spring Boot application is deployed
within the Kubernetes platform (for example, different development and production configuration).
Istio Awareness
当您在应用程序类路径中包含 spring-cloud-kubernetes-fabric8-istio
模块时,会向应用程序添加一个新配置文件,前提是在安装了 Istio 的 Kubernetes 集群内运行应用程序。然后,您可以在 Bean 和 @Configuration
类中使用 Spring @Profile("istio")
注释。
When you include the spring-cloud-kubernetes-fabric8-istio
module in the application classpath, a new profile is added to the application,
provided the application is running inside a Kubernetes Cluster with Istio installed. You can then use
spring @Profile("istio")
annotations in your Beans and @Configuration
classes.
Istio 感知模块使用 me.snowdrop:istio-client
与 Istio API 交互,让我们发现流量规则、断路器等,这样我们的 Spring Boot 应用程序可以很容易地使用这些数据根据环境动态配置自己。
The Istio awareness module uses me.snowdrop:istio-client
to interact with Istio APIs, letting us discover traffic rules, circuit breakers, and so on,
making it easy for our Spring Boot applications to consume this data to dynamically configure themselves according to the environment.