此版本仍在开发中,尚未被视为稳定版本。如需最新的稳定版本,请使用 Spring Cloud Kubernetes 3.1.4spring-doc.cadn.net.cn

Spring Cloud Kubernetes 配置观察器

Kubernetes 提供了将 ConfigMap 或 Secret 作为卷挂载到应用程序容器中的功能。当 ConfigMap 或 Secret 的内容发生更改时,挂载的卷将使用这些更改进行更新spring-doc.cadn.net.cn

但是,除非您重新启动应用程序,否则 Spring Boot 不会自动更新这些更改。Spring Cloud 提供刷新应用程序上下文的能力,而无需重新启动应用程序,方法是点击 执行器端点/refresh或通过发布RefreshRemoteApplicationEvent使用 Spring Cloud Bus。spring-doc.cadn.net.cn

要实现在 Kubernetes 上运行的 Spring Cloud 应用程序的配置刷新,您可以部署 Spring Cloud Kubernetes Configuration Watcher 控制器部署到 Kubernetes 集群中。spring-doc.cadn.net.cn

该应用程序作为容器发布,可在 Docker Hub 上使用。 但是,如果您需要自定义配置观察程序行为或更喜欢自己构建镜像,则可以轻松构建自己的镜像 图像,并使用它。spring-doc.cadn.net.cn

配置它的另一个选项是在用于部署配置观察器的 deployment.yaml 中提供一些环境变量。以下是一些重要的:spring-doc.cadn.net.cn

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

这些在 configuration watcher 上启用 debug logging,在初始设置中特别有用,以便能够诊断潜在的错误配置。spring-doc.cadn.net.cn

env:
  - name: SPRING_CLOUD_KUBERNETES_RELOAD_NAMESPACES_0
    value: "namespace-a"

这让 watcher 知道在哪里搜索 secret 和 configmap。您在这里有两个选项:选择性命名空间(上面的设置)和由 Namespace Resolution 选择的命名空间(这是默认选项)。 请记住,所有这些选项都需要适当的 RBAC 规则。spring-doc.cadn.net.cn

如果特定更改来自具有标签的来源,则来自 configmaps/secrets 的更改只会触发从配置观察器触发的事件:spring.cloud.kubernetes.config=truespring.cloud.kubernetes.secret=true.spring-doc.cadn.net.cn

简单来说,如果你更改了一个没有上述标签的 configmap(或 secret),配置观察器将跳过为其触发事件(如果你启用了调试日志记录,这将在日志中可见)。spring-doc.cadn.net.cn

默认情况下,配置观察器将监控已配置命名空间中的所有 configmap/secret。如果您想过滤以仅关注特定来源,您可以通过设置来实现:spring-doc.cadn.net.cn

SPRING_CLOUD_KUBERNETES_CONFIG_INFORMER_ENABLED=TRUE

这将告诉 watcher 只监控具有标签的源:spring.cloud.kubernetes.config.informer.enabled=true.spring-doc.cadn.net.cn

另一个更重要的配置,特别是对于作为卷挂载的 configmap 和 secret(通过spring.cloud.kubernetes.config.paths/spring.cloud.kubernetes.secrets.paths或使用spring.config.import) 是:spring-doc.cadn.net.cn

- name: SPRING_CLOUD_KUBERNETES_CONFIGURATION_WATCHER_REFRESHDELAY
  value: "10000"

这告诉我们在从 configuration watcher 触发事件之前我们应该等待多少毫秒。这很重要,因为 kubernetes 文档说:spring-doc.cadn.net.cn

当卷中当前使用的 ConfigMap 更新时,投影的 key 最终也会更新。spring-doc.cadn.net.cn

您需要将此最终部分与集群上的该值(以毫秒为单位)“匹配”。spring-doc.cadn.net.cn

Spring Cloud Kubernetes Configuration Watcher 可以通过两种方式向应用程序发送刷新通知。spring-doc.cadn.net.cn

  1. 通过 HTTP,在这种情况下,要通知的应用程序必须具有/refreshActuator 终端节点公开且可从集群内访问spring-doc.cadn.net.cn

  2. 使用 Spring Cloud Bus,在这种情况下,您需要将消息代理部署到您的客户中,以供应用程序使用。spring-doc.cadn.net.cn

部署 YAML

以下是可用于将 Kubernetes Configuration Watcher 部署到 Kubernetes 的示例部署 YAML。spring-doc.cadn.net.cn

---
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.2.0-RC1
            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 集群中的 ConfigMap、Pod、服务、端点和 Secrets 的数据。spring-doc.cadn.net.cn

监控 ConfigMap 和 Secret

如果对具有有效标签的 ConfigMap 或 Secret 进行了更改(如上所述),则 Spring Cloud Kubernetes Configuration Watcher 将采用 ConfigMap 或 Secret 的名称 ,然后使用该名称向应用程序发送通知。不过,这可能还不够,例如,您可能希望:spring-doc.cadn.net.cn

因此,您可以指定一个 additional annotation:spring-doc.cadn.net.cn

spring.cloud.kubernetes.configmap.appsspring.cloud.kubernetes.secret.apps.它需要一个用逗号分隔的 String 应用程序, 该 Config 指定当此 secret/configmap 中发生更改时将接收通知的应用程序的名称。spring-doc.cadn.net.cn

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 配置观察器和 更改为 ConfigMap 或 Secret 时,HTTP 实现将使用 Spring Cloud Kubernetes Discovery Client 来获取所有 与 ConfigMap 或 Secret 的名称匹配并向应用程序的 actuator 发送 HTTP POST 请求的实例/refresh端点。默认情况下,它会将 post 请求发送到/actuator/refresh使用在 Discovery Client 中注册的端口。spring-doc.cadn.net.cn

非默认管理端口和执行器路径

如果应用程序使用非默认 actuator 路径和/或对管理端点使用不同的端口,则应用程序的 Kubernetes 服务 可以添加一个名为boot.spring.io/actuator并将其值设置为应用程序使用的 path 和 port。例如spring-doc.cadn.net.cn

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

您可以选择配置 actuator path 和/或 management port 的另一种方法是设置spring.cloud.kubernetes.configuration.watcher.actuatorPathspring.cloud.kubernetes.configuration.watcher.actuatorPort.spring-doc.cadn.net.cn

消息传递实现

可以通过将 profile 设置为bus-amqp(RabbitMQ) 或bus-kafka(Kafka) 当 Spring Cloud Kubernetes 配置观察器 应用程序部署到 Kubernetes。spring-doc.cadn.net.cn

配置 RabbitMQ

bus-amqpprofile 启用后,您需要配置 Spring RabbitMQ 以将其指向 RabbitMQ 的位置 实例以及进行身份验证所需的任何凭证。这是可以做到的 通过设置标准的 Spring RabbitMQ 属性,例如spring-doc.cadn.net.cn

spring:
  rabbitmq:
    username: user
    password: password
    host: rabbitmq

配置 Kafka

bus-kafkaprofile 启用后,您需要配置 Spring Kafka 以将其指向 Kafka Broker 的位置 实例。这可以通过设置标准的 Spring Kafka 属性来完成,例如spring-doc.cadn.net.cn

spring:
  kafka:
    producer:
      bootstrap-servers: localhost:9092