This is a step in Getting an API running in Kubernetes

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.

index.js

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

playback.js

// test kubernetes stuff
module.exports = ((ns) => {
  const express = require('express');
  const app = express();
  const bodyParser = require('body-parser');

  ns.app = 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
  app.use(require('cors')(copts));
  
  // json parsing

  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({
    extended: true
  }));

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

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

})({});

package.json

{
  "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
    https://8080-dot-3155262-dot-devshell.appspot.com/?authuser=0&data=helloworld
  • The result should be echoed

Creating an 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

Dockerfile

FROM node:alpine
MAINTAINER Bruce Mcpherson <bruce@mcpher.com>

#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
EXPOSE 8080

#how to run it
CMD [ "npm", "start" ]</bruce@mcpher.com>

Next, a shell script like this

makedock.sh
docker build -t brucemcpherson/playback .
docker tag brucemcpherson/playback  gcr.io/fid-sql/playback
gcloud docker -- push gcr.io/fid-sql/playback

executing this script will

  • make an image
  • tag it with the project name and the gcr repo
  • push it to gcr

and in your cloud console

Ready for Kubernetes deployment see the next steps below

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