Increase visibility into IT operations to detect and resolve technical issues before they impact your business.
Learn More
Go to Insights
Engage with our Red Hat Product Security team, access security updates, and ensure your environments are not exposed to any known security vulnerabilities.
Product Security Center
Keep your systems secure with Red Hat's specialized responses to security vulnerabilities.
View Responses
摘要
本指南详细介绍了使用 Node.js 运行时。
本指南涵盖开发人员使用 Node.js 运行时所需的概念和实际详情。
第 1 章 使用 Node.js 进行应用程序开发简介
本节介绍使用红帽运行时进行应用程序开发的基本概念。它还提供有关 Node.js 运行时的概述。
1.1. 使用 Red Hat Runtimes 进行应用程序开发概述
Red Hat OpenShift
是一个容器应用程序平台,提供一组云原生运行时集合。您可以使用运行时在 OpenShift 中开发、构建和部署 Java 或 JavaScript 应用。
使用 Red Hat Runtimes for OpenShift 进行应用程序开发包括:
一个运行时集合,如 Eclipse Vert.x、Thotail、Spring Boot 等,它们设计为在 OpenShift 上运行。
在 OpenShift 上进行云原生开发的指定方法。
OpenShift 可帮助您管理、保护和自动化应用的部署和监控。您可以将业务问题分解为较小的微服务,并使用 OpenShift 部署、监控和维护微服务。您可以在应用程序中实施断路器、健康检查和服务发现等模式。
云原生开发充分利用云计算。
您可以在以下位置构建、部署和管理应用程序:
Node.js 基于 Google 的
V8 JavaScript 引擎
,允许您编写服务器端 JavaScript 应用。它根据事件和非阻塞操作提供 I/O 模型,使您能够编写高效的应用程序。Node.js 还提供一个名为
npm
的大型模块生态系统。查看
附加资源
以进一步读取 Node.js。
Node.js 运行时允许您在 OpenShift 上运行 Node.js 应用程序和服务,同时提供 OpenShift 平台的所有优点和方便性,如滚动更新、持续交付管道、服务发现和 Canary 部署。OpenShift 还便于您的应用实施常见微服务模式,如外部化配置、健康检查、断路器和故障转移。
红帽提供了不同的 Node.js 版本。有关如何获得支持的更多信息,请参阅
从红帽获得 Node.js 和支持
。
Node.js 支持以下构架:
x86_64 (AMD64)
OpenShift 环境中的 IBM Z (s390x)
OpenShift 环境中的 IBM Power 系统(ppc64le)
联邦信息处理标准(FIPS)提供了提高计算机系统和网络的安全性和互操作性的指南和要求。FIPS 140-2 和 140-3 系列适用于硬件和软件级别的加密模块。
联邦信息处理标准(FIPS)出版物 140-2 是美国开发的计算机安全标准。政府和行业工作组来验证加密模块的质量。请参阅
NIST 计算机安全资源中心
上的官方 FIPS 出版物。
Red Hat Enterprise Linux (RHEL)提供了一个集成框架,可在系统范围内启用 FIPS 140-2 合规性。在 FIPS 模式下运行时,使用加密库的软件包会根据全局策略自行配置。
要了解合规要求,请参阅
红帽政府标准
页面。
Red Hat build of Node.js 在启用了 FIPS 的 RHEL 系统上运行,并使用 RHEL 提供的 FIPS 认证库。
1.2.2.2. 验证 Node.js 是否在 FIPS 模式下运行
您可以使用
crypto.fips
来验证 Node.js 是否在 FIPS 模式下运行。
RHEL 主机上启用了 FIPS。
在 Node.js 项目中,创建一个名为的应用程序文件,如
app.js
。
在
app.js
文件中输入以下详情:
const crypto = require('crypto');
console.log(crypto.fips);
保存
app.js
文件。
在 Node.js 项目中,运行
app.js
文件:
node app.js
如果启用了 FIPS,应用程序会将
1
打印到控制台。如果禁用了 FIPS,应用程序会将
0
打印到控制台。
您可以创建新的 Node.js 应用程序,并将它们部署到 OpenShift。
对于基本的 Node.js 应用,您必须创建一个 JavaScript 文件,其中包含 Node.js 方法。
已安装了
npm
。
创建新目录
myApp
,再导航到该目录。
$ mkdir myApp
$ cd MyApp
这是应用程序的根目录。
使用
npm
初始化您的应用。
本例的其余部分假定入口点是
app.js
,在运行
npm init
时提示您对其进行设置。
$ cd myApp
$ npm init
在名为
app.js
的新文件中创建入口点。
使用
curl
或浏览器,验证您的应用程序是否在
http://localhost:8080
中运行。
$ curl http://localhost:8080
{"content":"Hello, World!"}
2.2. 将 Node.js 应用程序部署到 Openshift
要将 Node.js 应用部署到 OpenShift,请将
nodeshift
添加到应用程序中,配置
package.json
文件,然后使用
nodeshift
进行部署。
2.2.1. 为 OpenShift 部署准备 Node.js 应用程序
要为 OpenShift 部署准备 Node.js 应用程序,您必须执行以下步骤:
将
nodeshift
添加到应用程序。
在
package.json
文件中添加
openshift
和
start
条目。
已安装了
npm
。
将
nodeshift
添加到应用程序中。
$ npm install nodeshift --save-dev
将
openshift
和
start
条目添加到
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",
openshift
脚本使用
nodeshift
将应用部署到 OpenShift。
通用基础镜像和 RHEL 镜像可用于 Node.js。有关镜像名称的更多信息,请参阅 Node.js 发行注记。
可选
:在
package.json
中添加
文件
部分。
"name": "myApp",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"files": [
"package.json",
"app.js"
files
部分告诉
nodeshift
在部署到 OpenShift 时要包含哪些文件和目录。
nodeshift
使用
node-tar
模块根据您在
files
部分中列出的文件和目录创建一个 tar 文件。当
nodeshift
将应用程序部署到 OpenShift 时,会使用这个 tar 文件。如果没有指定
files
部分,
nodeshift
将发送整个当前目录,不包括:
node_modules/
.git/
建议您在
package.json
中包含
files
部分,以避免在部署到 OpenShift 时包括不必要的文件。
2.2.2. 将 Node.js 应用部署到 OpenShift
您可以使用
nodeshift
将 Node.js 应用部署到 OpenShift。
已安装
oc
CLI 客户端。
已安装了
npm
。
在配置路由时,确保应用程序使用的所有端口正确公开。
使用
oc
客户端登录到您的 OpenShift 实例。
$ oc login ...
使用
nodeshift
将应用程序部署到 OpenShift。
$ npm run openshift
2.3. 将 Node.js 应用程序部署到独立 Red Hat Enterprise Linux
您可以使用
npm
将 Node.js 应用程序部署到独立 Red Hat Enterprise Linux。
Node.js 应用。
已安装 npm 6.14.8
安装了 RHEL 7 或 RHEL 8。
已安装 Node.js
如果您在项目的
package.json
文件中指定了额外的依赖项,请确保在运行应用程序前安装它们。
$ npm install
从应用的根目录部署应用。
$ node app.js
Server running at http://localhost:8080
本节包含有关调试基于 Node.js 的应用的信息,并在本地和远程部署中使用调试日志。
要远程调试应用,您需要以调试模式启动它,并为它附加调试器。
原生调试器允许您使用内置的调试客户端调试基于 Node.js 的应用。
要调试的应用程序。
启动启用了 debugger 的应用。
原生调试器会自动附加,并提供调试提示。
3.1.2. 在本地启动应用程序并附加 V8 检查器
V8 检查器允许您使用其他工具(如
Chrome DevTools
)调试基于 Node.js 的应用,该工具使用
Chrome 调试协议
。
要调试的应用程序。
已安装 V8 检查器,如
Google Chrome 浏览器中
提供的检查器。
启动应用程序,启用了
V8 检查器集成
。
$ node --inspect app.js
如果您的应用程序有不同的入口点,则需要更改命令来指定该入口点:
$ node --inspect path/to/entrypoint
例如,当使用
express 生成器
创建应用程序时,入口点默认设置为
./bin/www
。
附加 V8 检查器并执行调试命令。
例如,如果使用 Google Chrome:
导航到
chrome://inspect
。
从
Remote Target
下选择您的应用程序。
现在,您可以看到应用程序的来源,并可执行调试操作。
3.1.3. 以调试模式在 OpenShift 上启动应用程序
要在 OpenShift 上远程调试基于 Node.js 的应用程序,您必须将容器内的
NODE_ENV
环境变量设置为
开发并
配置端口转发,以便您可以从远程调试器连接到应用程序。
在 OpenShift 上运行的应用。
已安装
oc
二进制文件。
在目标 OpenShift 环境中执行
oc port-forward
命令的功能。
使用
oc
命令,列出可用的部署配置:
$ oc get dc
将应用程序的部署配置中的
NODE_ENV
环境变量设置为
development
以启用调试。例如:
$ oc set env dc/MY_APP_NAME NODE_ENV=development
如果应用没有设置为在配置更改时自动重新部署,请重新部署应用。例如:
$ oc rollout latest dc/MY_APP_NAME
配置从本地机器到应用程序 pod 的端口转发:
列出当前运行的 pod,找到包含应用程序的 pod:
$ oc get pod
NAME READY STATUS RESTARTS AGE
MY_APP_NAME-3-1xrsp 0/1 Running 0 6s
配置端口转发:
$ oc port-forward MY_APP_NAME-3-1xrsp $LOCAL_PORT_NUMBER:5858
在这里,$LOCAL_PORT_NUMBER
是您在本地计算机上选择的未使用端口号。请记住远程调试器配置的这个数字。
附加 V8 检查器并执行调试命令。
例如,如果使用 Google Chrome:
导航到 chrome://inspect
。
单击 配置。
添加 127.0.0.1:$LOCAL_PORT_NUMBER
.
点 Done。
从 Remote Target 下选择您的应用程序。
现在,您可以看到应用程序的来源,并可执行调试操作。
完成调试后,在应用程序 pod 中取消设置 NODE_ENV
环境变量。例如:
$ oc set env dc/MY_APP_NAME NODE_ENV-
调试日志记录是在调试时向应用程序日志添加详细信息的方法。这可让您:
在应用程序正常操作期间保留最小日志输出,以提高可读性和减少磁盘空间的使用。
在解决问题时,查看有关应用程序内部工作的详细信息。
这个示例使用
debug 软件包
,但还有
其他软件包
可以处理调试日志。
您有一个要调试的应用程序。
添加
debug
日志记录定义。
const debug = require('debug')('myexample');
添加 debug 语句。
app.use('/api/greeting', (request, response) => {
const name = request.query ? request.query.name : undefined;
//log name in debugging
debug('name: '+name);
response.send({content: `Hello, ${name || 'World'}`});
将 debug 模块添加到 package.json
。
"dependencies": {
"debug": "^3.1.0"
根据您的应用,可能已包含此模块。例如,在使用 express 生成器 创建应用程序时,debug
模块已添加到 package.json
中。
安装应用依赖项。
$ npm install
3.2.2. 访问 localhost 上的调试日志
在启动应用程序时,使用
DEBUG
环境变量来启用调试日志记录。
具有调试日志的应用程序。
在启动应用程序时设置
DEBUG
环境变量,以启用调试日志记录。
$ DEBUG=myexample npm start
debug
模块
可以使用通配符
来过滤调试消息。这使用
DEBUG
环境变量进行设置。
测试您的应用程序,以调用调试日志记录。
例如,以下命令基于 REST API 级别 0 应用程序,其中调试日志被设置为记录
/api/greeting
方法中的
name
变量:
$ curl http://localhost:8080/api/greeting?name=Sarah
查看应用程序日志以查看您的调试信息。
myexample name: Sarah +3m
3.2.3. 访问 OpenShift 上的 Node.js 调试日志
在 OpenShift 中使用应用 Pod 中的
DEBUG
环境变量启用调试日志。
具有调试日志的应用程序。
已安装
oc
CLI 客户端。
使用
oc
CLI 客户端登录到您的 OpenShift 实例。
$ oc login ...
将应用部署到 OpenShift。
$ npm run openshift
这会运行
openshift
npm 脚本,该脚本将直接调用
nodeshift
。
查找 pod 的名称,并遵循日志来监视它。
$ oc get pods
$ oc logs -f pod/POD_NAME
pod 启动后,请保持此命令运行并在新的终端窗口中执行剩余的步骤。这可让您
跟踪日志
并查看其生成的新条目。
测试您的应用程序。
例如,以下命令基于 REST API 级别 0 应用程序,其中调试日志被设置为记录
/api/greeting
方法中的
name
变量:
$ oc get routes
$ curl $APPLICATION_ROUTE/api/greeting?name=Sarah
返回到 pod 日志,并注意日志中没有调试日志信息。
设置
DEBUG
环境变量以启用调试日志记录。
$ oc get dc
$ oc set env dc DC_NAME DEBUG=myexample
返回 pod 日志以观察更新推出。
推出更新后,您的 pod 将停止,您不再遵循日志。
查找新 pod 的名称并遵循日志。
$ oc get pods
$ oc logs -f pod/POD_NAME
pod 启动后,请保持此命令运行并在不同的终端窗口中执行剩余的步骤。这可让您
跟踪日志
并查看其生成的新条目。具体来说,日志将显示您的调试信息。
测试应用来调用调试日志记录。
$ oc get routes
$ curl $APPLICATION_ROUTE/api/greeting?name=Sarah
返回 pod 日志以查看调试信息。
myexample name: Sarah +3m
要禁用调试日志记录,请从 pod 中删除
DEBUG
环境变量:
$ oc set env dc DC_NAME DEBUG-
Nodeshift
是使用 Node.js 项目运行 OpenShift 部署的模块。
Nodeshift 假设您已安装了
oc
CLI 客户端,并且已登录到 OpenShift 集群。Nodeshift 还使用
oc
CLI 客户端正在使用的当前项目。
Nodeshift 使用位于项目根目录的
.nodeshift
文件夹中的资源文件来处理 OpenShift 路由、服务和 DeploymentConfig。Nodeshift 项目页面中提供了更多与
Nodeshift 项目
相关的信息。
示例应用的部署配置包含与 OpenShift 中部署和运行应用相关的信息,如路由信息或就绪度探测位置。示例应用程序的部署配置存储在一组 YAML 文件中。有关使用 Fabric8 Maven 插件的示例,YAML 文件位于
src/main/fabric8/
目录中。有关使用 Nodeshift 的示例,YAML 文件位于
.nodeshift
目录中。
Fabric8 Maven 插件和 Nodeshift 使用的部署配置文件不必完全 OpenShift 资源定义。Fabric8 Maven 插件和 Nodeshift 都可以获取部署配置文件,并添加一些缺少的信息来创建完整的 OpenShift 资源定义。Fabric8 Maven 插件生成的资源定义位于
target/classes/META-INF/fabric8/
目录中。Nodeshift 生成的资源定义位于
tmp/nodeshift/resource/
目录中。
现有示例项目。
已安装
oc
CLI 客户端。
编辑现有的 YAML 文件,或使用您的配置更新创建额外的 YAML 文件。
例如,如果您的示例已经配置了
readinessProbe
的 YAML 文件,您可以将
path
值改为不同的可用路径来检查就绪度:
spec:
template:
spec:
containers:
readinessProbe:
httpGet:
path: /path/to/probe
port: 8080
scheme: HTTP
如果现有 YAML 文件中没有配置 readinessProbe
,您还可以使用 readinessProbe
配置在同一目录中创建新的 YAML 文件。
使用 Maven 或 npm 部署示例的更新版本。
验证您的示例部署版本中是否显示您的配置更新。
$ oc export all --as-template='my-template'
apiVersion: template.openshift.io/v1
kind: Template
metadata:
creationTimestamp: null
name: my-template
objects:
- apiVersion: template.openshift.io/v1
kind: DeploymentConfig
spec:
template:
spec:
containers:
livenessProbe:
failureThreshold: 3
httpGet:
path: /path/to/different/probe
port: 8080
scheme: HTTP
initialDelaySeconds: 60
periodSeconds: 30
successThreshold: 1
timeoutSeconds: 1
...
附录 C. 配置 Jenkins 自由样式项目以使用 nodeshift 部署 Node.js 应用程序
与使用本地主机中的 nodeshift 来部署 Node.js 应用程序,您可以将 Jenkins 配置为使用 nodeshift 部署 Node.js 应用。
访问 OpenShift 集群
在同一 OpenShift 集群上运行的
Jenkins 容器镜像
。
在 Jenkins 服务器上安装的 Node.js 插件
。
Node.js 应用程序配置为使用
nodeshift
和红帽基础镜像。
附录 G. Source-to-Image (S2I)构建过程
Source-to-Image
(S2I)是一种构建工具,用于通过应用源从在线 SCM 存储库生成可重复生成的 Docker 格式容器镜像。借助 S2I 构建,您可以轻松地将最新版本的应用交付至生产中,构建时间更短、资源和网络使用、提高安全性以及许多其他优点。OpenShift 支持多种
构建策略和输入源
。
如需更多信息,请参阅 OpenShift Container Platform 文档中的
Source-to-Image (S2I)构建
章节。
您必须为 S2I 进程提供三个元素,以编译最终容器镜像:
托管在在线 SCM 存储库中的应用源,如 GitHub。
S2I Builder 镜像,作为编译镜像的基础,并提供运行应用程序的生态系统。
另外,您还可以提供
S2I 脚本使用的环境变量和参数
。
此过程会根据 S2I 脚本中指定的指令将应用程序源和依赖项注入到 Builder 镜像中,并生成运行汇编应用程序的 Docker 格式容器镜像。如需更多信息,请参阅 OpenShift Container Platform 文档中的
S2I构建要求、构建选项 以及如何构建工作
部分。
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.