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。
流程
打开新的 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
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 Copied! Toggle word wrap Toggle overflow archetype 插件切换到交互模式以提示您输入剩余的字段:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 出现提示时,为
groupId
值输入org.example.fis
,为artifactId
值输入fuse74-configmap
。接受剩余的字段的默认值。登录 OpenShift 并切换到您要部署应用的 OpenShift 项目。例如,若要以
developer
用户身份登录并部署到test
项目,请输入以下命令:oc login -u developer -p developer oc project test
oc login -u developer -p developer oc project test
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 在命令行中,更改到 new
fuse74-configmap
项目的目录,再为此应用创建 Secret 对象。oc create -f sample-secret.yml
oc create -f sample-secret.yml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 注意在部署应用程序前,需要创建 Secret 对象,否则部署的容器会进入等待状态,直到 Secret 变为可用。如果您随后创建 Secret,则容器将退出等待状态。有关如何设置 Secret 对象的更多信息,请参阅设置 Secret。
构建和部署快速入门应用程序。从
fuse74-configmap
项目的顶层,输入:mvn fabric8:deploy -Popenshift
mvn fabric8:deploy -Popenshift
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 按如下所示查看应用日志:
-
在浏览器中打开 OpenShift 控制台,再选择相关的项目命名空间(如
test
)。 -
单击
fuse74-configmap
服务的 circular pod 图标的中心。 - 在 Pod 视图中,点 pod Name 查看正在运行的 Pod 的详细信息(如果只有一个 pod 正在运行),您将直接转至详细信息页面。
- 单击 Logs 标签,以查看应用程序日志并向下滚动,以查找由 Camel 应用程序生成的日志消息。
-
在浏览器中打开 OpenShift 控制台,再选择相关的项目命名空间(如
默认接收者列表(在
src/main/resources/application.properties
中配置)将生成的消息发送到两个 dummy 端点:direct:async-queue
和direct: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
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 Copied! Toggle word wrap Toggle overflow 在使用 ConfigMap 对象更新
fuse74-configmap
应用程序配置前,您必须提供fuse74-configmap
应用程序权限来查看 OpenShift ApiServer 的数据。输入以下命令为fuse74-configmap
应用程序的服务帐户授予view
权限:oc policy add-role-to-user view system:serviceaccount:test:qs-camel-config
oc policy add-role-to-user view system:serviceaccount:test:qs-camel-config
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 注意服务帐户通过语法
system:serviceaccount:PROJECT_NAME:SERVICE_ACCOUNT_NAME
来指定。fis-config
部署描述符将SERVICE_ACCOUNT_NAME
定义为qs-camel-config
。要查看操作中的实时重新加载功能,请按如下所示创建一个 ConfigMap 对象:
oc create -f sample-configmap.yml
oc create -f sample-configmap.yml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 新的 ConfigMap 覆盖正在运行的应用程序中 Camel 路由的接收者列表,将其配置为将生成的消息发送到 三个 dummy 端点:
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
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 Copied! Toggle word wrap Toggle overflow
9.2.2. 配置属性 Bean 复制链接链接已复制到粘贴板!
配置属性 bean 是可通过注入接收配置设置的常规 Java Bean。它提供了 Java 代码和外部配置机制之间的基本接口。
外部化配置和 Bean Registry
下图显示了 Spring Boot Externalized Configuration 如何在 spring-boot-camel-config
quickstart 中工作。
配置机制有以下主要部分:
- 属性源
-
提供注入配置的属性设置。默认属性源是应用程序的
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/
目录下),如下所示:
- 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:
- 创建 Sample Secret 对象
- 为 Secret 配置卷挂载
- 将 spring-cloud-kubernetes 配置为读取 Secret 属性
Secret 对象示例
Quickstart 项目提供了一个示例 Secret sample-secret.yml
,如下所示:Secret 对象中的属性值始终采用 base64 编码(使用 base64
命令行工具)。当 Secret 挂载到 pod 的文件系统中时,这些值会自动解码为纯文本。
sample-secret.yml 文件
Kubernetes 不允许在 CamelCase 中定义属性名称(需要属性名称才能都是小写)。要临时解决这个问题,请使用连字符格式 queue-username
,Spring Boot 与 queueUsername
匹配。这利用 Spring Boot 的 relaxed 绑定规则 进行外部化配置。
为 Secret 配置卷挂载
应用程序必须配置为在运行时加载 Secret,方法是将 Secret 配置为卷挂载。应用程序启动后,Secret 属性就会在文件系统中指定的位置可用。应用的 deployment.yml
文件位于 src/main/fabric8/
目录下,后者定义 Secret 的卷挂载。
deployment.yml 文件
配置 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 文件
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
。
设置查看权限
如 Secret 的 deployment.yml 文件所示,serviceAccountName
在项目的 deployment.yml
文件中被设置为 qs-camel-config
。因此,您需要输入以下命令来启用快速启动应用程序的 查看
权限(假设它部署到 测试
项目命名空间中):
oc policy add-role-to-user view system:serviceaccount:test:qs-camel-config
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。