Coder Social home page Coder Social logo

cgn170 / static-app-nginx-kubernetes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stevenacoffman/static-app-nginx-kubernetes

0.0 1.0 0.0 780 KB

Containerizing a static application for Kubernetes

License: MIT License

Shell 24.92% JavaScript 27.64% CSS 28.18% HTML 1.90% Dockerfile 5.24% Svelte 12.13%

static-app-nginx-kubernetes's Introduction

static-app-nginx-kubernetes

Containerizing a static application for Kubernetes

What the heck?

Single page apps deliver fantastically rich user experiences, and they open up an entirely different avenue for continuous deployment. Separating out a front-end application from the server is a sound strategy for breaking up the responsibilities of the team. Maintaining a separate front-end code base allows teams to iterate on features quickly and interact through formalized contracts in the form of an API.

Not everything about delivering static assets is so rosy though. From Continuously Deploying Single Page Apps

Routing in a Single Page Application

If you are build a SPA with react, you probably use react-router. There's svelte and angular equivalents. You should be clear that when we click on an internal link in a static app page, things are a little different from the traditional web page:

  • In a traditional static html web page, the browser will send a request to nginx server for a new html page matching that url.
  • In react-router, history will listen to the url of the browser, and when it changes, it will find the proper component to render, and maybe send async request to ask for data.

For example, when the single page application changes the location to /users, there is no static file /users.html to serve on nginx. The browser just changes the url and renders some new component. This works great, until that URL gets bookmarked, reloaded, or shared outside the context of that running application.

In this setup, when a user bookmarks this URL, and restarts the page with the url /users, nginx should first try to find /users.html. Of course NGINX fails to find this file, and so it will try to return index.html, and let the browser handle the rest.

Continuous Integration for a Single Page Application

If you want to perform continuous integration or continuous delivery of a static (single page) application, you need an ephemeral version of it somewhere you can test against.

Multistage docker builds

An ephemeral, immutable web app container that is optimized for production is great, but the developer needs to have a richer experience without comprimising the fidelity of simulating the production environment. Rex Roof had a genius solution for this.

When building a Dockerfile with multiple build stages, --target can be used to specify an intermediate build stage by name as a final stage for the resulting image. Commands after the target stage will be skipped. This way the local development container can be part of the production build pipeline, but each can be tailored for specific needs.

FROM node:10.15.0-alpine as development
...
FROM node:10.15.0-alpine as build
...
FROM nginx:1.15.8-alpine as production
...

$ docker build -t development --target development .
$ docker run development
...
$ export GIT_REVISION=$(git rev-parse HEAD)
$ docker build --build-arg GIT_REVISION -t "${REPOSITORY}:${GIT_REVISION}" .

Some other nginx configs to look at:

Prometheus Metrics and Horizontal Pod Autoscaling

Autoscaling deployments in Kubernetes is more exciting since HorizontalPodAutoscaler can scale on custom and external metrics instead of simply CPU and memory like before. Requests per second is really a better metric for scaling certain applications.

This setup has metrics available on /metrics

svelte-todomvc

svelte-todomvc.surge.sh

TodoMVC implemented in Svelte. The entire app weighs 3.5kb zipped.

Where did this content actually come from (useful links):

static-app-nginx-kubernetes's People

Contributors

dependabot[bot] avatar stevenacoffman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.