GCP Compute Engine の e2-micro で Node-RED に最低限の構築するメモです。
Compute Engine の無料枠 2022/9 現在
2022/09 現在の GCP 無料インスタンスが e2-micro なので、ここをターゲットにやってみます。e2-micro は価格としてはお安いほうなので、有料ですぐ削除する前提でサッと試されるぶんには、お財布にも優しいかと思います。
1 つの非プリエンプティブル e2-micro VM インスタンス(1 か月あたり)。次の米国リージョンのいずれかで利用できます。
オレゴン: us-west1
アイオワ: us-central1
サウスカロライナ: us-east1
無料枠のご注意
なお実際に無料枠で使えるかどうかは、みなさんのお使いになられている状況によって違います。ご自分で無料枠として試される場合は、無料枠の定義をご確認の上、自己責任でお試しください。
Compute Engine インスタンスを作成
Compute Engine のページにアクセスして、インスタンスを作成ボタンをクリックします。
インスタンスの設定
- 名前
- 自分で考えた命名(例:node-red-instance01)
- リージョン
- 今回はオレゴンを選びました
- シリーズ
- E2
- マシンタイプ
- e2-micro
ネットワークタグの設定
詳細オプションが閉じている場合があるので開きます。
ネットワークタグを以下に設定します。
- ネットワーク タグ
- node-red
ネットワーク タグ node-red は、この後、ファイアウォール作成時に関連付く設定です。
作成
作成ボタンをクリックします。
作成を待ちます。
作成されました。
ブラウザウィンドウで SSH を開く
インスタンスができたら、ブラウザウィンドウで SSH を開きます。
SSH にアクセスできました。Linux で Debian のサーバーが起動しています。
Node-RED インストール
Raspberry Piで実行する : Node-RED日本ユーザ会
こちらを参考に、
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
アップデートコマンドを実行します。
- Are you really sure you want to do this ? [y/N] ?
- y
- Would you like to install the Pi-specific nodes ? [y/N] ?
- N
途中、聞かれるものはこのようにすすめます。「Would you like to install the Pi-specific nodes」は Raspberry Pi の特別設定をp反映するかどうかなので、仮に間違えても大枠としては影響ないです。
インストールが進みます。
All done. You can now start Node-RED with the command node-red-start or using the icon under Menu / Programming / Node-RED Then point your browser to localhost:1880 or http://{your_pi_ip-address}:1880 Started : Wed Sep 28 02:59:21 UTC 2022 Finished: Wed Sep 28 02:59:50 UTC 2022 You may want to run node-red admin init to configure your initial options and settings.
これでインストールできました。
対話的な設定ファイル作成ツールで設定
対話的な設定ファイル作成ツール node-red admin init コマンドでどんなファイルが書き出されるか見てみたメモ – 1ft-seabass.jp.MEMO
こちらを参考に対話的なファイル設定作成を行います。
node-red admin init
を実行します。対話的に進めていきます。
以下、対応ログです。
Node-RED Settings File initialisation ===================================== This tool will help you create a Node-RED settings file. V Settings file · /home/tnk_seigo/.node-red/settings.js V That file already exists. Are you sure you want to overwrite it? · Yes User Security ============= V Do you want to setup user security? · Yes V Username · V Password · **************** V User permissions · full access V Add another user? · No Projects ======== The Projects feature allows you to version control your flow using a local git repository. V Do you want to enable the Projects feature? · No Flow File settings ================== V Enter a name for your flows file · flows.json V Provide a passphrase to encrypt your credentials file · Editor settings =============== V Select a theme for the editor. To use any theme other than "default", you will need to install @node-red-contrib-themes/theme-collection in your Node-RED user directory. · default V Select the text editor component to use in the Node-RED Editor · monaco (default) Node settings ============= V Allow Function nodes to load external modules? (functionExternalModules) · Yes
各項目、このように設定しました。
特に大事なのは User Securityのところで、Username と Password を設定するとエディタへのログインパスワードが設定できます。
ファイアウォールルールの設定
ポート 1880 にアクセスするためにコンソール上部の検索からファイアウォールを検索します。
ファイアウォールルールを作成ボタンをクリックします。
以下に設定します。
- 名前
- allow-node-red
- トラフィックの方向
- 上り
- 一致したときのアクション
- 許可
- 指定されたターゲットタグ
- node-red
- ソースフィルタ
- IPv4 範囲
- 送信元 IPv4 範囲
- 0.0.0.0/0
- プロトコルとポート
- 指定したプロトコルとポート
- TCP をチェック
- 1880
このように設定します。送信元 IPv4 範囲は 0.0.0.0/0 です。私はうっかり 0.0.0.0 にしてしまって、アクセスできない変なハマり方をしたのでご注意ください!
作成ボタンをクリックして設定します。
Node-RED 起動
node-red-start
で Node-RED 起動します。
(省略) 28 Sep 03:01:26 - [info] Server now running at http://127.0.0.1:1880/ 28 Sep 03:01:26 - [warn] Encrypted credentials not found 28 Sep 03:01:26 - [info] Starting flows 28 Sep 03:01:26 - [info] Started flows
このようにログが出て起動します。
ブラウザでアクセスしてみる
URL は Compute Engine の外部 IP をコピーします。
これに :1880
をつけてアクセスします。
無事アクセスできました。先ほど設定作成ツールで User Securityのところで、Username と Password を設定するとエディタへのログインパスワードでログインしてみます。
無事ログインできました!
これで最低限の準備ができました!
自動起動の設定
今回は一度だけの起動です。インスタンスを停止したり再起動しても Node-RED が起動している自動起動を ON にしましょう。
ターミナルで、
sudo systemctl enable nodered.service
こちらのコマンドを入力します。これで設定は完了です。ちなみにオフにする場合は
sudo systemctl disable nodered.service
にします。
自動起動の確認
sudo systemctl status nodered.service
のコマンドを入力します。
● nodered.service - Node-RED graphical event wiring tool Loaded: loaded (/lib/systemd/system/nodered.service; enabled; vendor preset: enabled) Active: inactive (dead) Docs: http://nodered.org/docs/hardware/raspberrypi.html
と確認出来たら、
sudo reboot
で再起動をして、しばらく待って、先ほどアクセス出来た URL にアクセスしてみましょう。
無事、アクセスできて、自動起動の設定も成功です!
再起動時に IP が変わらないようにする設定
通常、インスタンスを停止→開始で再起動時に IP が変わってしまうので、固定したい場合は
こちらの記事を参考に、設定します。
なお、この対応には有料になります。くわしくは 仮想マシン(VM)の料金の外部 IP アドレス料金をみてみてください。2022/10 現在、$0.004/時 なので月で 300 円くらいかかるようです。
今回のインスタンスで使われている IP アドレスのアクセスタイプ外部を探します。
予約ボタンをクリックします。
node-red-static-ip として予約ボタンをクリックします。
アクセスタイプが外部の設定の種類が静的になって固定 IP 化されます。
これでインスタンスを停止→開始で再起動しても IP アドレスが変わらず固定の IP でアクセスできます。