Building your App ready for Kubernetes deployment

Cloud Platform (intermediate level) posted on 20th March 2018

Building your demo app

Before getting into the Kubernetes configuration, the first step is to create a demo App, which will run on Node/Express. It'll playback any post body, or the contents of the ?data= parameter on a GET request. It'll also support CORS so you can test it from a JavaScript app. 

const server = require("./playback");
console.log ("v4");
// start listening
const port =  8080;
const ip =  "";, ip);
console.log("listening on:" + ip + ":" + port);

// test kubernetes stuff
module.exports = ((ns) => {
  const express = require('express');
  const app = express();
  const bodyParser = require('body-parser'); = app;
  // CORS
  const copts = {
    origin: (origin, callback) => {
      // i could do whitelisting here .. test the origin against a list and replace true with result
      callback(null, true);
  // allowing cors
  // json parsing

    extended: true

  // play back data param
  app.get('/',  (req, res) => {
    console.log ("get / ",;
    return res.status(200).send (;

  // post ("/", (req,res) => {
    console.log ("post /", req.body);
    return res.status(201).send(req.body);
  return ns;


  "name": "playback",
  "version": "1.0.0",
  "description": "testing kubernetes",
  "main": "index.js",
  "dependencies": {
    "async": "^2.6.0",
    "body-parser": "^1.18.2",
    "cors": "^2.8.4",
    "express": "^4.16.2"
  "devDependencies": {
    "axios": "^0.18.0",
    "chai": "^4.1.2",
    "mocha": "^5.0.3"
  "scripts": {
    "start": "node index.js",
    "test": "mocha ~/workspace/kube/tests"
  "author": "bruce mcpherson",
  "license": "ISC"

Building a docker image

So that Kubernetes can access your app, you need to build an image, then load it somewhere accessible to both you and Kubernetes. The Google Cloud container registry is perfect for that, so the steps are
  • create your app
  • test it
  • create a Docker image
  • load it to Google Cloud Registry.

Testing the app

Before creating an image, you can test it using the web preview feature of the cloud shell
  • Start your app
    npm start
  • Kick off web preview mode on port 8080

  • pass a data parameter
  • The result should be echoed

Creating and image and loading to the container registry

First create a simple Dockerfile. This will create an image with Node (from the Alpine build), plus the dependencies, and the App
FROM node:alpine
MAINTAINER Bruce Mcpherson <>

#create a workdirectory
WORKDIR /usr/src/kube

#get the package files
COPY package.json ./

#install the dependencies
RUN npm install

#copy the files required
COPY *.js ./

#expose the port we're listening on

#how to run it
CMD [ "npm", "start" ]

Next, a shell script like this
docker build -t brucemcpherson/playback .
docker tag brucemcpherson/playback
gcloud docker -- push

executing this script will
  • make an image
  • tag it with the the project name and the gcr repo
  • push it to gcr
and in your cloud console
Ready for Kubernetes deployment - go back to Getting an API running in Kubernetes for the next stage.

Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.