8.10. 在 OpenShift 上的应用程序中使用 ConfigMap


对于 OpenShift,您可以使用部署控制器(dc)将 configmap 挂载到用于运行应用的容器集中。

ConfigMap 是一种 OpenShift 资源,用于在键值对中存储非机密数据。

在指定了 microprofile-platform Galleon 层以 add microprofile-config-smallrye 子系统和服务器配置文件的任何扩展后,您可以使用 CLI 脚本向 服务器配置添加新 ConfigSource。您可以在 Maven 项目的根目录下将 CLI 脚本保存在可访问的目录中,如 /scripts 目录。

MicroProfile 配置功能在 JBoss EAP 中使用 SmallRye Config 组件实施,由 microprofile-config-smallrye 子系统提供。此子系统包含在 microprofile-platform Galleon 层中。

先决条件

  • 已安装 Maven。
  • 您已配置了 JBoss EAP Maven 存储库。
  • 您已将应用打包为可引导 JAR,您可以在 JBoss EAP OpenShift 平台上运行该应用。有关在 OpenShift 平台上将应用构建为可引导 JAR 的信息,请参阅 在 JBoss EAP OpenShift 平台上使用可引导 JAR

流程

  1. 在项目的根目录下,创建名为 scripts 的目录。例如:

    $ mkdir scripts
  2. 创建 cli.properties 文件,并将文件保存到 /scripts 目录中。在此文件中定义 config.pathconfig.ordinal 系统属性。例如:

    config.path=/etc/config
    config.ordinal=200
  3. 创建 CLI 脚本,如 mp-config.cli,并将它保存在可引导 JAR 的可访问目录中,如 /scripts 目录。以下示例显示了 mp-config.cli 脚本的内容:

    # config map
    
    /subsystem=microprofile-config-smallrye/config-source=os-map:add(dir={path=${config.path}}, ordinal=${config.ordinal})

    The mp-config.cli CLI 脚本创建一个新的 ConfigSource,其从属性文件检索到序数和路径值。

  4. 将脚本保存到 /scripts 目录中,该目录位于项目的根目录下。
  5. 将以下配置提取添加到现有插件 <configuration> 元素中:

    <cli-sessions>
        <cli-session>
            <properties-file>
                scripts/cli.properties
            </properties-file>
            <script-files>
                <script>scripts/mp-config.cli</script>
            </script-files>
        </cli-session>
    </cli-sessions>
  6. 打包应用程序:

    $ mvn package
  7. 使用 oc login 命令登录 您的 OpenShift 实例。
  8. 可选: 如果您之前没有创建 target/openshift 子目录,则必须使用以下命令创建 suddirectory:

    $ mkdir target/openshift
  9. 将打包的应用复制到创建的子目录中。

    $ cp target/microprofile-config-bootable.jar target/openshift
  10. 使用 target/openshift 子目录作为二进制输入来构建应用程序:

    $ oc start-build microprofile-config-app --from-dir target/openshift
    注意

    OpenShift 将一组 CLI 脚本命令应用到可引导 JAR 配置文件,以将其用于云环境。您可以通过打开 Maven 项目 /target 目录中的 bootable-jar-build-artifacts/generated-cli-script.txt 文件来访问此脚本。

  11. 创建 ConfigMap。例如:

    $ oc create configmap microprofile-config-map --from-literal=name="Name comes from Openshift ConfigMap"
  12. 使用 dc 将 ConfigMap 挂载到应用中。例如:

    $ oc set volume deployments/microprofile-config-app --add --name=config-volume \
    --mount-path=/etc/config \
    --type=configmap \
    --configmap-name=microprofile-config-map

    在执行 oc set volume 命令后,应用将使用新的配置设置重新部署。

  13. 测试输出:

    $ curl http://$(oc get route microprofile-config-app --template='{{ .spec.host }}')/config/json

    以下是预期的输出:

    {"result":"Hello Name comes from Openshift ConfigMap"}
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat
返回顶部