Flask website up and running

python flask kubernetes

Background

For a while, I have been thinking of setting up a website where I can write about technology including Kubernetes, containerization, Linux, *BSD, and various technologies I have a deep passion for. Initially, I considered using a static site generator such as Hugo or Jekyll. It had to be easy to deploy in my k3s cluster and easy to maintain. However, I did not find these solutions easy to customize, so I decided to try Flask, a Python web framework I used to play with about 5-10 years ago.

Flask development

I managed to create a very simple Flask website quickly using Bootstrap and Flask-FlatPages, which did most of the work for me. It was easy to plug Markdown into it, allowing me to write posts in Markdown with minimal effort.

Docker image

Today, it is essential to expose the deliverable in a Docker image, so I used a Python Alpine image to encapsulate the application. The entrypoint is a Gunicorn webserver that is ready to go and exposes a port.

Here is the Dockerfile I came up with.

FROM python:alpine3.20

WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# copy project
COPY . /usr/src/app/

CMD gunicorn --bind 0.0.0.0:39782 app:app --log-level=debug --workers=2 --chdir /usr/src/app
EXPOSE 39782

Kubernetes deployment

I wrote a Kubernetes deployment to deploy it into my k3s cluster in a dedicated namespace. It was simple to get it running and exposing it as an ingress resource. The specifics of how that is achieved using cloudflared and Cloudflare Zero Trust will be material for a dedicated article at a later date.