搜索

11.2. 为 ConfigMap 属性源运行教程

download PDF

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

11.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-7_13_0-00014-redhat-00001/archetypes-catalog-2.2.0.fuse-7_13_0-00014-redhat-00001-archetype-catalog.xml \
      -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \
      -DarchetypeArtifactId=spring-boot-camel-config-archetype \
      -DarchetypeVersion=2.2.0.fuse-7_13_0-00014-redhat-00001

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

    Define value for property 'groupId': : org.example.fis
    Define value for property 'artifactId': : fuse713-configmap
    Define value for property 'version':  1.0-SNAPSHOT: :
    Define value for property 'package':  org.example.fis: :
    Confirm properties configuration:
    groupId: org.example.fis
    artifactId: fuse713-configmap
    version: 1.0-SNAPSHOT
    package: org.example.fis
    Y: : Y

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

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

    oc login -u developer -p developer
    oc project openshift
  3. 在命令行中,切换到新的 fuse713-configmap 项目目录,并为此应用创建 Secret 对象。

    cd fuse713-configmap
    oc create -f sample-secret.yml
    注意

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

  4. 构建和部署快速入门应用程序。在 fuse713-configmap 项目的顶部级别输入:

    mvn oc:deploy -Popenshift
  5. 查看应用日志,如下所示:

    1. 导航到浏览器中的 OpenShift Web 控制台(https://OPENSHIFT_IP_ADDR,将 OPENSHIFT_IP_ADDR 替换为集群的 IP 地址),并使用您的凭证(例如,使用用户名 developer和密码 developer )登录控制台。
    2. 在左侧面板中,展开 Home。单击 Status 以查看 Project Status 页面。此时会显示所选命名空间中的所有现有应用程序(如 openshift)。
    3. 单击 fuse713-configmap,以查看 Quickstart 的 Overview 信息页面。
    4. 在左侧面板中,展开 Workloads
    5. 单击 Pods,然后单击 fuse713-configmap-xxxx。此时会显示应用程序的 Pod 详情。
    6. Logs 选项卡查看应用程序日志。
  6. 默认接收者列表在 src/main/resources/application.properties 中配置,将生成的消息发送到两个 dummy 端点: direct:async-queuedirect:file。这会导致以下信息写入应用程序日志中:

    5:44:57.377 [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
  7. 在使用 ConfigMap 对象更新 fuse713-configmap 应用配置前,您必须提供 fuse713-configmap 应用程序权限来查看 OpenShift ApiServer 中的数据。输入以下命令为 fuse713-configmap 应用程序的服务帐户授予 view 权限:

    oc policy add-role-to-user view system:serviceaccount:openshift:qs-camel-config
    注意

    服务帐户通过 system:serviceaccount:PROJECT_NAME:SERVICE_ACCOUNT_NAME 指定。fis-config 部署描述符定义 SERVICE_ACCOUNT_NAMEqs-camel-config

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

    oc create -f sample-configmap.yml

    新 ConfigMap 覆盖正在运行的应用程序中 Camel 路由的接收者列表,将其配置为将生成的消息发送到 三个 虚拟端点: direct:async-queue,direct: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

11.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 decorated。
Spring bean registry
使用 requisite 注解时,在 Spring bean registry 中注册了配置属性 bean。
与 Camel bean registry 集成
Camel bean registry 会自动与 Spring bean registry 集成,以便在 Camel 路由中引用注册的 Spring Bean。

QuickstartConfiguration 类

fuse713-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
    ...
}
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 属性可从属性源注入。

11.2.3. 设置 Secret

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

  1. 创建示例 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

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/jkube/ 目录下,它定义了 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 }}}'

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

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

要将 secret 与 Spring Boot 外部化配置集成,Spring Cloud Kubernetes 插件必须配置有 secret 的挂载路径。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

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

注意

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

11.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
1
metadata.name: 标识 ConfigMap。OpenShift 系统的其他部分使用此标识符来引用 ConfigMap。
2
data.application.properties:本节列出了可覆盖使用应用程序部署的原始 application.properties 文件中的设置的属性设置。
3
quickstart.recipients :它被注入到 quickstartConfiguration bean 的 recipients 属性中。

设置查看权限

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

oc policy add-role-to-user view system:serviceaccount:test:qs-camel-config

配置 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 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

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

© 2024 Red Hat, Inc.