Feedback

Chat Icon

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Requirements and Setup
15%

The Application

Now that we have a server, let's create a simple application to use it as a base to 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, to create the API. The choice of using Python and Flask is based on the simplicity and ease of use of these tools. We could have used other 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: You can check Python version by running python3 --version and if you don't have Python 3.12, you can install it using apt install python3.12.
  • Pip: The package installer for Python.
  • virtualenv: A tool to create isolated Python 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 Virtualenv
apt update
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.
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

Cloud Native CI/CD with GitLab

From Commit to Production Ready

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