|
此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Cloud Kubernetes 3.3.0! |
Spring Cloud Kubernetes 配置观察器
Kubernetes 提供了将 ConfigMap 或 Secret 作为卷挂载到应用程序容器中的功能。当 ConfigMap 或 Secret 的内容发生更改时,挂载的卷将使用这些更改进行更新。
但是,除非您重新启动应用程序,否则 Spring Boot 不会自动更新这些更改。Spring Cloud
提供了刷新应用程序上下文而无需重新启动应用程序的功能,方法是点击
执行器端点/refresh或通过发布RefreshRemoteApplicationEvent使用 Spring Cloud Bus。
要实现在 Kubernetes 上运行的 Spring Cloud 应用程序的配置刷新,您可以部署 Spring Cloud Kubernetes 配置观察程序控制器添加到 Kubernetes 集群中。
该应用程序作为容器发布,可在 Docker Hub 上使用。 但是,如果您需要自定义配置观察器行为或更喜欢自己构建映像,则可以轻松构建自己的映像 图像来自 GitHub 上的源代码并使用它。
配置它的另一个选项是在用于部署配置观察程序的 deployment.yaml 中提供一些环境变量。以下是一些重要的:
env:
- name: LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CONFIGURATION_WATCHER
value: DEBUG
- name: LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD
value: DEBUG
- name: LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG_RELOAD
value: DEBUG
这些支持配置观察程序上的调试日志记录,并且在初始设置中特别有用,以便能够诊断潜在的错误配置。
env:
- name: SPRING_CLOUD_KUBERNETES_RELOAD_NAMESPACES_0
value: "namespace-a"
这让观察者知道在哪里搜索机密和配置映射。这里有两个选项:选择性命名空间(上述设置)和命名空间解析选择的命名空间(这是默认选项)。 请记住,所有这些选项都需要适当的 RBAC 规则。
如果特定更改来自具有标签的源,则来自 configmaps/secrets 的更改只会触发从配置观察器触发的事件:spring.cloud.kubernetes.config=true或spring.cloud.kubernetes.secret=true.
简单来说,如果您更改没有上述标签的 configmap(或 secret),则配置观察器将跳过为其触发事件(如果您启用了调试日志记录,这将在日志中可见)。
默认情况下,配置观察程序将监视配置命名空间中的所有配置映射/机密。如果您想过滤以仅观看特定来源,您可以通过设置以下内容来实现:
SPRING_CLOUD_KUBERNETES_CONFIG_INFORMER_ENABLED=TRUE
这将告诉观察程序仅监视具有标签的源:spring.cloud.kubernetes.config.informer.enabled=true.
一个更重要的配置,特别是对于作为卷挂载的配置映射和密钥(通过spring.cloud.kubernetes.config.paths/spring.cloud.kubernetes.secrets.paths或使用spring.config.import) 是:
- name: SPRING_CLOUD_KUBERNETES_CONFIGURATION_WATCHER_REFRESHDELAY
value: "10000"
这说明了在从配置观察程序触发事件之前应该等待多少毫秒。这很重要,因为 kubernetes 文档说:
当卷中当前使用的 ConfigMap 被更新时,投影的键最终也会更新。
您需要将此最终部分与集群上的该值(以毫秒为单位)“匹配”。
Spring Cloud Kubernetes Configuration Watcher 可以通过两种方式向应用程序发送刷新通知。
-
通过 HTTP,在这种情况下,收到通知的应用程序必须具有
/refresh执行器端点公开并可从集群内访问 -
使用 Spring Cloud Bus,在这种情况下,您需要将消息代理部署到您的 custer 上,以便应用程序使用。
部署 YAML
下面是一个示例部署 YAML,可用于将 Kubernetes 配置观察程序部署到 Kubernetes。
---
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Service
metadata:
labels:
app: spring-cloud-kubernetes-configuration-watcher
name: spring-cloud-kubernetes-configuration-watcher
spec:
ports:
- name: http
port: 8888
targetPort: 8888
selector:
app: spring-cloud-kubernetes-configuration-watcher
type: ClusterIP
- apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app: spring-cloud-kubernetes-configuration-watcher
name: spring-cloud-kubernetes-configuration-watcher
- apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
app: spring-cloud-kubernetes-configuration-watcher
name: spring-cloud-kubernetes-configuration-watcher:view
roleRef:
kind: Role
apiGroup: rbac.authorization.k8s.io
name: namespace-reader
subjects:
- kind: ServiceAccount
name: spring-cloud-kubernetes-configuration-watcher
- apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: namespace-reader
rules:
- apiGroups: ["", "extensions", "apps"]
resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
verbs: ["get", "list", "watch"]
- apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-cloud-kubernetes-configuration-watcher-deployment
spec:
selector:
matchLabels:
app: spring-cloud-kubernetes-configuration-watcher
template:
metadata:
labels:
app: spring-cloud-kubernetes-configuration-watcher
spec:
serviceAccount: spring-cloud-kubernetes-configuration-watcher
containers:
- name: spring-cloud-kubernetes-configuration-watcher
image: springcloud/spring-cloud-kubernetes-configuration-watcher:3.1.7-SNAPSHOT
imagePullPolicy: IfNotPresent
readinessProbe:
httpGet:
port: 8888
path: /actuator/health/readiness
livenessProbe:
httpGet:
port: 8888
path: /actuator/health/liveness
ports:
- containerPort: 8888
服务帐户和相关的角色绑定对于 Spring Cloud Kubernetes 配置的正常工作非常重要。 控制器需要访问权限才能读取有关 Kubernetes 集群中的 ConfigMaps、Pod、服务、端点和密钥的数据。
监控 ConfigMap 和密钥
如果对具有有效标签的 ConfigMap 或 Secret 进行了更改(如上所述),则 Spring Cloud Kubernetes Configuration Watcher 将采用 ConfigMap 或 Secret 的名称 并使用该名称向应用程序发送通知。不过,这对于您的用例来说可能还不够,例如,您可能想要:
-
将 config-map 绑定到多个应用程序,以便单个 configmap 中的更改会触发许多服务的刷新
-
让基于配置文件的源触发应用程序的事件
出于这个原因,您可以指定一个附加注释:
spring.cloud.kubernetes.configmap.apps或spring.cloud.kubernetes.secret.apps.它需要一串用逗号分隔的应用程序,
指定在此 secret/configmap 中发生更改时将收到通知的应用程序的名称。
例如:
kind: ConfigMap
apiVersion: v1
metadata:
name: example-configmap
labels:
spring.cloud.kubernetes.config: "true"
annotations:
spring.cloud.kubernetes.configmap.apps: "app-a, app-b"
HTTP 实现
默认情况下使用的 HTTP 实现。使用此实现时,Spring Cloud Kubernetes Configuration Watcher 和
更改为 ConfigMap 或 Secret 时,HTTP 实现将使用 Spring Cloud Kubernetes Discovery Client 来获取所有
与 ConfigMap 或 Secret 的名称匹配的应用程序实例,并向应用程序的执行器发送 HTTP POST 请求/refresh端点。默认情况下,它会将发布请求发送到/actuator/refresh使用在发现客户端中注册的端口。
非默认管理端口和执行器路径
如果应用程序使用非默认执行器路径和/或对管理端点使用不同的端口,则应用程序的 Kubernetes 服务
可以添加一个名为boot.spring.io/actuator并将其值设置为应用程序使用的路径和端口。例如
apiVersion: v1
kind: Service
metadata:
labels:
app: config-map-demo
name: config-map-demo
annotations:
boot.spring.io/actuator: http://:9090/myactuator/home
spec:
ports:
- name: http
port: 8080
targetPort: 8080
selector:
app: config-map-demo
您可以选择配置执行器路径和/或管理端口的另一种方法是将spring.cloud.kubernetes.configuration.watcher.actuatorPath和spring.cloud.kubernetes.configuration.watcher.actuatorPort.
消息传递实现
可以通过将配置文件设置为bus-amqp(RabbitMQ) 或bus-kafka(Kafka) 当 Spring Cloud Kubernetes 配置观察器
应用程序被部署到 Kubernetes。
配置 RabbitMQ
当bus-amqp配置文件,则需要配置 Spring RabbitMQ 以将其指向 RabbitMQ 的位置
实例以及身份验证所需的任何凭据。这是可以做到的
通过设置标准 Spring RabbitMQ 属性,例如
spring:
rabbitmq:
username: user
password: password
host: rabbitmq
配置 Kafka
当bus-kafka配置文件,则需要配置 Spring Kafka 以将其指向 Kafka 代理的位置
实例。这可以通过设置标准 Spring Kafka 属性来完成,例如
spring:
kafka:
producer:
bootstrap-servers: localhost:9092