Node-RED 介绍
- Node-RED 是一种基于流程的编程工具
- 由 IBM 的新兴技术服务团队原创开发
- Node-RED 是一种事件触发工具,和 StackStorm 类似, 可以归类为上层的自动化工具,可以用来触发与之相对应的下层自动化工具,比如 ansible,来更加优化的完成自动化任务。
Node-RED 环境
我们首先需要明白 Node-RED 是基于Node.js来运行的。我们可以使用 Web 浏览器来访问到流程编辑器。我们可以使用拖拽和连线的方式来创建应用,也就是将所需的节点从面板拖拽到工作区中,然后用连接这些节点到一起。通过一键部署方式可以自动将已经创建好的应用部署和运行。
Node-RED 安装 之前 - 在 CentOS 中安装Node.js
添加 node.js yum repository
首先我们需要将 node.js 的 yum repository 添加到我们的系统中,它来源于nodejs的官方网站。
当前 LTS 版本是版本 14。
Index of /download/release/latest-v14.x/nodejs.org如果您想安装版本 8,只需在下面的命令中更改setup_14 到 setup_8 x。
[alick@devnet ~]# curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -
其他组件如下:
## You may also need development tools to build native addons:
[alick@devnet ~]# sudo yum install -y gcc-c++ make## To install the Yarn package manager, run:
[alick@devnet ~]# curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
[alick@devnet ~]# sudo yum install yarn
安装 node.js 和 NPM
查看将要安装的版本
[alick@devnet ~]# sudo yum list available nodejs
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
Loading mirror speeds from cached hostfile
Available Packages
nodejs.x86_64 2:14.5.0-1nodesource
然后安装 node.js 包和 NPM 包,运行以下命令执行此操作。
## Run `sudo yum install -y nodejs` to install Node.js 14.x and npm.
[alick@devnet ~]# sudo yum install -y nodejs
如果你装错了版本或者你要从旧版本升级,你可以使用如下的命令
[alick@devnet ~]# rm -f /etc/yum.repos.d/nodesource-el*
[alick@devnet ~]# curl -sL https://rpm.nodesource.com/setup_14.x | bash -
[alick@devnet ~]# sudo yum install -y nodejs
查看 node.js 和 npm 版本
使用如下命令来查看 node 和 npm 的版本
[alick@devnet ~]# node -v
v14.5.0[alick@devnet ~]# npm -v
6.14.5
使用简单的 javascript 代码来测试安装环境
我们创建一个文件 devnet.js
其中 192.168.100.100 你可以改为你 centos 的 ip 地址,12345 可以改为你需要访问的端口。
[alick@devnet ~]# cat devnet.js
var http = require('http');
http.createServer(function (req, res) {res.writeHead(200, {'Content-Type': 'text/plain'});res.end('Welcome to DevNet SG');
}).listen(12345, "192.168.100.100");
console.log('Server running at http://192.168.100.100:12345/');
然后使用下面的代码来运行
[alick@devnet ~]# node --inspect devnet.js
Debugger listening on ws://127.0.0.1:9229/b7c74321-a226-4bcb-9e1d-bc0e1c90379d
For help, see: https://nodejs.org/en/docs/inspector
Server running at http://192.168.100.100:12345/
可以在浏览器中访问,运行成功!
Node-RED 正式安装
这个时候 node.js 的环境已经安装完成,下面将开始安装 Node-RED
直接执行下面命令即可
[alick@devnet ~]# sudo npm install -g node-red
然后我们运行 node-red, 可以看到一些输出信息
[alick@devnet ~]# node-red
9 Jul 18:34:12 - [info] Welcome to Node-RED
===================9 Jul 18:34:12 - [info] Node-RED version: v1.1.1
9 Jul 18:34:12 - [info] Node.js version: v14.5.0
9 Jul 18:34:12 - [info] Linux 3.10.0-1062.el7.x86_64 x64 LE
9 Jul 18:34:12 - [info] Loading palette nodes
9 Jul 18:34:13 - [info] Settings file : /root/.node-red/settings.js
9 Jul 18:34:13 - [info] Context store : 'default' [module=memory]
9 Jul 18:34:13 - [info] User directory : /root/.node-red
9 Jul 18:34:13 - [warn] Projects disabled : editorTheme.projects.enabled=false
9 Jul 18:34:13 - [info] Flows file : /root/.node-red/flows_Al-TERAFORM-CENTOS-1.json
9 Jul 18:34:13 - [info] Creating new flow file
9 Jul 18:34:13 - [warn] ---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------9 Jul 18:34:13 - [info] Server now running at http://127.0.0.1:1880/
9 Jul 18:34:13 - [info] Starting flows
9 Jul 18:34:13 - [info] Started flows
PM2是Node.js的进程管理工具,利用它可以非常容易地实现开机应用自动启动以及必要时自动启动的功能。
然后我们可以使用 pm2 来守护进程
[alick@devnet ~]# sudo npm install -g pm2
然后我们启动 node-red
[alick@devnet ~]# pm2 start node-red-------------__/____/____________/____/______//_/________/______/_______/_///____///_///______//___//__//_/___________//____//____/_____/________///______/_____________/____///_____/_____///_________/_____________/_____________/___//____________/_____________/_____________/__/__///______________///______________///__///__Runtime EditionPM2 is a Production Process Manager for Node.js applicationswith a built-in Load Balancer.Start and Daemonize any application:$ pm2 start app.jsLoad Balance 4 instances of api.js:$ pm2 start api.js -i 4Monitor in production:$ pm2 monitorMake pm2 auto-boot at server restart:$ pm2 startupTo go further checkout:http://pm2.io/-------------[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /usr/bin/node-red in fork_mode (1 instance)
[PM2] Done.
┌─────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ node-red │ default │ N/A │ fork │ 24229 │ 0s │ 0 │ online │ 0% │ 13.5mb │ root │ disabled │
└─────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
然后我们重启所有 ProcessId
[alick@devnet ~]# pm2 restart all
Use --update-env to update environment variables
[PM2] Applying action restartProcessId on app [all](ids: [ 0 ])
[PM2] [node-red](0) ✓
┌─────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ node-red │ default │ N/A │ fork │ 24257 │ 0s │ 1 │ online │ 0% │ 13.2mb │ root │ disabled │
└─────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
我们可以查看 node-red 相关系统信息如下:
[alick@devnet ~]# pm2 info node-redDescribing process with id 0 - name node-red
┌───────────────────┬────────────────────────────────────┐
│ status │ online │
│ name │ node-red │
│ namespace │ default │
│ version │ N/A │
│ restarts │ 1 │
│ uptime │ 64s │
│ script path │ /usr/bin/node-red │
│ script args │ N/A │
│ error log path │ /root/.pm2/logs/node-red-error.log │
│ out log path │ /root/.pm2/logs/node-red-out.log │
│ pid path │ /root/.pm2/pids/node-red-0.pid │
│ interpreter │ node │
│ interpreter args │ N/A │
│ script id │ 0 │
│ exec cwd │ /root │
│ exec mode │ fork_mode │
│ node.js version │ 14.5.0 │
│ node env │ N/A │
│ watch & reload │ ✘ │
│ unstable restarts │ 0 │
│ created at │ 2020-07-09T10:38:40.795Z │
└───────────────────┴────────────────────────────────────┘Actions available
┌────────────────────────┐
│ km:heapdump │
│ km:cpu:profiling:start │
│ km:cpu:profiling:stop │
│ km:heap:sampling:start │
│ km:heap:sampling:stop │
└────────────────────────┘Trigger via: pm2 trigger node-red <action_name>Code metrics value
┌────────────────────────┬───────────┐
│ Heap Size │ 26.41 MiB │
│ Heap Usage │ 93.64 % │
│ Used Heap Size │ 24.73 MiB │
│ Active requests │ 0 │
│ Active handles │ 4 │
│ Event Loop Latency │ 0.45 ms │
│ Event Loop Latency p95 │ 1.55 ms │
└────────────────────────┴───────────┘Divergent env variables from local env Add your own code metrics: http://bit.ly/code-metricsUse `pm2 logs node-red [--lines 1000]` to display logsUse `pm2 env 0` to display environment variablesUse `pm2 monit` to monitor CPU and Memory usage node-red
我们可以查看 node-red 相关日志信息如下:
[alick@devnet ~]# pm2 logs node-red
[TAILING] Tailing last 15 lines for [node-red] process (change the value with --lines option)
/root/.pm2/logs/node-red-error.log last 15 lines:
/root/.pm2/logs/node-red-out.log last 15 lines:
0|node-red | ---------------------------------------------------------------------
0|node-red | Your flow credentials file is encrypted using a system-generated key.
0|node-red |
0|node-red | If the system-generated key is lost for any reason, your credentials
0|node-red | file will not be recoverable, you will have to delete it and re-enter
0|node-red | your credentials.
0|node-red |
0|node-red | You should set your own key using the 'credentialSecret' option in
0|node-red | your settings file. Node-RED will then re-encrypt your credentials
0|node-red | file using your chosen key the next time you deploy a change.
0|node-red | ---------------------------------------------------------------------
0|node-red |
0|node-red | 9 Jul 18:38:45 - [info] Starting flows
0|node-red | 9 Jul 18:38:45 - [info] Started flows
0|node-red | 9 Jul 18:38:45 - [info] Server now running at http://127.0.0.1:1880/
我们保存现在的 process 信息然后设置为开机启动
[alick@devnet ~]# pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2[alick@devnet ~]# pm2 startup
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failureExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill[Install]
WantedBy=multi-user.targetTarget path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-root.service to /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save[PM2] Remove init script via:
$ pm2 unstartup systemd
访问浏览器界面,我们可以通过 1880 端口来正常访问 Node-RED 了。
访问的链接为 http://{Node-RED-machine-ip-address}:1880
到这里,通过这篇详细的教程,相信大家已经了解如何安装 Node-RED 了,欢迎关注,我们将继续分享和深入学习。