2.2. 将 Node.js 应用程序部署到 Openshift


要将 Node.js 应用部署到 OpenShift,请将 nodeshift 添加到应用,配置 package.json 文件,然后使用 nodeshift 进行部署。

2.2.1. 为 OpenShift 部署准备 Node.js 应用

要为 OpenShift 部署准备 Node.js 应用程序,您必须执行以下步骤:

  • nodeshift 添加到应用程序。
  • openshiftstart 条目添加到 package.json 文件。

先决条件

  • 安装了 npm

步骤

  1. nodeshift 添加到应用程序中。

    $ npm install nodeshift --save-dev
    Copy to Clipboard Toggle word wrap
  2. openshiftstart 条目添加到 package.json 中的 scripts 部分。

    {
      "name": "myApp",
      "version": "1.0.0",
      "description": "",
      "main": "app.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "openshift": "nodeshift --expose --dockerImage=registry.access.redhat.com/rhscl/ubi8/nodejs-12",
        "start": "node app.js",
        ...
      }
      ...
    }
    Copy to Clipboard Toggle word wrap

    openshift 脚本使用 nodeshift 将应用部署到 OpenShift。

    注意

    Node.js 提供了通用基础镜像和 RHEL 镜像。有关镜像名称的更多信息,请参阅 Node.js 发行注记。

  3. 可选 :在 package.json 中添加一个 files 部分。

    {
      "name": "myApp",
      "version": "1.0.0",
      "description": "",
      "main": "app.js",
      "scripts": {
        ...
      },
      "files": [
        "package.json",
        "app.js"
      ]
      ...
    }
    Copy to Clipboard Toggle word wrap

    files 部分告知 nodeshift 在部署到 OpenShift 时要包含哪些文件和目录。nodeshift 使用 node-tar 模块根据您在 files 部分中列出的文件和目录创建 tar 文件。nodeshift 将应用部署到 OpenShift 时使用此 tar 文件。如果没有指定 files 部分,nodeshift 将发送整个当前目录,不包括:

    • node_modules/
    • .git/
    • tmp/

      建议您在 package.json 中包含 files 部分,以避免在部署到 OpenShift 时包括不必要的文件。

2.2.2. 将 Node.js 应用部署到 OpenShift

您可以使用 nodeshift 将 Node.js 应用部署到 OpenShift。

先决条件

  • 安装了 oc CLI 客户端。
  • 安装了 npm
  • 在配置路由时,请确保应用程序使用的所有端口都被正确公开。

步骤

  1. 使用 oc 客户端登录到您的 OpenShift 实例。

    $ oc login ...
    Copy to Clipboard Toggle word wrap
  2. 使用 nodeshift 将应用部署到 OpenShift。

    $ npm run openshift
    Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat