9.2. 为 ConfigMap 属性源运行 Tutorial


以下教程允许您试验设置 Kubernetes Secret 和 ConfigMap。按照 Enabling Spring Boot with Kubernetes Integration 所述,启用 Spring Cloud Kubernetes 插件,以将 Kubernetes 配置对象与 Spring Boot Externalized Configuration 集成。

9.2.1. 运行 Spring Boot Camel Config Quickstart

以下教程基于 spring-boot-camel-config-archetype Maven archetype,它可让您设置 Kubernetes Secret 和 ConfigMap。

流程

  1. 打开新的 shell 提示符,再输入以下 Maven 命令来创建简单的 Camel Spring Boot 项目。

    mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate \
      -DarchetypeCatalog=https://maven.repository.redhat.com/ga/io/fabric8/archetypes/archetypes-catalog/2.2.0.fuse-740017-redhat-00003/archetypes-catalog-2.2.0.fuse-740017-redhat-00003-archetype-catalog.xml \
      -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \
      -DarchetypeArtifactId=spring-boot-camel-config-archetype \
      -DarchetypeVersion=2.2.0.fuse-740017-redhat-00003
    Copy to Clipboard Toggle word wrap

    archetype 插件切换到交互模式以提示您输入剩余的字段:

    Define value for property 'groupId': : org.example.fis
    Define value for property 'artifactId': : fuse74-configmap
    Define value for property 'version':  1.0-SNAPSHOT: :
    Define value for property 'package':  org.example.fis: :
    [INFO] Using property: spring-boot-version = 1.5.17.RELEASE
    Confirm properties configuration:
    groupId: org.example.fis
    artifactId: fuse74-configmap
    version: 1.0-SNAPSHOT
    package: org.example.fis
    spring-boot-version: 1.5.17.RELEASE
     Y: :
    Copy to Clipboard Toggle word wrap

    出现提示时,为 groupId 值输入 org.example.fis,为 artifactId 值输入 fuse74-configmap。接受剩余的字段的默认值。

  2. 登录 OpenShift 并切换到您要部署应用的 OpenShift 项目。例如,若要以 developer 用户身份登录并部署到 test 项目,请输入以下命令:

    oc login -u developer -p developer
    oc project test
    Copy to Clipboard Toggle word wrap
  3. 在命令行中,更改到 new fuse74-configmap 项目的目录,再为此应用创建 Secret 对象。

    oc create -f sample-secret.yml
    Copy to Clipboard Toggle word wrap
    注意

    在部署应用程序前,需要创建 Secret 对象,否则部署的容器会进入等待状态,直到 Secret 变为可用。如果您随后创建 Secret,则容器将退出等待状态。有关如何设置 Secret 对象的更多信息,请参阅设置 Secret

  4. 构建和部署快速入门应用程序。从 fuse74-configmap 项目的顶层,输入:

    mvn fabric8:deploy -Popenshift
    Copy to Clipboard Toggle word wrap
  5. 按如下所示查看应用日志:

    1. 在浏览器中打开 OpenShift 控制台,再选择相关的项目命名空间(如 test)。
    2. 单击 fuse74-configmap 服务的 circular pod 图标的中心。
    3. Pod 视图中,点 pod Name 查看正在运行的 Pod 的详细信息(如果只有一个 pod 正在运行),您将直接转至详细信息页面。
    4. 单击 Logs 标签,以查看应用程序日志并向下滚动,以查找由 Camel 应用程序生成的日志消息。
  6. 默认接收者列表(在 src/main/resources/application.properties 中配置)将生成的消息发送到两个 dummy 端点: direct:async-queuedirect:file。这会导致类似以下内容的信息写入应用程序日志中:

    5:44:57.376 [Camel (camel) thread #0 - timer://order] INFO  generate-order-route - Generating message message-44, sending to the recipient list
    15:44:57.378 [Camel (camel) thread #0 - timer://order] INFO  target-route-queue - ----> message-44 pushed to an async queue (simulation)
    15:44:57.379 [Camel (camel) thread #0 - timer://order] INFO  target-route-queue - ----> Using username 'myuser' for the async queue
    15:44:57.380 [Camel (camel) thread #0 - timer://order] INFO  target-route--file - ----> message-44 written to a file
    Copy to Clipboard Toggle word wrap
  7. 在使用 ConfigMap 对象更新 fuse74-configmap 应用程序配置前,您必须提供 fuse74-configmap 应用程序权限来查看 OpenShift ApiServer 的数据。输入以下命令为 fuse74-configmap 应用程序的服务帐户授予 view 权限:

    oc policy add-role-to-user view system:serviceaccount:test:qs-camel-config
    Copy to Clipboard Toggle word wrap
    注意

    服务帐户通过语法 system:serviceaccount:PROJECT_NAME:SERVICE_ACCOUNT_NAME 来指定。fis-config 部署描述符将 SERVICE_ACCOUNT_NAME 定义为 qs-camel-config

  8. 要查看操作中的实时重新加载功能,请按如下所示创建一个 ConfigMap 对象:

    oc create -f sample-configmap.yml
    Copy to Clipboard Toggle word wrap

    新的 ConfigMap 覆盖正在运行的应用程序中 Camel 路由的接收者列表,将其配置为将生成的消息发送到 三个 dummy 端点: direct:async-queuedirect:file,以及 direct:mail。如需有关 ConfigMap 对象的更多信息,请参阅设置 ConfigMap。这会导致类似以下内容的信息写入应用程序日志中:

    16:25:24.121 [Camel (camel) thread #0 - timer://order] INFO  generate-order-route - Generating message message-9, sending to the recipient list
    16:25:24.124 [Camel (camel) thread #0 - timer://order] INFO  target-route-queue - ----> message-9 pushed to an async queue (simulation)
    16:25:24.125 [Camel (camel) thread #0 - timer://order] INFO  target-route-queue - ----> Using username 'myuser' for the async queue
    16:25:24.125 [Camel (camel) thread #0 - timer://order] INFO  target-route--file - ----> message-9 written to a file (simulation)
    16:25:24.126 [Camel (camel) thread #0 - timer://order] INFO  target-route--mail - ----> message-9 sent via mail
    Copy to Clipboard Toggle word wrap

9.2.2. 配置属性 Bean

配置属性 bean 是可通过注入接收配置设置的常规 Java Bean。它提供了 Java 代码和外部配置机制之间的基本接口。

外部化配置和 Bean Registry

下图显示了 Spring Boot Externalized Configuration 如何在 spring-boot-camel-config quickstart 中工作。

kube spring boot 01

配置机制有以下主要部分:

属性源
提供注入配置的属性设置。默认属性源是应用程序的 application.properties 文件,这可以选择被 ConfigMap 对象或 Secret 对象覆盖。
配置属性 Bean
从属性源接收 configuraton 更新。配置属性 bean 是 @Configuration@ConfigurationProperties 注释的 Java Bean。
Spring bean registry
使用 requisite 注解时,在 Spring bean registry 中注册了一个配置属性 Bean。
与 Camel bean registry 集成
Camel bean registry 会自动与 Spring bean registry 集成,以便在 Camel 路由中引用注册的 Spring Bean。

QuickstartConfiguration 类

fuse74-configmap 项目的配置属性 bean 定义为 QuickstartConfiguration Java 类(在 src/main/java/org/example/fis/ 目录下),如下所示:

package org.example.fis;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration  
1

@ConfigurationProperties(prefix = "quickstart")  
2

public class QuickstartConfiguration {

    /**
     * A comma-separated list of routes to use as recipients for messages.
     */
    private String recipients;  
3


    /**
     * The username to use when connecting to the async queue (simulation)
     */
    private String queueUsername;  
4


    /**
     * The password to use when connecting to the async queue (simulation)
     */
    private String queuePassword;  
5


    // Setters and Getters for Bean properties
    // NOT SHOWN
    ...
}
Copy to Clipboard Toggle word wrap
1
@Configuration 注释会导致 QuickstartConfiguration 类在 Spring 中实例化和注册,作为 ID 为 quickstartConfiguration 的 bean。这会自动使 bean 可从 Camel 访问。例如,target-route-queue 路由可以使用 Camel 语法 ${bean:quickstartConfiguration?method=getQueueUsername} 访问 queueUserName 属性。
2
@ConfigurationProperties 注释定义一个前缀 Quickstart,该前缀在属性源中定义属性值时必须使用。例如,属性文件将 recipients 属性引用为 quickstart.recipients
3
recipient 属性可从属性源注入。
4
queueUsername 属性可从属性源注入。
5
queuePassword 属性可从属性源注入。

9.2.3. 设置 Secret

此快速入门中的 Kubernetes Secret 以标准方式设置,除了一个额外步骤外:Spring Cloud Kubernetes 插件必须使用 Secret 的挂载路径进行配置,以便在运行时读取 Secret。设置 Secret:

  1. 创建 Sample Secret 对象
  2. 为 Secret 配置卷挂载
  3. 将 spring-cloud-kubernetes 配置为读取 Secret 属性

Secret 对象示例

Quickstart 项目提供了一个示例 Secret sample-secret.yml,如下所示:Secret 对象中的属性值始终采用 base64 编码(使用 base64 命令行工具)。当 Secret 挂载到 pod 的文件系统中时,这些值会自动解码为纯文本。

sample-secret.yml 文件

apiVersion: v1
kind: Secret
metadata: 
1

  name: camel-config
type: Opaque
data:
  # The username is 'myuser'
  quickstart.queue-username: bXl1c2VyCg== 
2

  quickstart.queue-password: MWYyZDFlMmU2N2Rm 
3
Copy to Clipboard Toggle word wrap

1
metadata.name: 标识 Secret。OpenShift 系统的其他部分使用此标识符来引用 Secret。
2
quickstart.queue-username :旨在注入 QuickstartConfiguration bean 的 queueUsername 属性。该值必须 采用 base64 编码。
3
quickstart.queue-password :旨在注入到 quickstartConfiguration bean 的 queuePassword 属性中。该值必须 采用 base64 编码。
注意

Kubernetes 不允许在 CamelCase 中定义属性名称(需要属性名称才能都是小写)。要临时解决这个问题,请使用连字符格式 queue-username,Spring Boot 与 queueUsername 匹配。这利用 Spring Boot 的 relaxed 绑定规则 进行外部化配置。

为 Secret 配置卷挂载

应用程序必须配置为在运行时加载 Secret,方法是将 Secret 配置为卷挂载。应用程序启动后,Secret 属性就会在文件系统中指定的位置可用。应用的 deployment.yml 文件位于 src/main/fabric8/ 目录下,后者定义 Secret 的卷挂载。

deployment.yml 文件

spec:
  template:
    spec:
      serviceAccountName: "qs-camel-config"
      volumes: 
1

        - name: "camel-config"
          secret:
            # The secret must be created before deploying this application
            secretName: "camel-config"
      containers:
        -
          volumeMounts: 
2

            - name: "camel-config"
              readOnly: true
              # Mount the secret where spring-cloud-kubernetes is configured to read it
              # see src/main/resources/bootstrap.yml
              mountPath: "/etc/secrets/camel-config"
          resources:
#            requests:
#              cpu: "0.2"
#              memory: 256Mi
#            limits:
#              cpu: "1.0"
#              memory: 256Mi
             env:
              - name: SPRING_APPLICATION_JSON
               value: '{"server":{"undertow":{"io-threads":1, "worker-threads":2 }}}'
Copy to Clipboard Toggle word wrap

1
volumes 部分中,部署会声明一个名为 camel-config 的新卷,它引用名为 camel-config 的 Secret。
2
volumeMounts 部分中,部署声明一个新的卷挂载,它引用了 camel-config 卷,并指定 Secret 卷应挂载到 pod 文件系统的路径 /etc/secrets/camel-config 中。

配置 spring-cloud-kubernetes 来读取 Secret 属性

要将 secret 与 Spring Boot 外部化配置集成,必须使用 secret 的挂载路径配置 Spring Cloud Kubernetes 插件。Spring Cloud Kubernetes 从指定的位置读取 secret,并使其可用于 Spring Boot 作为属性源。Spring Cloud Kubernetes 插件由 bootstrap.yml 文件中的设置进行配置,位于 Quickstart 项目中的 src/main/resources 下。

bootstrap.yml 文件

# Startup configuration of Spring-cloud-kubernetes
spring:
  application:
    name: camel-config
  cloud:
    kubernetes:
      reload:
        # Enable live reload on ConfigMap change (disabled for Secrets by default)
        enabled: true
      secrets:
        paths: /etc/secrets/camel-config
Copy to Clipboard Toggle word wrap

spring.cloud.kubernetes.secrets.paths 属性指定 pod 中 secret 卷挂载的路径列表。

注意

bootstrap.properties 文件(或 bootstrap.yml 文件)的行为与 application.properties 文件类似,但它会在应用程序启动的早期阶段载入。在 bootstrap.properties 文件中设置与 Spring Cloud Kubernetes 插件相关的属性更可靠。

9.2.4. 设置 ConfigMap

除了创建 ConfigMap 对象并正确设置 view 权限外,与 Spring Cloud Kubernetes 集成还需要您将 ConfigMap 的 metadata.name 与项目的 bootstrap.yml 文件中配置的 spring.application.name 属性的值匹配。设置 ConfigMap:

  • 创建示例 ConfigMap 对象
  • 设置查看权限
  • 配置 Spring Cloud Kubernetes 插件

ConfigMap 对象示例

Quickstart 项目提供一个示例 ConfigMap sample-configmap.yml

kind: ConfigMap
apiVersion: v1
metadata: 
1

  # Must match the 'spring.application.name' property of the application
  name: camel-config
data:
  application.properties: | 
2

    # Override the configuration properties here
    quickstart.recipients=direct:async-queue,direct:file,direct:mail 
3
Copy to Clipboard Toggle word wrap
1
metadata.name: 标识 ConfigMap。OpenShift 系统的其他部分使用此标识符来引用 ConfigMap。
2
data.application.properties :本节列出了属性设置,可覆盖使用应用程序部署的原始 application.properties 文件中的设置。
3
quickstart.recipients :旨在注入到 quickstartConfiguration bean 的 receivers 属性中。

设置查看权限

如 Secret 的 deployment.yml 文件所示,serviceAccountName 在项目的 deployment.yml 文件中被设置为 qs-camel-config。因此,您需要输入以下命令来启用快速启动应用程序的 查看 权限(假设它部署到 测试 项目命名空间中):

oc policy add-role-to-user view system:serviceaccount:test:qs-camel-config
Copy to Clipboard Toggle word wrap

配置 Spring Cloud Kubernetes 插件

Spring Cloud Kubernetes 插件由 bootstrap.yml 文件中的以下设置配置。

spring.application.name
这个值必须与 ConfigMap 对象的 metadata.name 匹配(例如,在 Quickstart 项目中的 sample-configmap.yml 中定义)。默认为 应用程序
spring.cloud.kubernetes.reload.enabled
把它设置为 true 可启用 ConfigMap 对象的动态重新加载。

有关支持的属性的更多详细信息,请参阅 PropertySource Reload Configuration Properties

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat