在 OpenShift Container Platform 中使用 .NET


.NET 9.0

在 OpenShift Container Platform 上安装并运行 .NET 9.0

Red Hat Customer Content Services

摘要

本指南论述了如何在 OpenShift Container Platform 上安装和运行 .NET 9.0。

对红帽文档提供反馈

我们感谢您对我们文档的反馈。让我们了解如何改进它。

通过 Jira 提交反馈(需要帐户)

  1. 登录到 Jira 网站。
  2. 在顶部导航栏中点 Create
  3. Summary 字段中输入描述性标题。
  4. Description 字段中输入您对改进的建议。包括文档相关部分的链接。
  5. 点对话框底部的 Create

第 1 章 概述

通过从 s2i-dotnetcore 导入镜像流定义,将 .NET 镜像添加到 OpenShift。

镜像流定义包括 dotnet 镜像流,其中包含用于不同支持的 .NET 版本的 sdk 镜像。.NET 程序的生命周期和支持政策 提供了支持版本的最新概述。

Expand
VersionTagAlias

.NET 8.0

dotnet:8.0-ubi8

dotnet:8.0

.NET 9.0

dotnet:9.0-ubi8

dotnet:9.0

sdk 镜像具有对应的运行时镜像,它们在 dotnet-runtime 镜像流下定义。

容器镜像在不同的 Red Hat Enterprise Linux 和 OpenShift 版本间工作。基于 UBI-8 的镜像(suffix -ubi8)托管在 registry.access.redhat.com 上,不需要身份验证。

第 2 章 安装 .NET 镜像流

要安装 .NET 镜像流,请使用 s2i-dotnetcore 中的镜像流定义以及 OpenShift Client (oc)二进制文件。镜像流可以从 Linux、Mac 和 Windows 安装。

您可以在全局 openshift 命名空间中或本地在项目命名空间中定义 .NET 镜像流。更新 openshift 命名空间定义需要足够的权限。

流程

  1. 安装(或更新)镜像流:

    $ oc apply [-n namespace] -f
    https://raw.githubusercontent.com/redhat-developer/s2i-dotnetcore/main/dotnet_imagestreams.json
    Copy to Clipboard Toggle word wrap

第 3 章 使用 OpenShift 客户端部署应用程序

您可以使用 OpenShift Client (oc)进行应用程序部署。

流程

  1. 创建新的 OpenShift 项目:

    $ oc new-project sample-project
    Copy to Clipboard Toggle word wrap
  2. 添加 ASP.NET Core 应用程序:

    $ oc new-app --name=example-app 'dotnet:9.0-ubi8~https://github.com/redhat-developer/s2i-dotnetcore-ex#dotnet-9.0' --build-env DOTNET_STARTUP_PROJECT=app
    Copy to Clipboard Toggle word wrap
  3. 监控构建的进度:

    $ oc logs -f bc/example-app
    Copy to Clipboard Toggle word wrap
  4. 构建完成后查看部署的应用程序:

    $ oc logs -f dc/example-app
    Copy to Clipboard Toggle word wrap

    该应用现在可在项目内访问。

  5. 可选:使项目可以被外部访问:

    $ oc expose svc/example-app
    Copy to Clipboard Toggle word wrap
  6. 获取可共享 URL:

    $ oc get routes
    Copy to Clipboard Toggle word wrap

第 4 章 .NET 9.0 的环境变量

.NET 镜像支持多个环境变量来控制 .NET 应用程序的构建行为。您可以将这些变量设置为构建配置的一部分,或者将它们添加到应用程序源代码存储库中的 .s2i/environment 文件中。

Expand
变量名称描述default

DOTNET_STARTUP_PROJECT

选择要运行的项目。这必须是项目文件(如 csprojfsproj)或包含单个项目文件的文件夹。

.

DOTNET_ASSEMBLY_NAME

选择要运行的 assembly。这不得包含 .dll 扩展。把它设置为 csproj 中指定的输出装配名称(PropertyGroup/AssemblyName)。

csproj 文件的名称

DOTNET_PUBLISH_READYTORUN

当设置为 true 时,应用程序将提前编译。这可减少 JIT 加载时所需的工作量,从而缩短启动时间。

false

DOTNET_RESTORE_SOURCES

指定恢复操作中使用的 NuGet 软件包源的空格分隔列表。这会覆盖 NuGet.config 文件中指定的所有源。此变量不能与 DOTNET_RESTORE_CONFIGFILE 结合使用。

 

DOTNET_RESTORE_CONFIGFILE

指定用于恢复操作的 NuGet.Config 文件。此变量不能与 DOTNET_RESTORE_SOURCES 结合使用。

 

DOTNET_TOOLS

指定在构建应用程序前要安装的 .NET 工具列表。可以通过发布待处理软件包名称(带有 @ <version> )来安装特定版本。

 

DOTNET_TEST_PROJECTS

指定要测试的测试项目列表。这必须是包含单个项目文件的项目文件或文件夹。为每个项目调用 dotnet test

 

DOTNET_CONFIGURATION

以 Debug 或 Release 模式运行应用程序。这个值应该是 ReleaseDebug

Release

DOTNET_VERBOSITY

指定 dotnet 构建 命令的详细程度。设置后,环境变量会在构建开始时打印。这个变量可以被设置为 msbuild verbosity 值(q[uiet]m[inimal]n[ormal]d[etailed]diag[nostic])。

 

HTTP_PROXY, HTTPS_PROXY

配置分别构建和运行应用时使用的 HTTP 或 HTTPS 代理。

 

DOTNET_RM_SRC

当设置为 true 时,镜像中不包含源代码。

 

SSL_CERT_DIR

指定要信任的额外 SSL 证书的文件夹或文件列表。证书由构建期间运行的每个进程以及构建后在镜像中运行的所有进程(包括构建的应用程序)信任。这些项目可以是绝对路径(从 /开始)或源存储库中的路径(如证书)。

 

ASPNETCORE_URLS

这个变量设定为 http://*:8080 以配置 ASP.NET Core 以使用由镜像公开的端口。不建议更改此设置。

http://*:8080

DOTNET_RESTORE_DISABLE_PARALLEL

当设置为 true 时,会禁用并行恢复多个项目。这可减少构建容器以低 CPU 限值运行时恢复超时错误。

false

DOTNET_INCREMENTAL

当设置为 true 时,将保留 NuGet 软件包,以便它们可用于增量构建。

false

DOTNET_PACK

当设置为 true 时,在 /opt/app-root/app. tar.gz 中创建一个 tar.gz 文件,其中包含公布的应用程序。

 

第 5 章 使用 .NET 9.0 创建示例应用程序

5.1. 创建 MVC 示例应用程序

s2i-dotnetcore-ex 是 .NET 的默认 Model, View, Controller (MVC)模板应用程序。

此应用程序被 .NET S2I 镜像用作示例应用程序,可以使用 Try 示例 链接直接从 OpenShift UI 创建。

也可以使用 OpenShift 客户端二进制文件(oc)创建应用。

流程

使用 oc 创建示例应用程序:

  1. 添加 .NET 应用程序:

    $ oc new-app dotnet:9.0-ubi8~https://github.com/redhat-developer/s2i-dotnetcore-ex#dotnet-9.0 --context-dir=app
    Copy to Clipboard Toggle word wrap
  2. 使应用程序可以被外部访问:

    $ oc expose service s2i-dotnetcore-ex
    Copy to Clipboard Toggle word wrap
  3. 获取 sharable URL:

    $ oc get route s2i-dotnetcore-ex
    Copy to Clipboard Toggle word wrap

5.2. 创建 CRUD 示例应用程序

s2i-dotnetcore-persistent-ex 是一个一个简单的 Create, Read, Update, Delete (CRUD).NET web 应用程序,它将数据存储在 PostgreSQL 数据库中。

流程

使用 oc 创建示例应用程序:

  1. 添加数据库:

    $ oc new-app postgresql-ephemeral
    Copy to Clipboard Toggle word wrap
  2. 添加 .NET 应用程序:

    $ oc new-app dotnet:9.0-ubi8~https://github.com/redhat-developer/s2i-dotnetcore-persistent-ex#dotnet-9.0 --context-dir app
    Copy to Clipboard Toggle word wrap
  3. postgresql secret 和数据库服务名称环境变量添加环境变量:

    $ oc set env dc/s2i-dotnetcore-persistent-ex --from=secret/postgresql -e database-service=postgresql
    Copy to Clipboard Toggle word wrap
  4. 使应用程序可以被外部访问:

    $ oc expose service s2i-dotnetcore-persistent-ex
    Copy to Clipboard Toggle word wrap
  5. 获取 sharable URL:

    $ oc get route s2i-dotnetcore-persistent-ex
    Copy to Clipboard Toggle word wrap

法律通告

Copyright © 2025 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat
返回顶部