Hello Node.js on Rancher

In this post I’m going to show how to go through the Kubernetes Hello World Walkthrough but using Rancher instead of Google’s Cloud Platform. One of the reasons I wanted to install Kubernetes on my own resources instead of in the cloud is so that I don’t have to pay additional costs while I’m experimenting/learning.

You’ll need to have done the following before proceeding:

Create the Docker image

I’m going to build the Docker image on my Rancher machine (a VM), but you can build it anywhere. If you decide to build the Docker image somewhere other than the Rancher machine, you’ll need to push your image up to Docker Hub. You may be able to add a container registry to Rancher, but I haven’t explored that. You can also use my image.

Run the following on the Rancher machine.

mkdir hello-node
cd hello-node

Create a file named Dockerfile with the following contents:

FROM node:4.4
COPY server.js .
CMD node server.js

Create a file named server.js with the following contents:

var http = require(‘http’);
var handleRequest = function(request, response) {
response.end(“Hello World!”);
var www = http.createServer(handleRequest);

Build the Docker image (that’s a period after v1):

docker build -t chrisgreene/hello-node:v1 .

If you need to upload the image to Docker Hub (make sure to change the image name or it will conflict with mine and fail):

docker login
docker push

Create the deployment

Now that we have our Docker image ready to go we can create the deployment. To do so:

  1. Make sure you’re in the Kubernetes (k8s) environment.
  2. Select Kubernetes
  3. Select Kubectl
  4. You’ll now have a shell to enter commands


Enter the following command (replace the image name if you’re using your own):

kubectl run hello-node –image=chrisgreene/hello-node:v1 –port=8080


If the deployment was successfully created, you’ll see the following:


Verify our deployment:


If we get all of the pods, we will see the hello-node pod:


You can also view the replica sets by running kubectl get rs.

Select Infrastructure > Containers and you’ll see the hello-node containers:


Expose the Node.js service to the outside

In order to reach the Node.js service, we need to expose it. We can do this with the following command:

kubectl expose deployment hello-node –port=80 –target-port=8080 –external-ip= is the IP of my Rancher machine. You’ll most likely need to change this. If you’re copy and pasting from this example, make sure there are two dashes in front of port, target-port and external-ip. Sometimes those get lost during copy/paste and the command won’t work.


Let’s verify our service:


Now I can access my Node.js app:


Scale the app

Let’s scale the app to 4 replicas instead of one:


Verify that we now have 4 pods:


Upgrade the app

I’m not going to show the steps to upgrade the app, but they are exactly as described in Roll out an upgrade to your website.

I performed the steps and was able to see the new site:


Deploying Kubernetes with Rancher

In a previous post I showed how to deploy Rancher. In this post I want to show how to deploy Kubernetes with Rancher and then deploy a simple application on top of Kubernetes. Please refer to the previous post for installation procedures for what OS to use and how to install docker. You can read up to “Start the Rancher Server” of that post and then come back to this post.

Starting the Rancher Server

You can skip this step if you’ve already started the Rancher server from the previous post. If not, let’s grab the latest version of Rancher and start the container. If you’re copying and pasting, make sure that there are two dashes before restart in the command below. Sometimes when I was copying and pasting the dashes would get converted into a single dash and the command would fail:

sudo docker run -d –restart=always -p 8080:8080 rancher/server

Now you should be able to access the Rancher application by opening a web browser and hitting the IP/URL of the VM where the Rancher container was launched.

We need to create a new environment so let’s:

  1. Highlight Default Environment
  2. Select Manage Environments

Create the kubernetes environment


Select Add Environment


  1. For Container Orchestration select kubernetes
  2. Provide a name
  3. Press Create


To access the Kubernetes environment,

  1. Highlight Environment Default
  2. Select k8s


Let’s go ahead and add the first host by selecting Add Host:


On the next screen I’m going to use the IP address of the VM running my Rancher container to make things simpler by not having to worry about name resolution.


On the next screen:

  1. Leave the host type as custom
  2. Select the clipboard to copy the command
  3. Press close
  4. Paste the command into the CLI of your VM running the Rancher container


2016-06-28_20-33-52.jpgDocker should pull down the Rancher agent container:


Kubernetes is now starting:


If you want to see more details or troubleshoot an issue, select Infrastructure > Containers:


Select Kubernetes > System to view all of the Kubernetes services:


Launching a web server on Kubernetes

Now we are going to run a simple nginx server. Let’s first start by creating a new Replication Controller by selecting:

  1. Kubernetes
  2. Replication Controllers
  3. Add RC


Paste in the following:

apiVersion: v1
kind: ReplicationController
name: nginx
replicas: 2
app: nginx
name: nginx
app: nginx
– name: nginx
image: nginx
– containerPort: 80

To find out more about replication controllers, I’d suggest reading about them here, but I’ll cover a few things:

  • Replicas states that we want two containers running nginx
  • We apply the label app: nginx. This can be used to select the containers later.
  • image: nginx specifies the name of the docker iamge to pull down
  • We are going to expose port 80 on the container.

It shouldn’t take too long for both of the containers to be running. Notice the IP addresses. These IPs most likely won’t be accessible from your machine so you won’t have a way of accessing the nginx web server.


We can access our nginx web servers by exposing them via a Kubernetes Service.

To create the service select:

  1. Kubernetes
  2. Services
  3. Add Service


Paste in the following and press Create:

kind: Service
apiVersion: v1
name: “nginx-service”
app: nginx
– name: http
protocol: TCP
port: 80
targetPort: 80
– “”


The IP is the IP of the VM running the Rancher/Kubernetes services.

If we expand our nginx-service, we can see that it’s associated with the two nginx containers. How did it do this? It used the selector app: nginx defined in the service  to find all containers with the label “app: nginx”. This is an important concept in Kubernetes.


Now if I open a web browser and go to, I’ll see that nginx is running:


Getting started with Rancher

A few days ago Rancher Labs released Rancher 1.0 so I’d thought I’d take it for a test drive. This is the first time I’ve worked with a product like this so this post will be really basic, but if this is the way things are going, it’s pretty amazing. Take a moment to check out their site and watch the “See Rancher in Action” video. The speaker sounds like a cowboy so you can imagine me talking like a cowboy for the rest of this post. I’m going to show how to deploy an application named Rocket Chat, which is like Slack. In my next post on Rancher I’ll show how to deploy Kubernetes using Rancher and then deploy and an application on Kubernetes.


I’m going to mainly be following the Quick Start Guide 

I started with a Ubuntu 12.04.5 LTS VM running on ESXi 5.1. My VM’s name is rancher1a.vmware.local with an IP of

First let’s update the OS:

sudo apt-get update
sudo apt-get upgrade

Install the latest version of Docker by using the following commands or using Docker’s Instructions.

curl https://get.docker.com/ > docker-install.sh
Check out the docker-install.sh script to see what it’s doing.
chmod 700 docker-install.sh

Adding myself into the docker group:

sudo usermod -aG docker chris

Start the Rancher server

We will run the Rancher server in a container by running:

sudo docker run -d –restart=always -p 8080:8080 rancher/server

Verify that the container is running:


Now I can access Rancher by going to http://rancher1a.vmware.local:8080. You’ll be logged in automatically and will see the screen where you can add your first host:


Go ahead and select Add Host and we see that the VM that is running Rancher has been pre-populated. For this demonstration I’m going to leave things as is and press Save.


On the next screen, I’ll perform the following:

  1. Enter the IP of the VM where Ranch is running.
  2. Select the copy button
  3. Paste the copied text into the terminal running your Rancher container.
  4. Select Close


Let’s verify that the Rancher agent container is running:


Now go to:

  1. Infrastructure
  2. Hosts
  3. View the newly added host


You can click on the hostname and view a bunch of info:


Launching an App

We can view the built-in catalog by going to Catalog > All


Let’s deploy the RocketChat app by selecting View Details:


I’m going to leave everything at the defaults and select Launch:


You should be redirected to Applications > Stacks where you can see the application starting up:


At this point I like to switch over to the terminal that’s running the Rancher container and run the sudo watch docker ps command so that I can see the containers coming online.


It shouldn’t take long for everything to become active:


Now I can access the Rocket Chat instance at http://rancher1a.vmware.local:3000. From here you need to register for a new account:


Fill in some info. The email address doesn’t have to be real:


Acknowledge the warning that pops up and select a username:


You’ll be logged in where you can begin using the application:


The WordPress app is also simple to deploy so you may want to try that as well.