Feedback

Chat Icon

GitOps the Hard Way, with Argo CD

Build Real GitOps Pipelines From Empty Clusters to Automated Deploys

Stand Up the Lab: Two Servers, a Cluster, and an App to Deploy
24%

The Application

Now that we have a server, let's create a simple application to use as a base to deploy 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 Python and Flask is based on the simplicity and ease of use of this stack. We could have used other languages and frameworks, but Python is a good choice for most people because it is 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: By default, Ubuntu 24.04 comes with this version of Python. You can check the version by running python3 --version.
  • pip: The package installer for Python.
  • virtualenv: A tool to create isolated Python development environments.
  • Flask: A lightweight WSGI web application framework for Python.

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

# Install pip and 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:

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

Install Flask and create the requirements.txt file:

# Install Flask
pip install Flask==3.0.0

# Create the requirements.txt file
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.
from flask import Flask, jsonify, request


# Create a Flask application instance.
# This instance will be the central object of the app.
app = Flask(__name__)


# Define a list to store tasks.
# Each task will be a dictionary containing details
# such as id, title, and description.
tasks = []


# Define a route to handle GET requests to retrieve all tasks.
# When a user sends a GET request to the '/tasks' endpoint,
# this function will be triggered.
@app.route('/tasks', methods=['GET'])
def get_tasks():
    # Return a JSON response containing the list of all tasks.
    # The jsonify function converts the list into a JSON format.
    return jsonify(
        {
            # Wrap the tasks list in a dictionary with the key 'tasks'.
            'tasks': tasks
        }
    )


# Define a route to handle POST requests to add a new task.
# When a user sends a POST request to the '/tasks' endpoint,
# this function will be triggered.
@app.route('/tasks', methods=['POST'])
def add_task():
    # Create a new task as a dictionary.
    # The task ID is generated by incrementing
    # the length of the current tasks list.
    task = {
        'id': len(tasks) + 1,
        # Get the 'title' of the task from the incoming JSON.
        'title': request.json['title'],
        # Get the 'description' of the task from the incoming JSON.
        'description': request.json['description']
    }

    # Append the newly created task to the tasks list.
    tasks.append(task)

    # Return the newly created task as a JSON response
    # with a 201 HTTP status code.
    # The status code 201 indicates that the resource
    # was successfully created.
    return jsonify(task), 201


# Launch the Flask application.
# This will start a local web server that listens
# on port 5000 on all interfaces ('0.0.0.0').
# The 'debug=True' argument enables the debug mode,
# which provides detailed error messages
# and auto-reloads the server when changes are detected.
if __name__ == '__main__':
    app.run(
        debug=True,
        host

GitOps the Hard Way, with Argo CD

Build Real GitOps Pipelines From Empty Clusters to Automated Deploys

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