Kubernetes 生态系统意识

本指南前面描述的所有功能都同样有效,无论您的应用程序是否在 Kubernetes。这对于开发和故障排除非常有帮助。 从开发的角度来看,这允许您启动 Spring Boot 应用程序并调试一个 作为该项目一部分的模块。你不需要在 Kubernetes 中部署它, 因为项目的代码依赖于 Fabric8 Kubernetes Java 客户端,这是一个流畅的 DSL,可以 通过使用http协议到 Kubernetes 服务器的 REST API。spring-doc.cadn.net.cn

Kubernetes 感知基于 Spring Boot API,特别是 ConditionalOnCloudPlatform。 该属性将自动检测您的应用程序当前是否部署在 Kubernetes 中。可以覆盖 该设置通过spring.main.cloud-platform.spring-doc.cadn.net.cn

例如,如果您需要测试某些功能,但不想部署到集群,则只需设置:spring.main.cloud-platform=KUBERNETES.这将使spring-cloud-kubernetes就像部署在真实集群中一样。spring-doc.cadn.net.cn

如果您有spring-cloud-starter-bootstrap在类路径上或正在设置spring.cloud.bootstrap.enabled=true然后 您将不得不设置spring.main.cloud-platform应设置在bootstrap.{properties|yml}(或特定于配置文件的配置文件)。另请注意,这些属性:spring.cloud.kubernetes.config.enabledspring.cloud.kubernetes.secrets.enabled只有在设置bootstrap.{properties|yml}当您有spring-cloud-starter-bootstrap在类路径上或正在设置spring.cloud.bootstrap.enabled=true.

3.0.x 中的重大更改

3.0.x,Kubernetes 感知是使用spring.cloud.kubernetes.enabled财产。这 属性已被删除并且不受支持。相反,我们使用 Spring Boot API:ConditionalOnCloudPlatform。 如果需要显式启用或禁用此感知,请使用spring.main.cloud-platform=NONE/KUBERNETES.spring-doc.cadn.net.cn

  • 另一个重大变化是额外的list加载 configmaps/secrets 所需的动词。例如:spring-doc.cadn.net.cn

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 配置文件自动配置

当应用程序在 Kubernetes 中作为 Pod 运行时,名为kubernetes自动激活。 这允许您自定义配置,以定义部署 Spring Boot 应用程序时应用的 Bean 在 Kubernetes 平台中(例如,不同的开发和生产配置)。spring-doc.cadn.net.cn

Istio 意识

当您包含spring-cloud-kubernetes-fabric8-istio模块,将向应用程序添加一个新的配置文件, 前提是应用程序在安装了 Istio 的 Kubernetes 集群中运行。然后,您可以使用 Spring@Profile("istio")Commentations 和@Configuration类。spring-doc.cadn.net.cn

Istio 感知模块使用me.snowdrop:istio-client与 Istio API 交互,让我们发现流量规则、熔断器等, 使我们的 Spring Boot 应用程序可以轻松地使用这些数据来根据环境动态配置自己。spring-doc.cadn.net.cn