This is a step in Getting an API running in Kubernetes Building your demo appBefore 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 imageSo 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
Testing the appBefore creating an image, you can test it using the web preview feature of the cloud shell
https://8080-dot-3155262-dot-devshell.appspot.com/?authuser=0&data=helloworld
Creating and image and loading to the container registryFirst 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" ] 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
and in your cloud console Ready for Kubernetes deployment - go back to Getting an API running in Kubernetes for the next stage.
|
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Google cloud platform > Getting an API running in Kubernetes >