Serving Static Assets in Django With Kubernetes

Serving Static Assets in Django With Kubernetes

Django, React, and Kubernetes in GKE. This article focuses on a strategy to deploy static assets, specifically assets generated with the create-react-app toolchain. The assets are served using Django deployed in a Kubernetes cluster on the Google cloud GKE platform.

This article focuses on a strategy to deploy static assets, specifically assets generated with the create-react-app toolchain. The assets are served using Django deployed in a Kubernetes cluster on the Google cloud GKE platform.

The technologies used include Django, React, Dockergcloud and its related CLIs, e.g. gsutil. It explores an example of a Docker build pipeline that generates the image and pushes the static assets to a gcloud bucket. The article borrows heavily from Google’s own guide titled “Running Django on Google Kubernetes Engine” _(referencedas Django GKE_ for the rest of the article).

It was found that the GKE guide had some blind spots, or rather specific use cases, that were not covered in the guide, and some of these gaps are addressed here. Anything not mentioned in this article is assumed to be covered in the Django GKE guide (e.g., creating GKE clusters as well as buckets and databases) so refer to it if you need to have the whole picture.

The article is divided into three main sections: the React App section, the Django section, and the build and deploy section. Everything in this article can be found in the django_react_k8s GitHub repository. As such, we will only look at the core points as opposed to going into detail on how to set it all up.


React App Setup

In the django_react_k8s repository, the static assets can be found in the ./static folder and were created using the following command.

npx create-react-app static

This installs a standard React application, but we need to make some modifications in order to serve static assets to Django in a production setting. The first set of modifications is to install packages that allow us to customize the create-react-app config. There packages can be installed by running:

yarn add -D react-app-rewired customize-cra [email protected] write-file-webpack-plugin
  • The first package is the react-app-rewired which overrides the create-react-app webpack configs without having to eject it.
  • The customize-cra works in conjunction with react-app-rewired and provides useful utilities to perform the overrides.
  • The webpack-bundle-tracker spits out some stats about the webpack compilation process to a file. In this case, the stats file will provide an interface that makes Django aware of the different assets that have been generated, including their locations on disk or in the cloud. This is useful as filenames tend to be dynamically hashed during the production build process, and it will be quite cumbersome to keep hardcoding the changes in the Django app. Note the version to install is 0.4.3 due to a bug with the package in newer versions (see issue 227).
  • The write-file-webpack-plugin is a handy package that writes the generated static files to the file system during the development cycle. Using yarn start allows static content to be also accessed and served from Django locally.

javascript python kubernetes react programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

50+ Useful Kubernetes Tools for 2020 - Part 2

Our original Kubernetes tool list was so popular that we've curated another great list of tools to help you improve your functionality with the platform.

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Guide to Python Programming Language

Guide to Python Programming Language

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.