Node-RED on Google Cloud Platform

Daz Wilkin
Google Cloud - Community
5 min readAug 22, 2016

Node-RED is very interesting. The Node-RED documentation does not include instructions for running Node-RED on Google Cloud Platform (GCP). If you haven’t used GCP, there are free options.

Update: 18–05–20

There have been (many) changes to GCP since I wrote this post almost 2 years ago. In response to Shiv Kodam, here’s an update that will help you get Node-RED running on (a) Container-Optimized OS image (container-vm is being deprecated); (b) Kubernetes Engine.

Container-Optimized OS

Container-Optimized OS uses cloud-init for configuration. Here’s a tweaked cloud-init file that runs a Node-RED container:

Create the Container-Optimized OS VM using the above cloud-init file:

gcloud compute instances create ${NODE} \
--image-family=cos-stable \
--image-project=cos-cloud \
--metadata-from-file=user-data=./cloud-init.yaml \
--machine-type=f1-micro \
--zone=${ZONE} \
--preemptible \
--project=${PROJECT}

NB This is a cheap-cheap VM f1-micro and it’s preemptible. If you want more horse-power, choose a more powerful VM.

Give the image a short amount of time to stabilize, download Node-RED and run the container. You can either ssh into the VM and then run the — command, or — as here — run them together:

gcloud compute ssh ${NODE} \
--project=${PROJECT} \
--command="sudo journalctl --unit=node-red --follow"

You want to see something similar to the following for the successful start of the node-red-docker container:

20 May 19:54:31 - [info] Server now running at http://127.0.0.1:1880/
20 May 19:54:31 - [info] Starting flows
20 May 19:54:31 - [info] Started flows

Once the container is running, you can curl the Node-RED endpoint remotely:

gcloud compute ssh ${NODE} \
--project=${PROJECT} \
--command="curl localhost:1880"

And you should see output of the form:

<!DOCTYPE html>
<html>
...
<head>
<title>Node-RED</title>
...
</head>
...

If you’d prefer not to create a firewall rule, you may use gcloud to create an ssh port-forward from the VM’s port 1880 to your local machine’s 1880:

gcloud compute ssh ${NODE} \
--ssh-flag="-L 1880:localhost:1880" \
--project=${PROJECT}

And then, from your browser:

google-chrome http://localhost:1880

When you’re done, don’t forget to blat the VM:

gcloud compute instances delete ${NODE} \
--project=${PROJECT}

Kubernetes Engine

Deploying Node-RED as a single Pod|Service to Kubernetes (Engine) is straightforward. Assuming you have a cluster that you’re authenticated against:

NAMESPACE=node-redkubectl create namespace ${NAMESPACE}
kubectl apply --filename=node-red.yaml --namespace=${NAMESPACE}
deployment.extensions "node-red" created
service "node-red" created

Then you can use this shortcut to grab one the of Kubernetes’ Nodes, determine the Node-Red Service’s NodePort (in the Console screenshot above you can see this is31156 in my case) and then using gcloud to port-forward to this NodePort on this Node:

NODE=$(\
kubectl get nodes \
--output=jsonpath="{.items[0].metadata.name}")
PORT=$(\
kubectl get services/node-red \
--namespace=${NAMESPACE} \
--output=jsonpath="{.spec.ports[0].nodePort}")
echo ${PORT}gcloud compute ssh ${HOST} \
--ssh-flag="-L ${PORT}:localhost:${PORT}" \
--project=${PROJECT}

Then you can assess Node-RED as before *but* using the NodePort (${PORT} not 1880):

google-chrome http://${HOST}:${PORT}

Aside: Chromebook

I was using a Chromebook to revise this post yesterday. It continues to be possible to run the port-forward when using a Chromebook thanks to the excellent Cloud Shell.

Using Cloud Shell, you may run the commands as above. The one difference is that Cloud Shell has a constrained set of ports (8080–8084). So, when it comes to the gcloud port-forward, please use one of these values instead of 1880, let’s use 8083 for argument’s sake:

gcloud compute ${NODE} \
--ssh-flag="-L 8083:localhost:1880"

Then — if not already changed — click “Change port”:

And set it to 8083:

And, you should see the Node-Red console as before.

That’s all!

Original Content

It’s very easy to run Node-RED on a GCP.

If you’ve not used Google Cloud Platform before, start here. Otherwise, assuming you have a project [[PROJECT-ID]] and have the Cloud SDK installed, let’s start by defining some environment variables:

PROJECT=[[PROJECT-ID]]
NODE=[[NODE-NAME]] # e.g. "node-red"
ZONE=[[ZONE]] # e.g. "us-east1-d"
CONTAINER_FILE=[[CONTAINER-FILENAME]] # e.g. "node_red.yaml"

To make things simplest, we’re going to run Node-RED in Docker on the VM. We provide a configuration file to GCP with the command to create the VM.

Create a file called whatever you’ve decided for [[CONTAINER_FILE]] and use the following text for its content. This tells GCP where to find the Docker image for Node-RED and to run it on port 1880:

apiVersion: v1
kind: Pod
metadata:
name: node-red
spec:
containers:
- name: node-red
image: nodered/node-red-docker:latest
imagePullPolicy: Always
ports:
- containerPort: 1880
hostPort: 1880

Now, to create a VM running Docker, with Node-RED installed and configured using the [[CONTAINER_FILE]], use the following command:

gcloud compute instances create $NODE \
--image=container-vm \
--metadata-from-file=google-container-manifest=$CONTAINER_FILE \
--machine-type=custom-1-2048 \
--zone=$ZONE \
--project=$PROJECT \
--tags=node-red

When the command completes and the VM is created, it will summarize the VM details including the EXTERNAL_IP address. Keep a record of this EXTERNAL_IP address as it is the IP address you will use to browse to Node-RED. You may also find the EXTERNAL_IP for your NODE with this command:

gcloud compute instances list \
--filter=NAME=$NODE \
--format='table[no-heading](EXTERNAL_IP)' \
--project=$PROJECT

In order to access the Node-RED VM from other machines, you must open the firewall for port 1880. Use this command:

gcloud compute firewall-rules create allow-node-red \
--project=$PROJECT \
--allow tcp:1880 \
--network=default \
--source-ranges=0.0.0.0/0 \
--target-tags=node-red

That’s it!

You should now be able to visit Node-RED using the following URL. Replace EXTERNAL_IP with the IP address that you determined previously:

http://[[EXTERNAL_IP]]:1880
Node-Red — ready for use!

And, then with the “first flow” deployed:

Tear-Down

To delete the VM and delete the firewall, use the following commands:

gcloud compute instances delete $NODE \
--project=$PROJECT \
--quiet
gcloud compute firewall-rules delete allow-node-red \
--project=$PROJECT \
--quiet

Conclusion

It’s very easy to run Node-RED in a container on a VM in Google Cloud Platform. Have fun!

--

--