Feedback

Chat Icon

Helm in Practice

Designing, Deploying, and Operating Kubernetes Applications at Scale

Setting up the Environment
13%

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 to-do 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 files:

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():

Helm in Practice

Designing, Deploying, and Operating Kubernetes Applications at Scale

Enroll now to unlock current content and receive all future updates for free. Your purchase supports the author and fuels the creation of more exciting content. Act fast, as the price will rise as the course nears completion!

Unlock now  $15.99$11.99

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More