Feedback

Chat Icon

End-to-End Kubernetes with Rancher, RKE2, K3s, Fleet, Longhorn, and NeuVector

The full journey from nothing to production

Setting Up the Foundation
11%

The Application

The following steps will be executed on the workspace server.

Let's create an application to use as a sample and deploy it to Kubernetes later. We will use Python to create a simple todo API. We will use Flask, a lightweight Python web application framework. The choice of using Python and Flask is based on the simplicity and ease of use of this stack. We could have used other programming languages and frameworks, but Python is a good choice for most people as it's easy to understand. After all, the goal here is not learning Flask or any other framework, but having a simple application to use.

You will need to have the following packages installed on your server:

  • Python 3.12.
  • pip: The package installer for Python.
  • virtualenv: A tool to create isolated Python development environments.
  • Flask: A lightweight WSGI web application framework for Python.

SSH into the workspace server

ssh root@$WORKSPACE_PUBLIC_IP

Here are the commands to proceed with the installation on your workspace server:

# Update the package list
apt update

# Install Python 3.12
apt install python3.12 -y

# Install Virtualenv
apt install python3-pip -y
pip install virtualenv --break-system-packages

Create the following directory structure:

mkdir -p $HOME/todo/app

Create a new virtual environment:

cd $HOME/todo/app

# Create a new virtual environment
virtualenv venv

# Activate the virtual environment
source venv/bin/activate

Add a .gitignore file to ignore

curl -fsSL https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore \
-o $HOME/todo/app/.gitignore

Install Flask and create the "requirements.txt" file:

pip install Flask==3.0.0
pip freeze > $HOME/todo/app/requirements.txt

Use the following command to create the todo application:

cat << EOF> $HOME/todo/app/app.py
# Import the required libraries from Flask and SQLite.
from flask import Flask, jsonify, request
import sqlite3
import os

# Create a Flask application instance.
app = Flask(__name__)

# Define the database file path.
DATABASE_NAME = os.environ.get('DATABASE_NAME', 'todo.db')
DATABASE_FOLDER = os.environ.get('DATABASE_PATH', "/var/data/todo-app")
DATABASE_PATH = f'{DATABASE_FOLDER}/{DATABASE_NAME}'

# Ensure the directory exists.
os.makedirs(os.path.dirname(DATABASE_PATH), exist_ok=True)

# Database initialization function
def init_db():
    # Connect to the SQLite database (it will be created if it doesn't exist).
    conn = sqlite3.connect(DATABASE_PATH)
    cursor = conn.cursor()

    # Create the 'tasks' table if it doesn't already exist.
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS tasks (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            title TEXT NOT NULL,
            description TEXT NOT NULL
        )
    ''')

    # Commit changes and close the connection.
    conn.commit()
    conn.close()

# Define a route to handle GET requests to retrieve all tasks.
@app.route('/tasks', methods=['GET'])
def get_tasks():

End-to-End Kubernetes with Rancher, RKE2, K3s, Fleet, Longhorn, and NeuVector

The full journey from nothing to production

Enroll now to unlock all content and receive all future updates for free.