11.2. 为 ConfigMap 属性源运行教程
以下教程允许您试验设置 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。
流程
打开一个新的 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
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
Copy to Clipboard Copied! Toggle word wrap Toggle overflow archetype 插件切换到互动模式,提示您输入剩余的字段:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 出现提示时,为
groupId
值输入org.example.fis
,为artifactId
值输入fuse713-configmap
。接受剩余字段的默认值。登录 OpenShift,并切换到要部署应用的 OpenShift 项目。例如,要以
developer
用户身份登录并部署到openshift
项目,请输入以下命令:oc login -u developer -p developer oc project openshift
oc login -u developer -p developer oc project openshift
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 在命令行中,切换到新的
fuse713-configmap
项目目录,并为此应用创建 Secret 对象。cd fuse713-configmap oc create -f sample-secret.yml
cd fuse713-configmap oc create -f sample-secret.yml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 注意在部署应用程序前,需要创建 Secret 对象,否则部署的容器进入 wait 状态,直到 Secret 可用为止。如果随后创建 Secret,容器将退出等待状态。有关如何设置 Secret 对象的更多信息,请参阅设置 Secret。
构建和部署快速入门应用程序。在
fuse713-configmap
项目的顶部级别输入:mvn oc:deploy -Popenshift
mvn oc:deploy -Popenshift
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 查看应用日志,如下所示:
- 导航到浏览器中的 OpenShift Web 控制台(https://OPENSHIFT_IP_ADDR,将 OPENSHIFT_IP_ADDR 替换为集群的 IP 地址),并使用您的凭证(例如,使用用户名 developer和密码 developer )登录控制台。
- 在左侧面板中,展开 Home。单击 Status 以查看 Project Status 页面。此时会显示所选命名空间中的所有现有应用程序(如 openshift)。
-
单击
fuse713-configmap
,以查看 Quickstart 的 Overview 信息页面。 - 在左侧面板中,展开 Workloads。
-
单击 Pods,然后单击
fuse713-configmap-xxxx
。此时会显示应用程序的 Pod 详情。 - 点 Logs 选项卡查看应用程序日志。
默认接收者列表在
src/main/resources/application.properties
中配置,将生成的消息发送到两个 dummy 端点:direct:async-queue
和direct: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
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
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 在使用 ConfigMap 对象更新
fuse713-configmap
应用配置前,您必须提供fuse713-configmap
应用程序权限来查看 OpenShift ApiServer 中的数据。输入以下命令为fuse713-configmap
应用程序的服务帐户授予view
权限:oc policy add-role-to-user view system:serviceaccount:openshift:qs-camel-config
oc policy add-role-to-user view system:serviceaccount:openshift: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 路由的接收者列表,将其配置为将生成的消息发送到 三个 虚拟端点:
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
11.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 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/
目录下),如下所示:
- 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:
- 创建示例 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/jkube/
目录下,它定义了 Secret 的卷挂载。
deployment.yml 文件
配置 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 文件
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
示例。
设置查看权限
如 Secret 的 deployment.yml 文件中所示,serviceAccountName
在项目的 deployment.yml
文件中设置为 qs-camel-config
。因此,您需要输入以下命令来启用 Quickstart 应用的 查看
权限(假设它部署到 test
项目命名空间中):
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。